/** * 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('user', 'You will receive an email with instructions on how to reset your password in a few minutes.')); return true; } return false; }
/** * 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('user', 'An email has been sent with instructions for resetting your password')); return true; } return false; }
/** * Creates new confirmation token and sends it to the user. * * @return bool */ public function resend() { if (!$this->validate()) { return false; } $user = $this->finder->findUserByEmail($this->email); if ($user instanceof User && !$user->isConfirmed) { /** @var Token $token */ $token = \Yii::createObject(['class' => Token::className(), 'user_id' => $user->id, 'type' => Token::TYPE_CONFIRMATION]); $token->save(false); $this->mailer->sendConfirmationMessage($user, $token); } \Yii::$app->session->setFlash('info', \Yii::t('user', 'A message has been sent to your email address. It contains a confirmation link that you must click to complete registration.')); return true; }
/** * 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; }
public static function updateUser($user_id, $data = null) { $user = User::findOne($user_id); $user->scenario = 'settings'; $profile = $user->profile; $oldEmail = $user->email; if ($user->load(['User' => $data]) && $user->validate()) { if ($user->email != $oldEmail) { $user->unconfirmed_email = $user->email; $user->email = $oldEmail; $token = \Yii::createObject(['class' => Token::className(), 'user_id' => $user->id, 'type' => Token::TYPE_CONFIRM_NEW_EMAIL]); $token->save(false); $mailer = Yii::createObject(['class' => Mailer::className(), 'reconfirmationSubject' => 'Mail confirmation']); $mailer->sendReconfirmationMessage($user, $token); } if (!$profile) { $profile = Yii::createObject(['class' => Profile::className(), 'user_id' => $user->id]); $profile->save(); } $profile->load(['Profile' => $data]); $profile->save(); $user->save(); } else { self::error($user); } return self::clearUserData($user); }
/** * 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('user', 'We have sent confirmation links to both old and new email addresses. You must click both links to complete your request')); }
/** * Tests recovery request form. */ public function testRecoveryRequest() { $mailer = test::double(Mailer::className(), ['sendRecoveryMessage' => true]); $form = Yii::createObject(['class' => RecoveryForm::className(), 'scenario' => 'request']); $this->specify('form is not valid when email is empty', function () use($form) { $form->setAttributes(['email' => '']); verify($form->validate())->false(); verify($form->getErrors('email'))->contains('Email cannot be blank.'); }); $this->specify('form is not valid when email is incorrect', function () use($form) { $form->setAttributes(['email' => 'foobar']); verify($form->validate())->false(); verify($form->getErrors('email'))->contains('Email is not a valid email address.'); }); $this->specify('form is not valid when user does not exist', function () use($form) { test::double(ActiveQuery::className(), ['exists' => false]); $form->setAttributes(['email' => '*****@*****.**']); verify($form->validate())->false(); verify($form->getErrors('email'))->contains('There is no user with this email address'); test::double(ActiveQuery::className(), ['exists' => true]); }); $this->specify('form is not valid when user is not confirmed', function () use($form) { $user = \Yii::createObject(User::className()); test::double($user, ['getIsConfirmed' => false]); test::double(Finder::className(), ['findUserByEmail' => $user]); $form->setAttributes(['email' => '*****@*****.**']); verify($form->validate())->false(); verify($form->getErrors('email'))->contains('You need to confirm your email address'); test::double($user, ['getIsConfirmed' => true]); verify($form->validate())->true(); }); $this->specify('sendRecoveryMessage return true if validation succeeded', function () use($form, $mailer) { test::double($form, ['validate' => true]); $token = test::double(Token::className(), ['save' => true]); $user = \Yii::createObject(['class' => User::className(), 'id' => 1]); test::double(Finder::className(), ['findUserByEmail' => $user]); verify($form->sendRecoveryMessage())->true(); $token->verifyInvoked('save'); verify(\Yii::$app->session->getFlash('info'))->equals('An email has been sent with instructions for resetting your password'); $mailer->verifyInvoked('sendRecoveryMessage'); }); }
/** * 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. * * @return bool */ public function register() { if ($this->getIsNewRecord() == false) { throw new \RuntimeException('Calling "' . __CLASS__ . '::' . __METHOD__ . '" on existing user'); } $this->confirmed_at = $this->module->enableConfirmation ? null : time(); $this->password = $this->module->enableGeneratingPassword ? Password::generate(8) : $this->password; $this->trigger(self::BEFORE_REGISTER); if (!$this->save()) { return false; } if ($this->module->enableConfirmation) { /** @var Token $token */ $token = Yii::createObject(['class' => Token::className(), 'type' => Token::TYPE_CONFIRMATION]); $token->link('user', $this); } $this->mailer->sendWelcomeMessage($this, isset($token) ? $token : null); $this->trigger(self::AFTER_REGISTER); return true; }
/** * @return Mailer * @throws \yii\base\InvalidConfigException */ protected function getMailer() { return Yii::$container->get(Mailer::className()); }