Browse Source

Add more settings for username to engine config

Visman 2 years ago
parent
commit
e7676f6cfd

+ 17 - 1
app/Models/Pages/Admin/Update.php

@@ -25,7 +25,7 @@ class Update extends Admin
 {
     const PHP_MIN                    = '8.0.0';
     const REV_MIN_FOR_UPDATE         = 53;
-    const LATEST_REV_WITH_DB_CHANGES = 59;
+    const LATEST_REV_WITH_DB_CHANGES = 62;
     const LOCK_NAME                  = 'lock_update';
     const LOCK_TTL                   = 1800;
     const CONFIG_FILE                = 'main.php';
@@ -744,6 +744,22 @@ class Update extends Admin
         $this->c->DB->dropIndex('::search_matches', 'word_id_idx');
         $this->c->DB->addIndex('::search_matches', 'multi_idx', ['word_id', 'post_id']);
 
+        $coreConfig = new CoreConfig($this->configFile);
+
+        $coreConfig->add(
+            'USERNAME',
+            [
+                'phpPattern' => '\'%^\\p{L}[\\p{L}\\p{N}\\x20\\._-]+$%uD\'',
+                'jsPattern'  => '\'^.{2,}$\'',
+                'min'        => '2',
+                'max'        => '25',
+            ],
+            'FLOOD_INTERVAL'
+        );
+        $coreConfig->delete('USERNAME_PATTERN');
+
+        $coreConfig->save();
+
         return null;
     }
 }

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

@@ -96,12 +96,12 @@ class NewUser extends Users
                         'username' => [
                             'autofocus' => true,
                             'type'      => 'text',
-                            'maxlength' => '25',
+                            'maxlength' => $this->c->USERNAME['max'],
                             'value'     => $data['username'] ?? null,
                             'caption'   => 'Username',
                             'help'      => 'Login format',
                             'required'  => true,
-                            'pattern'   => '^.{2,25}$',
+                            'pattern'   => $this->c->USERNAME['jsPattern'],
                         ],
                         'email' => [
                             'type'           => 'text',

+ 2 - 2
app/Models/Pages/PostFormTrait.php

@@ -51,10 +51,10 @@ trait PostFormTrait
             $fieldset['username'] = [
                 'class'     => ['w1'],
                 'type'      => 'text',
-                'maxlength' => '25',
+                'maxlength' => $this->c->USERNAME['max'],
                 'caption'   => 'Username',
                 'required'  => true,
-                'pattern'   => '^.{2,25}$',
+                'pattern'   => $this->c->USERNAME['jsPattern'],
                 'value'     => $vars['username'] ?? null,
                 'autofocus' => $autofocus,
             ];

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

@@ -242,10 +242,10 @@ class Edit extends Profile
         if ($this->rules->rename) {
             $fields['username'] = [
                 'type'      => 'text',
-                'maxlength' => '25',
+                'maxlength' => $this->c->USERNAME['max'],
                 'caption'   => 'Username',
                 'required'  => true,
-                'pattern'   => '^.{2,25}$',
+                'pattern'   => $this->c->USERNAME['jsPattern'],
                 'value'     => $this->curUser->username,
             ];
         } else {

+ 2 - 2
app/Models/Pages/Register.php

@@ -187,12 +187,12 @@ class Register extends Page
         $fields['username'] = [
             'class'     => ['hint'],
             'type'      => 'text',
-            'maxlength' => '25',
+            'maxlength' => $this->c->USERNAME['max'],
             'value'     => $v->username ?? ($this->useOAuth ? $this->nameGenerator($this->provider) : ''),
             'caption'   => 'Username',
             'help'      => 'Login format',
             'required'  => true,
-            'pattern'   => '^.{2,25}$',
+            'pattern'   => $this->c->USERNAME['jsPattern'],
         ];
 
         if (! $this->useOAuth) {

+ 9 - 1
app/Models/Validators/Username.php

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

+ 6 - 1
app/config/main.dist.php

@@ -47,7 +47,12 @@ return [
     'MAX_FILE_SIZE'    => '2M',
     'MAX_EMAIL_LENGTH' => 80,
     'FLOOD_INTERVAL'   => 3600,
-    'USERNAME_PATTERN' => '%^(?=.{2,25}$)\p{L}[\p{L}\p{N}\x20\._-]+$%uD',
+    'USERNAME'         => [
+        'phpPattern' => '%^\p{L}[\p{L}\p{N}\x20\._-]+$%uD',
+        'jsPattern'  => '^.{2,}$',
+        'min'        => 2,
+        'max'        => 25,
+    ],
     'HTTP_HEADERS'     => [
         'common' => [
             'X-Content-Type-Options'  => 'nosniff',

+ 6 - 0
app/lang/en/validator.po

@@ -206,3 +206,9 @@ msgstr "The :alias field contains an invalid email address."
 
 msgid "The %s extension is not allowed"
 msgstr "The .%s extension is not allowed."
+
+msgid "Short username"
+msgstr "The username is too short."
+
+msgid "Long username"
+msgstr "The username is too long."

+ 6 - 0
app/lang/ru/validator.po

@@ -206,3 +206,9 @@ msgstr "Поле :alias содержит неверный электронный
 
 msgid "The %s extension is not allowed"
 msgstr "Расширение .%s запрещено."
+
+msgid "Short username"
+msgstr "Слишком короткое имя пользователя."
+
+msgid "Long username"
+msgstr "Слишком длинное имя пользователя."