Browse Source

Update Ban\IsBanned

Now the isBanned() method will return an exception if email is empty.
The result returns the type of ban: solo ban for email or banned the entire domain.
Visman 4 years ago
parent
commit
8b12b5750e
1 changed files with 40 additions and 32 deletions
  1. 40 32
      app/Models/BanList/IsBanned.php

+ 40 - 32
app/Models/BanList/IsBanned.php

@@ -12,54 +12,62 @@ namespace ForkBB\Models\BanList;
 
 use ForkBB\Models\Method;
 use ForkBB\Models\User\Model as User;
+use InvalidArgumentException;
 
 class IsBanned extends Method
 {
     /**
      * Проверяет наличие бана пользователя на основании email
+     *
+     * результат: 0 - для этого email нет бана
+     *            1 - забанен именно этот email
+     *            2 - забанен домен
      */
     public function isBanned(User $user): int
     {
-        if (
-            $user->isGuest
-            && ! empty($this->model->emailList)
-            && $user->email_normal
-        ) {
-            $email = $this->model->trimToNull($user->email_normal);
-            $stage = 0;
+        if (empty($this->model->emailList)) {
+            return 0;
+        }
 
-            do {
-                if (isset($this->model->emailList[$email])) {
-                    return $this->model->emailList[$email];
-                }
+        $email = $this->model->trimToNull($user->email_normal);
 
-                switch ($stage) {                               // "super@user"@example.com
-                    case 0:
-                        $pos = \strrpos($email, '@');
+        if (null === $email) {
+            throw new InvalidArgumentException('Expected email, not empty string');
+        }
 
-                        if (false !== $pos) {
-                            $email = \substr($email, $pos + 1); // -> example.com
-                            break;
-                        }
+        $stage = 0;
 
-                        ++$stage;
-                    case 1:
-                        $email = '.' . $email;                  // -> .example.com
-                        $pos = true;
-                        break;
-                    default:
-                        $pos = \strpos($email, '.', 1);
+        do {
+            if (isset($this->model->emailList[$email])) {
+                return false === \strpos($email, '@') ? 2 : 1;
+            }
 
-                        if (false !== $pos) {
-                            $email = \substr($email, $pos);     // -> .com
-                        }
+            switch ($stage) {                               // "super@user"@example.com
+                case 0:
+                    $pos = \strrpos($email, '@');
 
+                    if (false !== $pos) {
+                        $email = \substr($email, $pos + 1); // -> example.com
                         break;
-                }
+                    }
 
-                ++$stage;
-            } while (false !== $pos);
-        }
+                    ++$stage;
+                case 1:
+                    $email = '.' . $email;                  // -> .example.com
+                    $pos = true;
+                    break;
+                default:
+                    $pos = \strpos($email, '.', 1);
+
+                    if (false !== $pos) {
+                        $email = \substr($email, $pos);     // -> .com
+                    }
+
+                    break;
+            }
+
+            ++$stage;
+        } while (false !== $pos);
 
         return 0;
     }