Blame view

common/models/PasswordResetRequestForm.php 2.28 KB
950817c6   Alex Savenko   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  <?php
      
      namespace common\models;
      
      use artbox\order\models\Customer;
      use Yii;
      use yii\base\Model;
      
      /**
       * Password reset request form
       */
      class PasswordResetRequestForm extends Model
      {
          public $email;
          /**
           * @inheritdoc
           */
          public function rules()
          {
              return [
                  [
                      'email',
                      'trim',
                  ],
                  [
                      'email',
                      'required',
                  ],
                  [
                      'email',
                      'email',
                  ],
                  [
                      'email',
                      'exist',
                      'targetClass' => '\artbox\order\models\Customer',
                      'filter'      => [ 'status' => Customer::STATUS_ACTIVE ],
                      'message'     => 'There is no user with this email address.',
                  ],
              ];
          }
          /**
           * Sends an email with a link, for resetting the password.
           *
           * @return bool whether the email was send
           */
          public function sendEmail()
          {
              /* @var $user Customer */
              $user = Customer::findOne(
                  [
                      'status' => Customer::STATUS_ACTIVE,
                      'email'  => $this->email,
                  ]
              );
              if (!$user) {
                  return false;
              }
              
              if (!Customer::isPasswordResetTokenValid($user->password_reset_token)) {
                  $user->generatePasswordResetToken();
                  if (!$user->save()) {
                      return false;
                  }
              }
              return Yii::$app->mailer->compose(
                  [
                      'html' => 'passwordResetToken-html',
                      'text' => 'passwordResetToken-text',
                  ],
                  [ 'user' => $user ]
              )
                                      ->setFrom([ Yii::$app->params[ 'supportEmail' ] => Yii::$app->name . ' robot' ])
                                      ->setTo($this->email)
                                      ->setSubject('Password reset for ' . Yii::$app->name)
                                      ->send();
          }
      }