Make sure usernames cannot take the value of nicknames (#1)

This commit is contained in:
Sami Mazouz 2020-12-05 23:27:05 +01:00 committed by GitHub
parent 02298127b7
commit ffbb37e956
2 changed files with 10 additions and 10 deletions

View File

@ -21,7 +21,10 @@ class AddNicknameValidation
public function __invoke($flarumValidator, Validator $validator) public function __invoke($flarumValidator, Validator $validator)
{ {
$nicknameRules = [ $idSuffix = $flarumValidator->getUser() ? ','.$flarumValidator->getUser()->id : '';
$rules = $validator->getRules();
$rules['nickname'] = [
function ($attribute, $value, $fail) { function ($attribute, $value, $fail) {
$regex = $this->settings->get('flarum-nicknames.regex'); $regex = $this->settings->get('flarum-nicknames.regex');
if ($regex && !preg_match_all("/$regex/", $value)) { if ($regex && !preg_match_all("/$regex/", $value)) {
@ -34,12 +37,11 @@ class AddNicknameValidation
]; ];
if ($this->settings->get('flarum-nicknames.unique')) { if ($this->settings->get('flarum-nicknames.unique')) {
$nicknameRules[] = 'unique:users,username'; $rules['nickname'][] = 'unique:users,username'.$idSuffix;
$nicknameRules[] = 'unique:users,nickname'; $rules['nickname'][] = 'unique:users,nickname'.$idSuffix;
$rules['username'][] = 'unique:users,nickname'.$idSuffix;
} }
$validator->setRules([ $validator->setRules($rules);
'nickname' => $nicknameRules,
] + $validator->getRules());
} }
} }

View File

@ -24,10 +24,8 @@ class SaveNicknameToDatabase {
$nickname = $attributes['nickname']; $nickname = $attributes['nickname'];
// If unique validation is enabled, the nickname will be checked // If the user sets their nickname back to the username
// against ALL nicknames and usernames, including the username // set the nickname to null so that it just falls back to 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) { if ($user->username === $nickname) {
$user->nickname = null; $user->nickname = null;
} else { } else {