Bladeren bron

提交备份

frank 3 jaren geleden
bovenliggende
commit
27122329cf
2 gewijzigde bestanden met toevoegingen van 39 en 10 verwijderingen
  1. 34 0
      backendApi/modules/v1/models/Admin.php
  2. 5 10
      backendApi/modules/v1/models/AdminForm.php

+ 34 - 0
backendApi/modules/v1/models/Admin.php

@@ -100,6 +100,40 @@ class Admin extends ActiveRecord
         return \Yii::$app->security->validatePassword($password, $this->PASSWORD_HASH);
     }
 
+    /**
+     * verificationPassword
+     *
+     * @param string $password password to validate
+     * @return bool if password provided is valid for current user
+     */
+    public function verificationPassword($password,$scenario,$model) {
+        // 判断密码长度不能少于8位
+        if (strlen($password) < 8){
+            return [ 'code' => 1000, 'type' => $scenario , 'message' => '登录密码不能小于8位' ];
+        }
+
+        // 判断密码中数字、大写字母、小写字母、特殊字符至少两种
+        $symbol = '`~!@#$^&*()=|{}\'\":;\',\\[\\].<>\/?~!@#¥……&*()——|{}【】‘;:”。,、?';
+        // $requiredRules = '/^(?![\d]+$)(?![a-z]+$)(?![A-Z]+$)(?!['.$symbol.']+$)[\da-zA-z'.$symbol.']{8,}$/';   // 这是两种的正则
+        $requiredRules = '/^(?![\da-z]+$)(?![\dA-Z]+$)(?![\d'.$symbol.']+$)(?![a-zA-Z]+$)(?![a-z'.$symbol.']+$)(?![A-Z'.$symbol.']+$)[\da-zA-z'.$symbol.']{8,}$/';
+        $requiredHandleResults = preg_match($requiredRules,$password);
+        if (!$requiredHandleResults){
+            return [ 'code' => 1000, 'type' => $scenario , 'message' => '登录密码中需要包含数字、大写字母、小写字母、特殊字符至少三种' ];
+        }
+
+        // 判断有三位或以上重复性
+        if(preg_match('/(\w)*(\w)\2{2}(\w)*/',$password)) {
+            return [ 'code' => 1000, 'type' => $scenario , 'message' => '登录密码中数字、大写字母、小写字母不能连续重复三个或以上' ];
+        }
+
+        // 判断密码中不能出现登录账号
+        if (strpos($password, $model->ADMIN_NAME) !== false){
+            return [ 'code' => 1000, 'type' => $scenario , 'message' => '登录密码中不能出现登录账号' ];
+        }
+
+        return [ 'code' => 200, 'type' => $scenario , 'message' => '验证通过了' ];
+    }
+
     /**
      * 根据用户名查找
      * @param $username

+ 5 - 10
backendApi/modules/v1/models/AdminForm.php

@@ -134,19 +134,14 @@ class AdminForm extends Model
         // 给密码进行加密 ,如果需要添加验证密码安全验证时 ,可以在这个地方加
         if($this->password){
             if ($this->scenario == 'changePassword' || $this->scenario == 'noLoginModifyPassword'){
-                if (strlen($this->password) < 8){
-                    $this->addError($this->scenario, '登录密码不能小于8位');
+
+                $message = $model->verificationPassword($this->password,$this->scenario,$model);
+                if ( $message['code'] != 200 ){
+                    $this->addError($message['type'], $message['message']);
                     return null;
                 }
-//               $symbol = '!#$%^&*';
-               $symbol = '`~!@#$^&*()=|{}\'\":;\',\\[\\].<>\/?~!@#¥……&*()——|{}【】‘;:”。,、?';
-               $passwordRules = '/^(?![\d]+$)(?![a-z]+$)(?![A-Z]+$)(?!['.$symbol.']+$)[\da-zA-z'.$symbol.']{8,}$/';
-               $verificationResults = preg_match($passwordRules,$this->password);
-               if (!$verificationResults){
-                   $this->addError($this->scenario, '登录密码中需要包含数字、大写字母、小写字母、特殊字符至少两种');
-                   return null;
-               }
             }
+
             $model->PASSWORD_HASH = \Yii::$app->security->generatePasswordHash($this->password);
         }