|
2 | 2 |
|
3 | 3 | namespace App\Actions\Settings; |
4 | 4 |
|
| 5 | +use App\Exceptions\ConfigurationKeyMissingException; |
5 | 6 | use App\Exceptions\ConflictingPropertyException; |
6 | 7 | use App\Exceptions\Internal\QueryBuilderException; |
7 | 8 | use App\Exceptions\ModelDBException; |
8 | 9 | use App\Exceptions\UnauthenticatedException; |
| 10 | +use App\Models\Configs; |
9 | 11 | use App\Models\Logs; |
10 | 12 | use App\Models\User; |
11 | 13 | use Illuminate\Support\Facades\Auth; |
@@ -34,24 +36,46 @@ public function do(?string $username, string $password, string $oldPassword, str |
34 | 36 | $user = Auth::user() ?? throw new UnauthenticatedException(); |
35 | 37 |
|
36 | 38 | if (!Hash::check($oldPassword, $user->password)) { |
37 | | - Logs::notice(__METHOD__, __LINE__, 'User (' . $user->username . ') tried to change their identity from ' . $ip); |
| 39 | + Logs::notice(__METHOD__, __LINE__, sprintf('User (%s) tried to change their identity from %s', $user->username, $ip)); |
38 | 40 |
|
39 | 41 | throw new UnauthenticatedException('Previous password is invalid'); |
40 | 42 | } |
41 | 43 |
|
42 | | - if (User::query()->where('username', '=', $username)->where('id', '!=', $user->id)->count() !== 0) { |
43 | | - Logs::notice(__METHOD__, __LINE__, 'User (' . $user->username . ') tried to change their identity to ' . $username . ' from ' . $ip); |
44 | | - throw new ConflictingPropertyException('Username already exists.'); |
45 | | - } |
46 | | - |
47 | | - if ($username !== null && $username !== $user->username) { |
48 | | - Logs::notice(__METHOD__, __LINE__, 'User (' . $user->username . ') changed their identity for (' . $username . ') from ' . $ip); |
49 | | - $user->username = $username; |
| 44 | + if ($username !== null |
| 45 | + && $username !== '' |
| 46 | + && Configs::getValueAsBool('allow_username_change')) { |
| 47 | + $this->updateUsername($user, $username, $ip); |
50 | 48 | } |
51 | 49 |
|
52 | 50 | $user->password = Hash::make($password); |
53 | 51 | $user->save(); |
54 | 52 |
|
55 | 53 | return $user; |
56 | 54 | } |
| 55 | + |
| 56 | + /** |
| 57 | + * Update Username if it does not already exists. |
| 58 | + * |
| 59 | + * @param User $user |
| 60 | + * @param string $username |
| 61 | + * @param string $ip |
| 62 | + * |
| 63 | + * @return void |
| 64 | + * |
| 65 | + * @throws ConfigurationKeyMissingException |
| 66 | + * @throws QueryBuilderException |
| 67 | + * @throws ConflictingPropertyException |
| 68 | + */ |
| 69 | + private function updateUsername(User &$user, string $username, string $ip): void |
| 70 | + { |
| 71 | + if (User::query()->where('username', '=', $username)->where('id', '!=', $user->id)->count() !== 0) { |
| 72 | + Logs::notice(__METHOD__, __LINE__, sprintf('User (%s) tried to change their identity to (%s) from %s', $user->username, $username, $ip)); |
| 73 | + throw new ConflictingPropertyException('Username already exists.'); |
| 74 | + } |
| 75 | + |
| 76 | + if ($username !== $user->username) { |
| 77 | + Logs::notice(__METHOD__, __LINE__, sprintf('User (%s) changed their identity for (%s) from %s', $user->username, $username, $ip)); |
| 78 | + $user->username = $username; |
| 79 | + } |
| 80 | + } |
57 | 81 | } |
0 commit comments