/** * Change user's forgotten password. * * @param string $hash * @return mixed * @throws NotFoundHttpException */ public function actionChangePassword($hash) { // find a user $user = $this->userModule->findUserByChecker('email_checker', $hash); if (!$user instanceof User) { // user not found throw new NotFoundHttpException(); } $model = new ChangePasswordForm(); $ret = $this->performAjaxValidation($model); if (is_array($ret)) { // AJAX validation return $ret; } if ($model->load(Yii::$app->request->post()) && $model->validate()) { /* @var $systemAlert Alert */ $systemAlert = Yii::$app->systemAlert; if ($this->userModule->changeUserForgottenPassword($model, $user)) { if ($user->canSignIn()) { // authorize user $this->userModule->signInUser($user, $model->password); } $systemAlert->setMessage(Alert::INFO, Yii::t('user', 'Password successfully changed.')); return $this->goHome(); } else { $systemAlert->setMessage(Alert::DANGER, Yii::t('user', 'Password change error')); } } return $this->render('change-password', ['model' => $model]); }
/** * Tests user create form */ public function testCreateUser() { $user = new UserForm(); $user->setScenario('create'); // create user and check every error $this->assertFalse($user->validate(), 'Check error validation'); $this->assertArrayHasKey('roles', $user->getErrors(), 'Check has roles error'); $user->roles[] = 'admin'; $this->assertFalse($user->validate()); $this->assertArrayHasKey('name', $user->getErrors(), 'Check has name error'); $user->name = 'Tester'; $this->assertFalse($user->validate()); $this->assertArrayHasKey('email', $user->getErrors(), 'Check has email error'); $user->email = 'wrong email format'; $this->assertArrayHasKey('email', $user->getErrors(), 'Check has wrong e-mail format'); $user->email = '*****@*****.**'; $user->sendNotification = true; $this->assertTrue($user->validate(), 'Check every field is validated'); // create user model $result = $this->userModule->createUser($user); $this->assertNotEmpty($user->id); $this->assertTrue($result, 'User successfully created'); $this->assertNotEmpty($user->password); // activate user $foundUser = $this->userModule->findUserByChecker('email_checker', $user->checker->email_checker); $this->assertInstanceOf(User::className(), $foundUser); $this->assertEquals($foundUser->id, $user->id); // change user password $changePasswordForm = new ChangePasswordForm(); $this->assertFalse($changePasswordForm->validate(), 'Check error validation'); $this->assertArrayHasKey('password', $changePasswordForm->getErrors(), 'Check has password error'); $changePasswordForm->password = '******'; $this->assertFalse($changePasswordForm->validate()); $this->assertArrayHasKey('confirmPassword', $changePasswordForm->getErrors(), 'Check has confirmPassword error'); $changePasswordForm->confirmPassword = '******'; $this->assertFalse($changePasswordForm->validate()); $this->assertArrayHasKey('confirmPassword', $changePasswordForm->getErrors(), 'Check has confirmPassword error'); $changePasswordForm->confirmPassword = '******'; $this->assertTrue($changePasswordForm->validate(), 'Check every field is validated'); $result = $this->userModule->changeUserForgottenPassword($changePasswordForm, $foundUser); $this->assertTrue($result, 'Password successfully changed'); $this->assertNull($foundUser->checker->email_checker); // user can authenticate $this->assertTrue($user->canSignIn()); // create new user with exists data $newUser = new UserForm(); $newUser->setAttributes($user->getAttributes()); $this->assertFalse($newUser->validate()); $this->assertArrayHasKey('email', $newUser->getErrors(), 'Check user already exists'); return $foundUser; }