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.

62 lines
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. {
  11. public $email;
  12. /**
  13. * @inheritdoc
  14. */
  15. public function rules()
  16. {
  17. return [
  18. ['email', 'filter', 'filter' => 'trim'],
  19. ['email', 'required'],
  20. ['email', 'email'],
  21. ['email', 'exist',
  22. 'targetClass' => '\common\models\User',
  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. 'email' => $this->email,
  37. ]);
  38. if ($user) {
  39. if (!User::isPasswordResetTokenValid($user->password_reset_token)) {
  40. $user->generatePasswordResetToken();
  41. }
  42. if ($user->save())
  43. {
  44. return Yii::$app->mailer->compose(['html' => 'passwordResetToken-html', 'text' => 'passwordResetToken-text'], ['user' => $user])
  45. ->setFrom(['contact@laboiteapain.net' => 'La boîte à pain'])
  46. ->setTo($this->email)
  47. ->setSubject('[La boîte à pain] Mot de passe oublié')
  48. ->send();
  49. }
  50. }
  51. return false;
  52. }
  53. }