Allow admins to create/rename users with custom names

The maximum name length is 190 characters.
Characters with codes from 0 to 31 and @, ', ", \, /, <, > are prohibited.
This commit is contained in:
Visman 2023-07-16 20:00:38 +07:00
parent e7676f6cfd
commit 5c5e5385f4
3 changed files with 16 additions and 5 deletions

View file

@ -96,7 +96,7 @@ class NewUser extends Users
'username' => [
'autofocus' => true,
'type' => 'text',
'maxlength' => $this->c->USERNAME['max'],
'maxlength' => $this->user->isAdmin ? '190' : $this->c->USERNAME['max'],
'value' => $data['username'] ?? null,
'caption' => 'Username',
'help' => 'Login format',

View file

@ -242,7 +242,7 @@ class Edit extends Profile
if ($this->rules->rename) {
$fields['username'] = [
'type' => 'text',
'maxlength' => $this->c->USERNAME['max'],
'maxlength' => $this->user->isAdmin ? '190' : $this->c->USERNAME['max'],
'caption' => 'Username',
'required' => true,
'pattern' => $this->c->USERNAME['jsPattern'],

View file

@ -34,14 +34,25 @@ class Username extends RulesValidator
$user = $this->c->users->create(['id' => $id, 'username' => $username]);
$len = \mb_strlen($username, 'UTF-8');
if ($this->c->user->isAdmin) {
$max = 190;
$pattern = '%^[^@\'"<>\\/\x00-\x1F]+$%D';
} else {
$max = $this->c->USERNAME['max'];
$pattern = $this->c->USERNAME['phpPattern'];
}
// короткое
if ($len < $this->c->USERNAME['min']) {
if ($len < \max(2, $this->c->USERNAME['min'])) {
$v->addError('Short username');
// длинное
} elseif ($len > $this->c->USERNAME['max']) {
} elseif ($len > \min(190, $max)) {
$v->addError('Long username');
// паттерн не совпал
} elseif (! \preg_match($this->c->USERNAME['phpPattern'], $username)) {
} elseif (
! \preg_match($pattern, $username)
|| \preg_match('%[@\'"<>\\/\x00-\x1F]%', $username)
) {
$v->addError('Login format');
// идущие подряд пробелы
} elseif (\preg_match('%\s{2,}%u', $username)) {