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

View File

@ -24,10 +24,8 @@ class SaveNicknameToDatabase {
$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 the user sets their nickname back to the username
// set the nickname to null so that it just falls back to the username
if ($user->username === $nickname) {
$user->nickname = null;
} else {