From d6a179978135b2631b5809fad3d926cb3a926323 Mon Sep 17 00:00:00 2001 From: kiritokatklian Date: Mon, 19 Apr 2021 02:40:33 +0200 Subject: [PATCH] [Update] Username Settings - Users can edit their username if the can_change_username setting is enabled --- .../Web/UpdateUserProfileInformation.php | 25 ++++++++++++++++--- app/Rules/ValidateUsername.php | 18 ++++++++++--- .../update-profile-information-form.blade.php | 2 +- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/app/Actions/Web/UpdateUserProfileInformation.php b/app/Actions/Web/UpdateUserProfileInformation.php index 1ef42c0d8..facbf1664 100644 --- a/app/Actions/Web/UpdateUserProfileInformation.php +++ b/app/Actions/Web/UpdateUserProfileInformation.php @@ -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; @@ -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()); @@ -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); + } + } } /** diff --git a/app/Rules/ValidateUsername.php b/app/Rules/ValidateUsername.php index 2fb5abf46..60bbb7cb4 100644 --- a/app/Rules/ValidateUsername.php +++ b/app/Rules/ValidateUsername.php @@ -3,6 +3,7 @@ namespace App\Rules; use App\Models\User; +use Auth; use Illuminate\Contracts\Validation\Rule; class ValidateUsername implements Rule @@ -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; diff --git a/resources/views/livewire/profile/update-profile-information-form.blade.php b/resources/views/livewire/profile/update-profile-information-form.blade.php index 4e3fa6481..2ccef4b73 100644 --- a/resources/views/livewire/profile/update-profile-information-form.blade.php +++ b/resources/views/livewire/profile/update-profile-information-form.blade.php @@ -53,7 +53,7 @@
- +