From b118421b727ab79778e701d947cf0b25f27078b6 Mon Sep 17 00:00:00 2001 From: kiritokatklian Date: Mon, 19 Apr 2021 02:40:27 +0200 Subject: [PATCH 1/3] [Fix] Settings Helper - Added setEmptyValue parameter for cases when the value is detected as empty even if it's intentional --- app/Helpers/CustomHelpers.php | 5 +++-- app/Helpers/Settings.php | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/app/Helpers/CustomHelpers.php b/app/Helpers/CustomHelpers.php index 78c485ba3..ea4676ba0 100644 --- a/app/Helpers/CustomHelpers.php +++ b/app/Helpers/CustomHelpers.php @@ -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); } diff --git a/app/Helpers/Settings.php b/app/Helpers/Settings.php index 4782c76e8..5cac68248 100644 --- a/app/Helpers/Settings.php +++ b/app/Helpers/Settings.php @@ -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); } From d6a179978135b2631b5809fad3d926cb3a926323 Mon Sep 17 00:00:00 2001 From: kiritokatklian Date: Mon, 19 Apr 2021 02:40:33 +0200 Subject: [PATCH 2/3] [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 @@
- +
From 24690e5e6cd3e428dbce918d0f225322f3df7238 Mon Sep 17 00:00:00 2001 From: kiritokatklian Date: Mon, 19 Apr 2021 02:40:33 +0200 Subject: [PATCH 3/3] [Update] Username Settings - Users can edit their username if the can_change_username setting is enabled - Bumped version --- .../Web/UpdateUserProfileInformation.php | 25 ++++++++++++++++--- app/Rules/ValidateUsername.php | 18 ++++++++++--- config/app.php | 2 +- .../update-profile-information-form.blade.php | 2 +- 4 files changed, 39 insertions(+), 8 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/config/app.php b/config/app.php index 6b9e082d4..f92a60acf 100644 --- a/config/app.php +++ b/config/app.php @@ -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', /* |-------------------------------------------------------------------------- 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 @@
- +