AdminUserForm.php 4.8 KB
Newer Older
T
Terry 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
<?php
/**
 * FecShop file.
 *
 * @link http://www.fecshop.com/
 * @copyright Copyright (c) 2016 FecShop Software LLC
 * @license http://www.fecshop.com/license/
 */
namespace fecshop\models\mysqldb\AdminUser;
use fecshop\models\mysqldb\AdminUser;
/**
 * @author Terry Zhao <2358269014@qq.com>
 * @since 1.0
 */
class AdminUserForm extends AdminUser {
    private $_admin_user;
    public function rules()
    {
        $parent_rules  = parent::rules();
        $current_rules = [
            ['username', 'filter', 'filter' => 'trim'],
            ['username', 'required'],
            ['username', 'validateUsername'],
            ['username', 'string', 'min' => 2, 'max' => 20],
            ['email', 'filter', 'filter' => 'trim'],
            ['code', 'required'],
            ['code', 'filter', 'filter' => 'trim'],
            ['code', 'validateCode'],
            ['person', 'filter', 'filter' => 'trim'],
T
Terry 已提交
30
            //['password', 'validatePasswordFormat'],
T
Terry 已提交
31 32 33 34 35 36
        ];

        return array_merge($parent_rules,$current_rules) ;
    }

    public function validateUsername($attribute, $params){
T
Terry 已提交
37
        
T
Terry 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50
        if($this->id){
            $one = AdminUser::find()->where(" id != ".$this->id." AND username = '".$this->username."' ")
                ->one();
            if($one['id']){
                $this->addError($attribute,"this username is exist!");
            }
        }else{
            $one = AdminUser::find()->where(" username = '".$this->username."' ")
                ->one();
            if($one['id']){
                $this->addError($attribute,"this username is exist!");
            }
        }
T
Terry 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64
        // password
        if($this->id){
            if($this->password && strlen($this->password) <= 6){
                $this->addError($attribute,"password must >=6");
            }
        }else{
            if(!$this->password){
                $this->addError($attribute,"password can not empty");
            } else if (strlen($this->password) < 6) {
                $this->addError($attribute,"password must >=6");
            } else if (!strlen($this->password) >= 100) {
                $this->addError($attribute,"password must <= 100");
            }
        }
T
Terry 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    }

    public function validateCode($attribute, $params){
        if($this->id){
            $one = AdminUser::find()->where(" id != ".$this->id." AND code = '".$this->code."' ")
                ->one();
            if($one['id']){
                $this->addError($attribute,"this code is exist!");
            }
        }else{
            $one = AdminUser::find()->where(" code = '".$this->code."' ")
                ->one();
            if($one['id']){
                $this->addError($attribute,"this code is exist!");
            }
        }
    }

    public function validateEmail($attribute, $params){
        if($this->id){
            $one = AdminUser::find()->where(" id != ".$this->id." AND email = '".$this->email."' ")
                ->one();
            if($one['id']){
                $this->addError($attribute,"this email is exist!");
            }
        }else{
            $one = AdminUser::find()->where(" email = '".$this->email."' ")
                ->one();
            if($one['id']){
                $this->addError($attribute,"this email is exist!");
            }
        }
    }
T
Terry 已提交
98
    /*
T
Terry 已提交
99 100 101 102 103 104
    public function validatePasswordFormat($attribute, $params){
        if($this->id){
            if($this->password && strlen($this->password) <= 6){
                $this->addError($attribute,"password must >=6");
            }
        }else{
T
Terry 已提交
105 106 107
            if(!$this->password){
                $this->addError($attribute,"password can not empty");
            } else if (strlen($this->password) < 6) {
T
Terry 已提交
108
                $this->addError($attribute,"password must >=6");
T
Terry 已提交
109 110
            } else if (!strlen($this->password) >= 100) {
                $this->addError($attribute,"password must <= 100");
T
Terry 已提交
111 112 113
            }
        }
    }
T
Terry 已提交
114
    */
T
Terry 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

    public function setPassword($password)
    {
        if($this->password){
            $this->password_hash = \Yii::$app->security->generatePasswordHash($password);
            $this->password = '';
        }
    }
    # 重写保存方法
    public function save($runValidation = true, $attributeNames = NULL){

        if($this->id){
            $this->updated_at_datetime = date("Y-m-d H:i:s");
        }else{
            $this->created_at_datetime = date("Y-m-d H:i:s");
            $this->updated_at_datetime = date("Y-m-d H:i:s");
        }
        # 如果auth_key为空,则重置
        if(!$this->auth_key){
            $this->generateAuthKey();
        }
        # 如果access_token为空,则重置
        if(!$this->access_token){
            $this->generateAccessToken();
        }
        # 设置password
        $this->setPassword($this->password);
        parent::save($runValidation,$attributeNames);
    }
}