/** * Sends recovery message. * * @return bool */ public function sendRecoveryMessage() { if ($this->validate()) { /** @var Token $token */ $token = \Yii::createObject(['class' => Token::className(), 'user_id' => $this->user->id, 'type' => Token::TYPE_RECOVERY]); $token->save(false); $this->mailer->sendRecoveryMessage($this->user, $token); \Yii::$app->session->setFlash('info', \Yii::t('users', 'You will receive an email with instructions on how to reset your password in a few minutes.')); return true; } return false; }
<?php use mii\modules\users\models\Token; use mii\modules\users\models\User; use tests\codeception\_pages\RegistrationPage; use yii\helpers\Html; $I = new FunctionalTester($scenario); $I->wantTo('ensure that registration works'); $page = RegistrationPage::openBy($I); $I->amGoingTo('try to register with empty credentials'); $page->register('', '', ''); $I->see('Username cannot be blank'); $I->see('Email cannot be blank'); $I->see('Password cannot be blank'); $I->amGoingTo('try to register with already used email and username'); $user = $I->getFixture('users')->getModel('users'); $page->register($user->username, $user->email, 'qwerty'); $I->see(Html::encode('This username has already been taken')); $I->see(Html::encode('This email address has already been taken')); $I->amGoingTo('try to register'); $page->register('tester', '*****@*****.**', 'tester'); $I->see('A message has been sent to your email address. It contains a confirmation link that you must click to complete registration.'); $user = $I->grabRecord(User::className(), ['email' => '*****@*****.**']); $token = $I->grabRecord(Token::className(), ['user_id' => $user->id, 'type' => Token::TYPE_CONFIRMATION]); $I->seeInEmail(Html::encode($token->url));
/** * This method is used to register new user account. If Module::enableConfirmation is set true, this method * will generate new confirmation token and use mailer to send it to the user. Otherwise it will log the user in. * If Module::enableGeneratingPassword is set true, this method will generate new 8-char password. After saving user * to database, this method uses mailer component to send credentials (username and password) to user via email. * * @return bool */ public function register() { if ($this->getIsNewRecord() == false) { throw new \RuntimeException('Calling "' . __CLASS__ . '::' . __METHOD__ . '" on existing user'); } if ($this->module->enableConfirmation == false) { $this->confirmed_at = time(); } if ($this->module->enableGeneratingPassword) { $this->password = Password::generate(8); } $this->trigger(self::USER_REGISTER_INIT); if ($this->save()) { $this->trigger(self::USER_REGISTER_DONE); if ($this->module->enableConfirmation) { $token = \Yii::createObject(['class' => Token::className(), 'type' => Token::TYPE_CONFIRMATION]); $token->link('user', $this); $this->mailer->sendConfirmationMessage($this, $token); } else { \Yii::$app->user->login($this); } if ($this->module->enableGeneratingPassword) { $this->mailer->sendWelcomeMessage($this); } \Yii::$app->session->setFlash('info', $this->getFlashMessage()); \Yii::getLogger()->log('User has been registered', Logger::LEVEL_INFO); return true; } \Yii::getLogger()->log('An error occurred while registering user account', Logger::LEVEL_ERROR); return false; }
/** * Creates new confirmation token and sends it to the user. * * @return bool */ public function resend() { if (!$this->validate()) { return false; } /** @var Token $token */ $token = \Yii::createObject(['class' => Token::className(), 'user_id' => $this->user->id, 'type' => Token::TYPE_CONFIRMATION]); $token->save(false); $this->mailer->sendConfirmationMessage($this->user, $token); \Yii::$app->session->setFlash('info', \Yii::t('users', 'A message has been sent to your email address. It contains a confirmation link that you must click to complete registration.')); return true; }
$I->wantTo('ensure that password recovery works'); $page = RecoveryPage::openBy($I); $I->amGoingTo('try to request recovery token for unconfirmed account'); $user = $I->getFixture('users')->getModel('unconfirmed'); $page->recover($user->email); $I->see('You need to confirm your email address'); $I->amGoingTo('try to request recovery token'); $user = $I->getFixture('users')->getModel('users'); $page->recover($user->email); $I->see('You will receive an email with instructions on how to reset your password in a few minutes.'); $user = $I->grabRecord(User::className(), ['email' => $user->email]); $token = $I->grabRecord(Token::className(), ['user_id' => $user->id, 'type' => Token::TYPE_RECOVERY]); $I->seeInEmail(Html::encode($token->getUrl())); $I->seeInEmailRecipients($user->email); $I->amGoingTo('reset password with invalid token'); $user = $I->getFixture('users')->getModel('user_with_expired_recovery_token'); $token = $I->grabRecord(Token::className(), ['user_id' => $user->id, 'type' => Token::TYPE_RECOVERY]); $I->amOnPage(Url::toRoute(['/users/recovery/reset', 'id' => $user->id, 'code' => $token->code])); $I->see('Recovery link is invalid or out-of-date. Please try requesting a new one.'); $I->amGoingTo('reset password'); $user = $I->getFixture('users')->getModel('user_with_recovery_token'); $token = $I->grabRecord(Token::className(), ['user_id' => $user->id, 'type' => Token::TYPE_RECOVERY]); $I->amOnPage(Url::toRoute(['/users/recovery/reset', 'id' => $user->id, 'code' => $token->code])); $I->fillField('#recovery-form-password', 'newpass'); $I->click('Finish'); $I->see('Your password has been changed successfully.'); $page = LoginPage::openBy($I); $page->login($user->email, 'qwerty'); $I->see('Invalid login or password'); $page->login($user->email, 'newpass'); $I->dontSee('Invalid login or password');
/** * Sends a confirmation message to both old and new email addresses with link to confirm changing of email. * @throws \yii\base\InvalidConfigException */ protected function secureEmailChange() { $this->defaultEmailChange(); /** @var Token $token */ $token = \Yii::createObject(['class' => Token::className(), 'user_id' => $this->user->id, 'type' => Token::TYPE_CONFIRM_OLD_EMAIL]); $token->save(false); $this->mailer->sendReconfirmationMessage($this->user, $token); // unset flags if they exist $this->user->flags &= ~User::NEW_EMAIL_CONFIRMED; $this->user->flags &= ~User::OLD_EMAIL_CONFIRMED; $this->user->save(false); \Yii::$app->session->setFlash('info', \Yii::t('users', 'We have sent confirmation links to both old and new email addresses. You should click both links in order to complete your request')); }