mirror of
https://github.com/flarum/core.git
synced 2025-08-07 08:56:38 +02:00
If unique nicknames are enabled, don't allow using someone else's username as a nickname
This commit is contained in:
@@ -21,20 +21,25 @@ class AddNicknameValidation
|
||||
|
||||
public function __invoke($flarumValidator, Validator $validator)
|
||||
{
|
||||
$unique_nickname = ($this->settings->get('flarum-nicknames.unique')) ? 'unique:users,nickname' : '';
|
||||
$nicknameRules = [
|
||||
function ($attribute, $value, $fail) {
|
||||
$regex = $this->settings->get('flarum-nicknames.regex');
|
||||
if ($regex && !preg_match_all("/$regex/", $value)) {
|
||||
$fail(app('translator')->trans('flarum-nicknames.api.invalid_nickname_message'));
|
||||
}
|
||||
},
|
||||
'min:' . $this->settings->get('flarum-nicknames.min'),
|
||||
'max:' . $this->settings->get('flarum-nicknames.max'),
|
||||
'nullable'
|
||||
];
|
||||
|
||||
if ($this->settings->get('flarum-nicknames.unique')) {
|
||||
$nicknameRules[] = 'unique:users,username';
|
||||
$nicknameRules[] = 'unique:users,nickname';
|
||||
}
|
||||
|
||||
$validator->setRules([
|
||||
'nickname' => [
|
||||
$unique_nickname,
|
||||
function ($attribute, $value, $fail) {
|
||||
$regex = $this->settings->get('flarum-nicknames.regex');
|
||||
if ($regex && !preg_match_all("/$regex/", $value)) {
|
||||
$fail(app('translator')->trans('flarum-nicknames.api.invalid_nickname_message'));
|
||||
}
|
||||
},
|
||||
'min:' . $this->settings->get('flarum-nicknames.min', 1),
|
||||
'max:' . $this->settings->get('flarum-nicknames.max', 150),
|
||||
],
|
||||
'nickname' => $nicknameRules,
|
||||
] + $validator->getRules());
|
||||
}
|
||||
}
|
||||
|
@@ -21,7 +21,18 @@ class SaveNicknameToDatabase {
|
||||
} else {
|
||||
$actor->assertCan('edit', $user);
|
||||
}
|
||||
$user->nickname = $attributes['nickname'];
|
||||
|
||||
$nickname = $attributes['nickname'];
|
||||
|
||||
// If unique validation is enabled, the nickname will be checked
|
||||
// against ALL nicknames and usernames, including the username
|
||||
// of the current user. So, to allow users to reset their nickname
|
||||
// back to their username, in this case we'd set it to null.
|
||||
if ($user->username === $nickname) {
|
||||
$user->nickname = null;
|
||||
} else {
|
||||
$user->nickname = $nickname;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user