Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

61 lines
1.6KB

  1. <?php
  2. namespace frontend\models;
  3. use common\models\User;
  4. use yii\base\Model;
  5. /**
  6. * Password reset request form
  7. */
  8. class PasswordResetRequestForm extends Model
  9. {
  10. public $email;
  11. /**
  12. * @inheritdoc
  13. */
  14. public function rules()
  15. {
  16. return [
  17. ['email', 'filter', 'filter' => 'trim'],
  18. ['email', 'required'],
  19. ['email', 'email'],
  20. ['email', 'exist',
  21. 'targetClass' => '\common\models\User',
  22. 'filter' => ['status' => User::STATUS_ACTIVE],
  23. 'message' => 'There is no user with such email.'
  24. ],
  25. ];
  26. }
  27. /**
  28. * Sends an email with a link, for resetting the password.
  29. *
  30. * @return boolean whether the email was send
  31. */
  32. public function sendEmail()
  33. {
  34. /* @var $user User */
  35. $user = User::findOne([
  36. 'status' => User::STATUS_ACTIVE,
  37. 'email' => $this->email,
  38. ]);
  39. if ($user) {
  40. if (!User::isPasswordResetTokenValid($user->password_reset_token)) {
  41. $user->generatePasswordResetToken();
  42. }
  43. if ($user->save()) {
  44. return \Yii::$app->mailer->compose(['html' => 'passwordResetToken-html', 'text' => 'passwordResetToken-text'], ['user' => $user])
  45. ->setFrom([\Yii::$app->params['supportEmail'] => \Yii::$app->name . ' robot'])
  46. ->setTo($this->email)
  47. ->setSubject('Password reset for ' . \Yii::$app->name)
  48. ->send();
  49. }
  50. }
  51. return false;
  52. }
  53. }