Prechádzať zdrojové kódy

Update User model for username_normal

And fix User\Save.
Visman 3 rokov pred
rodič
commit
e39a350e8f

+ 1 - 1
app/Models/User/IsUniqueName.php

@@ -23,7 +23,7 @@ class IsUniqueName extends Action
         $vars = [
             ':id'    => (int) $user->id,
             ':name'  => $user->username,
-            ':norm'  => $this->manager->normUsername($user->username),
+            ':norm'  => $user->username_normal,
             ':normL' => $this->manager->normUsername(\mb_strtolower($user->username, 'UTF-8')), // ????
             ':normU' => $this->manager->normUsername(\mb_strtoupper($user->username, 'UTF-8')), // ????
         ];

+ 4 - 10
app/Models/User/Save.php

@@ -60,15 +60,6 @@ class Save extends Action
 
             if ('username' === $name) {
                 $nameChange = true;
-
-                // пересчет username_normal при изменении username
-                $name = 'username_normal';
-
-                if (isset($fileds[$name])) {
-                    $vars[] = $this->manager->normUsername($user->username);
-                    $set[]  = $name . '=?' . $fileds[$name];
-                }
-                // пересчет username_normal при изменении username
             } elseif ('group_id' === $name) {
                 $grChange = true;
             }
@@ -114,7 +105,10 @@ class Save extends Action
     {
         if (null !== $user->id) {
             throw new RuntimeException('The model has ID');
-        } elseif ($user->isGuest) {
+        } elseif (
+            null === $user->group_id
+            || FORK_GROUP_GUEST === $user->group_id
+        ) {
             throw new RuntimeException('Unexpected guest');
         }
 

+ 14 - 3
app/Models/User/User.php

@@ -38,6 +38,7 @@ class User extends DataModel
             'show_avatars' => ['showAvatar'],
             'signature'    => ['isSignature'],
             'email'        => ['email_normal'],
+            'username'     => ['username_normal'],
             'g_pm'         => ['usePM'],
         ];
     }
@@ -366,20 +367,30 @@ class User extends DataModel
     }
 
     /**
-     * Вычисление нормализованного email
+     * Вычисляет нормализованный email
      */
     protected function getemail_normal(): string
     {
         return $this->c->NormEmail->normalize($this->email);
     }
 
+    /**
+     * Вычисляет нормализованный username
+     */
+    protected function getusername_normal(): string
+    {
+        return $this->c->users->normUsername($this->username);
+    }
+
     /**
      * Возвращает значения свойств в массиве
      */
     public function getAttrs(): array
     {
-        if (isset($this->zModFlags['email_normal'])) {
-            $this->setAttr('email_normal', $this->email_normal);
+        foreach (['email_normal', 'username_normal'] as $key) {
+            if (isset($this->zModFlags[$key])) {
+                $this->setAttr($key, $this->$key);
+            }
         }
 
         return parent::getAttrs();