You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 line
1.5KB

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