Skip to content

Commit d982ef1

Browse files
authored
allow username change (#1667)
1 parent 2dcc23e commit d982ef1

26 files changed

+108
-90
lines changed

app/Actions/Settings/UpdateLogin.php

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace App\Actions\Settings;
44

5+
use App\Exceptions\ConfigurationKeyMissingException;
56
use App\Exceptions\ConflictingPropertyException;
67
use App\Exceptions\Internal\QueryBuilderException;
78
use App\Exceptions\ModelDBException;
89
use App\Exceptions\UnauthenticatedException;
10+
use App\Models\Configs;
911
use App\Models\Logs;
1012
use App\Models\User;
1113
use Illuminate\Support\Facades\Auth;
@@ -34,24 +36,46 @@ public function do(?string $username, string $password, string $oldPassword, str
3436
$user = Auth::user() ?? throw new UnauthenticatedException();
3537

3638
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));
3840

3941
throw new UnauthenticatedException('Previous password is invalid');
4042
}
4143

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);
5048
}
5149

5250
$user->password = Hash::make($password);
5351
$user->save();
5452

5553
return $user;
5654
}
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+
}
5781
}

app/Http/Controllers/Administration/UserController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function updateLogin(ChangeLoginRequest $request, UpdateLogin $updateLogi
3232
$request->oldPassword(),
3333
$request->ip()
3434
);
35+
3536
// Update the session with the new credentials of the user.
3637
// Otherwise, the session is out-of-sync and falsely assumes the user
3738
// to be unauthenticated upon the next request.

app/Http/Requests/User/ChangeLoginRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function authorize(): bool
3333
public function rules(): array
3434
{
3535
return [
36-
RequestAttribute::USERNAME_ATTRIBUTE => ['sometimes', new UsernameRule()],
36+
RequestAttribute::USERNAME_ATTRIBUTE => ['sometimes', new UsernameRule(true)],
3737
RequestAttribute::PASSWORD_ATTRIBUTE => ['required', new PasswordRule(false)],
3838
RequestAttribute::OLD_PASSWORD_ATTRIBUTE => ['required', new PasswordRule(false)],
3939
];

app/Locale/ChineseSimplified.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,6 @@ public function get_locale(): array
291291
'ERROR' => '错误',
292292
'ERROR_TEXT' => '噢,似乎出了一些问题。请刷新页面后再试!',
293293
'ERROR_UNKNOWN' => '发生未知问题。请再试一次,检查您的安装和服务器。请查看自述文件以获取更多信息。',
294-
'ERROR_LOGIN' => '无法保存登录信息。请用另一个用户名和密码再试一次!',
295294
'ERROR_MAP_DEACTIVATED' => '地图功能已在设置中停用。',
296295
'ERROR_SEARCH_DEACTIVATED' => '搜索功能已在设置中停用。',
297296
'SUCCESS' => 'OK',
@@ -328,12 +327,9 @@ public function get_locale(): array
328327
'SETTINGS_SUCCESS_NEW_PHOTOS_NOTIFICATION' => 'New photos notification updated',
329328
'USER_EMAIL_INSTRUCTION' => 'Add your email below to enable receiving email notifications. To stop receiving emails, simply remove your email below.',
330329

331-
'LOGIN_TITLE' => '输入管理员用户名和密码:',
332330
'LOGIN_USERNAME' => '新用户名',
333331
'LOGIN_PASSWORD' => '新密码',
334332
'LOGIN_PASSWORD_CONFIRM' => '确认密码',
335-
'LOGIN_CREATE' => '创建',
336-
337333
'PASSWORD_TITLE' => '输入您当前的密码:',
338334
'PASSWORD_CURRENT' => '当前密码',
339335
'PASSWORD_TEXT' => '您的用户名和密码将被修改为:',

app/Locale/ChineseTraditional.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,6 @@ public function get_locale(): array
291291
'ERROR' => '錯誤',
292292
'ERROR_TEXT' => '噢,似乎出了一些問題。請重整頁面後再試一次!',
293293
'ERROR_UNKNOWN' => '發生未知問題!請再試一次,檢查您的安裝和伺服器。請查看自述文件以獲取更多信息。',
294-
'ERROR_LOGIN' => '無法保存登錄信息。請用另一個用戶名和密碼再試一次!',
295294
'ERROR_MAP_DEACTIVATED' => '地圖功能已被設為停用。',
296295
'ERROR_SEARCH_DEACTIVATED' => '搜索功能已在設為停用。',
297296
'SUCCESS' => '',
@@ -328,12 +327,9 @@ public function get_locale(): array
328327
'SETTINGS_SUCCESS_NEW_PHOTOS_NOTIFICATION' => 'New photos notification updated',
329328
'USER_EMAIL_INSTRUCTION' => 'Add your email below to enable receiving email notifications. To stop receiving emails, simply remove your email below.',
330329

331-
'LOGIN_TITLE' => '輸入管理員用戶名和密碼:',
332330
'LOGIN_USERNAME' => '新用戶名',
333331
'LOGIN_PASSWORD' => '新密碼',
334332
'LOGIN_PASSWORD_CONFIRM' => '確認密碼',
335-
'LOGIN_CREATE' => '創建',
336-
337333
'PASSWORD_TITLE' => '當前密碼',
338334
'PASSWORD_CURRENT' => '當前密碼',
339335
'PASSWORD_TEXT' => '用戶名和密碼將被修改為:',

app/Locale/Czech.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,6 @@ public function get_locale(): array
291291
'ERROR' => 'Chyba',
292292
'ERROR_TEXT' => 'Něco není v pořádku. Obnovte stránku a postup zopakujte!',
293293
'ERROR_UNKNOWN' => 'Neočekávaná chyba. Postup prosím opakujte a ujistěte se o správnosti instalace na serveru. Další informace jsou k dispozici v souboru README.',
294-
'ERROR_LOGIN' => 'Nelze uložit přihlašovací informace. Opakujte prosím postup s jiným uřivatelským jménem a heslem!',
295294
'ERROR_MAP_DEACTIVATED' => 'Funkce Mapy byla v nastavení deaktivována.',
296295
'ERROR_SEARCH_DEACTIVATED' => 'Funkce hledání byla v nastavení deaktivována.',
297296
'SUCCESS' => 'OK',
@@ -328,12 +327,9 @@ public function get_locale(): array
328327
'SETTINGS_SUCCESS_NEW_PHOTOS_NOTIFICATION' => 'New photos notification updated',
329328
'USER_EMAIL_INSTRUCTION' => 'Add your email below to enable receiving email notifications. To stop receiving emails, simply remove your email below.',
330329

331-
'LOGIN_TITLE' => 'Zadejte jméno uživatele a heslo pro svoji instalaci:',
332330
'LOGIN_USERNAME' => 'Jméno uživatele',
333331
'LOGIN_PASSWORD' => 'Heslo',
334332
'LOGIN_PASSWORD_CONFIRM' => 'Zopakujte heslo',
335-
'LOGIN_CREATE' => 'Vytvořit uživatele',
336-
337333
'PASSWORD_TITLE' => 'Zadejte aktuální heslo:',
338334
'PASSWORD_CURRENT' => 'Aktuální heslo',
339335
'PASSWORD_TEXT' => 'Vaše uživatelské jméno a heslo budou změněny následovně:',

app/Locale/Dutch.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,6 @@ public function get_locale(): array
291291
'ERROR' => 'Error',
292292
'ERROR_TEXT' => 'Whoops, er is iets misgegaan. Herlaad de pagina en probeer het opnieuw!',
293293
'ERROR_UNKNOWN' => 'Er is iets onverwachts gebeurd. Probeer het opnieuw of controleer je installatie en server. Kijk naar de readme voor meer informatie.',
294-
'ERROR_LOGIN' => 'Kan login niet opslaan. Probeer het opnieuw met een andere gebruikersnaam en/of wachtwoord!',
295294
'ERROR_MAP_DEACTIVATED' => 'Map functionality has been deactivated under settings.',
296295
'ERROR_SEARCH_DEACTIVATED' => 'Search functionality has been deactivated under settings.',
297296
'SUCCESS' => 'OK',
@@ -328,12 +327,9 @@ public function get_locale(): array
328327
'SETTINGS_SUCCESS_NEW_PHOTOS_NOTIFICATION' => 'New photos notification updated',
329328
'USER_EMAIL_INSTRUCTION' => 'Add your email below to enable receiving email notifications. To stop receiving emails, simply remove your email below.',
330329

331-
'LOGIN_TITLE' => 'Voer een gebruikersnaam en wachtwoord in voor je installatie:',
332330
'LOGIN_USERNAME' => 'Nieuw Gebruikersnaam',
333331
'LOGIN_PASSWORD' => 'Nieuw Wachtwoord',
334332
'LOGIN_PASSWORD_CONFIRM' => 'Confirm Password',
335-
'LOGIN_CREATE' => 'Maak Login',
336-
337333
'PASSWORD_TITLE' => 'Voer je huidige wachtwoord in:',
338334
'PASSWORD_CURRENT' => 'Huidig Wachtwoord',
339335
'PASSWORD_TEXT' => 'Je gebruikersnaam en wachtwoord worden verandert naar:',

app/Locale/English.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,6 @@ public function get_locale(): array
291291
'ERROR' => 'Error',
292292
'ERROR_TEXT' => 'Whoops, it looks like something went wrong. Please reload the site and try again!',
293293
'ERROR_UNKNOWN' => 'Something unexpected happened. Please try again and check your installation and server. Take a look at the readme for more information.',
294-
'ERROR_LOGIN' => 'Unable to save login. Please try again with another username and password!',
295294
'ERROR_MAP_DEACTIVATED' => 'Map functionality has been deactivated under settings.',
296295
'ERROR_SEARCH_DEACTIVATED' => 'Search functionality has been deactivated under settings.',
297296
'SUCCESS' => 'OK',
@@ -328,15 +327,12 @@ public function get_locale(): array
328327
'SETTINGS_SUCCESS_NEW_PHOTOS_NOTIFICATION' => 'New photos notification updated',
329328
'USER_EMAIL_INSTRUCTION' => 'Add your email below to enable receiving email notifications. To stop receiving emails, simply remove your email below.',
330329

331-
'LOGIN_TITLE' => 'Enter a username and password for your installation:',
332330
'LOGIN_USERNAME' => 'New Username',
333331
'LOGIN_PASSWORD' => 'New Password',
334332
'LOGIN_PASSWORD_CONFIRM' => 'Confirm Password',
335-
'LOGIN_CREATE' => 'Create Login',
336-
337333
'PASSWORD_TITLE' => 'Enter your current password:',
338334
'PASSWORD_CURRENT' => 'Current Password',
339-
'PASSWORD_TEXT' => 'Your username and password will be changed to the following:',
335+
'PASSWORD_TEXT' => 'Your credentials will be changed to the following:',
340336
'PASSWORD_CHANGE' => 'Change Login',
341337

342338
'EDIT_SHARING_TITLE' => 'Edit Sharing',

app/Locale/French.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,6 @@ public function get_locale(): array
291291
'ERROR' => 'Erreur',
292292
'ERROR_TEXT' => 'Il semble qu’une erreur soit survenue. Veuillez rafraichir la page et réessayer !',
293293
'ERROR_UNKNOWN' => 'Une erreur inattendue est survenue. Veuillez réessayer et vérifier votre installation et votre serveur. Consultez le fichier Readme pour obtenir plus d’information.',
294-
'ERROR_LOGIN' => 'Impossible d’enregistrer les informations de connexion. Veuillez réessayer avec un autre nom d’utilisateur et mot de passe.',
295294
'ERROR_MAP_DEACTIVATED' => 'La carte a été désactivée dans les paramètres.',
296295
'ERROR_SEARCH_DEACTIVATED' => 'La recherche a été désactivée dans les paramètres.',
297296
'SUCCESS' => 'OK',
@@ -328,15 +327,12 @@ public function get_locale(): array
328327
'SETTINGS_SUCCESS_NEW_PHOTOS_NOTIFICATION' => 'Notification de nouvelles photos mise à jour',
329328
'USER_EMAIL_INSTRUCTION' => 'Ajouter votre email Add your email below to enable receiving email notifications. To stop receiving emails, simply remove your email below.',
330329

331-
'LOGIN_TITLE' => 'Entrez un nom d’utilisateur et un mot de passe pour votre installation :',
332330
'LOGIN_USERNAME' => 'Nouvel utilisateur',
333331
'LOGIN_PASSWORD' => 'Nouveau Mot de passe',
334332
'LOGIN_PASSWORD_CONFIRM' => 'Confirmez le mot de passe',
335-
'LOGIN_CREATE' => 'Créer les informations de connexion',
336-
337333
'PASSWORD_TITLE' => 'Entrez votre mot de passe existant :',
338334
'PASSWORD_CURRENT' => 'Mot de passe existant :',
339-
'PASSWORD_TEXT' => 'Votre nom d’utilisateur et votre mot de passe seront modifiés comme suit :',
335+
'PASSWORD_TEXT' => 'Vos identifiants seront modifiés comme suit :',
340336
'PASSWORD_CHANGE' => 'Modifier les informations de connexion',
341337

342338
'EDIT_SHARING_TITLE' => 'Modifier le partage',

app/Locale/German.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,6 @@ public function get_locale(): array
291291
'ERROR' => 'Fehler',
292292
'ERROR_TEXT' => 'Hoppla, da ist etwas schiefgegangen. Bitte laden Sie die Seite erneut und probieren Sie es noch einmal!',
293293
'ERROR_UNKNOWN' => 'Etwas Unerwartetes ist passiert. Bitte probieren Sie es erneut und überprüfen Sie die Installation und Ihren Server. Lesen Sie die README-Datei für mehr Informationen.',
294-
'ERROR_LOGIN' => 'Kann Login nicht speichern. Bitte versuchen Sie es erneut mit einem anderen Benutzernamen und Kennwort!',
295294
'ERROR_MAP_DEACTIVATED' => 'Karten sind unter Einstellungen deaktiviert worden.',
296295
'ERROR_SEARCH_DEACTIVATED' => 'Suchfunktion wurde unter Einstellungen deaktiviert.',
297296
'SUCCESS' => 'OK',
@@ -328,12 +327,9 @@ public function get_locale(): array
328327
'SETTINGS_SUCCESS_NEW_PHOTOS_NOTIFICATION' => 'New photos notification updated',
329328
'USER_EMAIL_INSTRUCTION' => 'Geben Sie Ihre E-Mail-Adresse unten ein, um Benachrichtigungen zu aktivieren. Um Benachrichtigungen zu deaktivieren, entfernen Sie die E-Mail-Adresse unten einfach.',
330329

331-
'LOGIN_TITLE' => 'Geben Sie Benutzername und Kennwort für Ihre Installation an:',
332330
'LOGIN_USERNAME' => 'Neuer Benutzername',
333331
'LOGIN_PASSWORD' => 'Neues Kennwort',
334332
'LOGIN_PASSWORD_CONFIRM' => 'Passwort bestätigen',
335-
'LOGIN_CREATE' => 'Benutzer anlegen',
336-
337333
'PASSWORD_TITLE' => 'Geben Sie Ihr bestehendes Kennwort ein:',
338334
'PASSWORD_CURRENT' => 'Bestehendes Kennwort',
339335
'PASSWORD_TEXT' => 'Ihr Benutzername und Passwort werden wie folgt geändert:',

0 commit comments

Comments
 (0)