浏览代码

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.
Visman 2 年之前
父节点
当前提交
5c5e5385f4
共有 3 个文件被更改,包括 16 次插入5 次删除
  1. 1 1
      app/Models/Pages/Admin/Users/NewUser.php
  2. 1 1
      app/Models/Pages/Profile/Edit.php
  3. 14 3
      app/Models/Validators/Username.php

+ 1 - 1
app/Models/Pages/Admin/Users/NewUser.php

@@ -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',

+ 1 - 1
app/Models/Pages/Profile/Edit.php

@@ -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'],

+ 14 - 3
app/Models/Validators/Username.php

@@ -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)) {