Skip to content

Commit

Permalink
Merge pull request #116 from Kurozora/username-settings
Browse files Browse the repository at this point in the history
  • Loading branch information
kiritokatklian authored Apr 19, 2021
2 parents 7f4c8d1 + 8c2a32d commit 64c778b
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 12 deletions.
25 changes: 22 additions & 3 deletions app/Actions/Web/UpdateUserProfileInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Models\User;
use App\Rules\ValidateProfileImage;
use App\Rules\ValidateEmail;
use App\Rules\ValidateUsername;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Support\Facades\Validator;
use App\Contracts\UpdatesUserProfileInformation;
Expand All @@ -28,10 +29,18 @@ class UpdateUserProfileInformation implements UpdatesUserProfileInformation
*/
public function update(User $user, array $input)
{
Validator::make($input, [
$rules = [
'email' => ['required', new ValidateEmail, Rule::unique(User::TABLE_NAME)->ignore($user->id)],
'photo' => [new ValidateProfileImage],
])->validateWithBag('updateProfileInformation');
];

if (settings('can_change_username')) {
$rules = array_merge($rules, [
'username' => ['required', new ValidateUsername]
]);
}

Validator::make($input, $rules)->validateWithBag('updateProfileInformation');

if (isset($input['photo'])) {
$user->updateProfileImage($input['photo']->getRealPath());
Expand All @@ -42,9 +51,19 @@ public function update(User $user, array $input)
$this->updateVerifiedUser($user, $input);
} else {
$user->forceFill([
'email' => $input['email']
'email' => $input['email'],
])->save();
}

if (settings('can_change_username')) {
if ($input['username'] !== $user->username) {
$user->forceFill([
'username' => $input['username']
])->save();

settings('can_change_username', false, true);
}
}
}

/**
Expand Down
5 changes: 3 additions & 2 deletions app/Helpers/CustomHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ function ios_app_url($path) {
*
* @param ?string $key
* @param mixed $value
* @param bool $setEmptyValue
* @return mixed
*/
function settings(?string $key = null, mixed $value = null): mixed
function settings(?string $key = null, mixed $value = null, bool $setEmptyValue = false): mixed
{
/** @var Settings $settings */
$settings = app(Settings::class);

return $settings->settings($key, $value);
return $settings->settings($key, $value, $setEmptyValue);
}
5 changes: 3 additions & 2 deletions app/Helpers/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,16 @@ protected function persist(): bool
*
* @param ?string $key
* @param mixed $value
* @param bool $setEmptyValue
* @return mixed
*/
public function settings(?string $key = null, mixed $value = null): mixed
public function settings(?string $key = null, mixed $value = null, bool $setEmptyValue = false): mixed
{
if (empty($key) && empty($value)) {
return $this;
}

if (empty($value)) {
if (empty($value) && !$setEmptyValue) {
return $this->get($key);
}

Expand Down
18 changes: 15 additions & 3 deletions app/Rules/ValidateUsername.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Rules;

use App\Models\User;
use Auth;
use Illuminate\Contracts\Validation\Rule;

class ValidateUsername implements Rule
Expand Down Expand Up @@ -47,9 +48,20 @@ public function passes($attribute, $value): bool
}

// Check if username taken
if (User::where('username', $value)->exists()) {
$this->errorType = 'exists';
return false;
$user = Auth::user();
if (!empty($user)) {
if (User::whereNotIn('id', [$user->id])
->where('username', $value)
->exists()
) {
$this->errorType = 'exists';
return false;
}
} else {
if (User::where('username', $value)->exists()) {
$this->errorType = 'exists';
return false;
}
}

return true;
Expand Down
2 changes: 1 addition & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
| the framework needs to place the application's version in a notification
| or any other location as required by the application or its packages.
*/
'version' => '1.2.0-alpha.1',
'version' => '1.2.0-alpha.2',

/*
|--------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<!-- Username -->
<div class="col-span-3 sm:col-span-2">
<x-label for="username" value="{{ __('Username') }}" />
<x-input id="username" type="text" class="mt-1 block w-full select-none opacity-25" wire:model.defer="state.username" autocomplete="username" disabled />
<x-input id="username" type="text" class="mt-1 block w-full {{ settings('can_change_username') ?: 'select-none opacity-25' }}" wire:model.defer="state.username" autocomplete="username" disabled="{{ !settings('can_change_username') }}" />
<x-input-error for="username" class="mt-2" />
</div>

Expand Down

0 comments on commit 64c778b

Please sign in to comment.