/** @inheritdoc */ public function rules() { return [[['username', 'email', 'current_password'], 'required'], [['username', 'email'], 'filter', 'filter' => 'trim'], ['username', 'match', 'pattern' => '/^[a-zA-Z]\\w+$/'], ['username', 'string', 'min' => 3, 'max' => 20], ['email', 'email'], [['email', 'username'], 'unique', 'when' => function ($model, $attribute) { return $this->user->{$attribute} != $model->{$attribute}; }, 'targetClass' => $this->module->modelMap['User']], ['new_password', 'string', 'min' => 6], ['current_password', function ($attr) { if (!Password::validate($this->{$attr}, $this->user->password_hash)) { $this->addError($attr, \Yii::t('user', 'Current password is not valid')); } }]]; }
public function testRegister() { $this->model = new RegistrationForm(); $this->model->setAttributes(['email' => '*****@*****.**', 'username' => 'foobar', 'password' => 'foobar']); /** @var User $user */ verify($this->model->register())->true(); $user = User::findOne(['email' => '*****@*****.**']); verify('$user is instance of User', $user instanceof User)->true(); verify('email is valid', $user->email)->equals($this->model->email); verify('username is valid', $user->username)->equals($this->model->username); verify('password is valid', Password::validate($this->model->password, $user->password_hash))->true(); $token = Token::findOne(['user_id' => $user->id, 'type' => Token::TYPE_CONFIRMATION]); verify($token)->notNull(); $mock = $this->getMock(RegistrationForm::className(), ['validate']); $mock->expects($this->once())->method('validate')->will($this->returnValue(false)); verify($mock->register())->false(); }
/** @inheritdoc */ public function rules() { return [[['login', 'password'], 'required'], ['login', 'trim'], ['password', function ($attribute) { if ($this->user === null || !Password::validate($this->password, $this->user->password_hash)) { $this->addError($attribute, \Yii::t('user', 'Invalid login or password')); } }], ['login', function ($attribute) { if ($this->user !== null) { $confirmationRequired = $this->module->enableConfirmation && !$this->module->enableUnconfirmedLogin; if ($confirmationRequired && !$this->user->getIsConfirmed()) { $this->addError($attribute, \Yii::t('user', 'You need to confirm your email address')); } if ($this->user->getIsBlocked()) { $this->addError($attribute, \Yii::t('user', 'Your account has been blocked')); } } }], ['rememberMe', 'boolean']]; }
/** @inheritdoc */ public function beforeSave($insert) { if ($insert) { $this->setAttribute('auth_key', \Yii::$app->security->generateRandomString()); if (\Yii::$app instanceof \yii\web\Application) { $this->setAttribute('registration_ip', \Yii::$app->request->userIP); } } if (!empty($this->password)) { $this->setAttribute('password_hash', Password::hash($this->password)); } return parent::beforeSave($insert); }