예제 #1
0
 /** @inheritdoc */
 public function rules()
 {
     return ['usernameRequired' => ['username', 'required'], 'usernameTrim' => ['username', 'filter', 'filter' => 'trim'], 'usernameLength' => ['username', 'string', 'min' => 3, 'max' => 255], 'usernamePattern' => ['username', 'match', 'pattern' => '/^[-a-zA-Z0-9_\\.@]+$/'], 'emailRequired' => ['email', 'required'], 'emailTrim' => ['email', 'filter', 'filter' => 'trim'], 'emailPattern' => ['email', 'email'], 'emailUsernameUnique' => [['email', 'username'], 'unique', 'when' => function ($model, $attribute) {
         return $this->user->{$attribute} != $model->{$attribute};
     }, 'targetClass' => $this->module->modelMap['User']], 'newPasswordLength' => ['new_password', 'string', 'min' => 6], 'currentPasswordRequired' => ['current_password', 'required'], 'currentPasswordValidate' => ['current_password', function ($attr) {
         if (!Password::validate($this->{$attr}, $this->user->passwordHash)) {
             $this->addError($attr, Yii::t('users', 'Current password is not valid'));
         }
     }]];
 }
예제 #2
0
 /** @inheritdoc */
 public function rules()
 {
     return ['requiredFields' => [['login', 'password'], 'required'], 'loginTrim' => ['login', 'trim'], 'passwordValidate' => ['password', function ($attribute) {
         if ($this->user === null || !Password::validate($this->password, $this->user->passwordHash)) {
             $this->addError($attribute, Yii::t('users', 'Invalid login or password'));
         }
     }], 'confirmationValidate' => ['login', function ($attribute) {
         if ($this->user !== null) {
             $confirmationRequired = $this->module->enableConfirmation && !$this->module->enableUnconfirmedLogin;
             if ($confirmationRequired && !$this->user->getIsConfirmed()) {
                 $this->addError($attribute, Yii::t('users', 'You need to confirm your email address'));
             }
             if ($this->user->getIsBlocked()) {
                 $this->addError($attribute, Yii::t('users', 'Your account has been blocked'));
             }
         }
     }], 'rememberMe' => ['rememberMe', 'boolean']];
 }
예제 #3
0
파일: User.php 프로젝트: yujin1st/yii2-user
 /** @inheritdoc */
 public function beforeSave($insert)
 {
     if ($insert) {
         $this->setAttribute('authKey', Yii::$app->security->generateRandomString());
         if (Yii::$app instanceof WebApplication) {
             $this->setAttribute('registrationIp', Yii::$app->request->userIP);
         }
     }
     if (!empty($this->password)) {
         $this->setAttribute('passwordHash', Password::hash($this->password));
     }
     return parent::beforeSave($insert);
 }