Completed
Push — master ( 55e0f0...d0f882 )
by Alexey
11:22
created

PasswordResetRequestForm::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
namespace modules\users\models;
3
4
use Yii;
5
use yii\base\Model;
6
use modules\users\Module;
7
8
/**
9
 * Class PasswordResetRequestForm
10
 * @package modules\users\models\frontend
11
 */
12
class PasswordResetRequestForm extends Model
13
{
14
    public $email;
15
16
    /**
17
     * @inheritdoc
18
     */
19
    public function rules()
20
    {
21
        return [
22
            ['email', 'trim'],
23
            ['email', 'required'],
24
            ['email', 'email'],
25
            ['email', 'exist',
26
                'targetClass' => '\modules\users\models\User',
27
                'filter' => ['status' => User::STATUS_ACTIVE],
28
                'message' => Module::t('module', 'There is no user with this e-mail.'),
29
            ],
30
        ];
31
    }
32
33
    /**
34
     * Sends an email with a link, for resetting the password.
35
     *
36
     * @return boolean whether the email was send
37
     */
38
    public function sendEmail()
39
    {
40
        /* @var $user \modules\users\models\User */
41
        $user = User::findOne([
42
            'status' => User::STATUS_ACTIVE,
43
            'email' => $this->email,
44
        ]);
45
46
        if (!$user) {
47
            return false;
48
        }
49
50
        if (!User::isPasswordResetTokenValid($user->password_reset_token)) {
51
            $user->generatePasswordResetToken();
52
            if (!$user->save()) {
53
                return false;
54
            }
55
        }
56
57
        return Yii::$app->mailer->compose(
58
            ['html' => '@modules/users/mail/passwordResetToken-html', 'text' => '@modules/users/mail/passwordResetToken-text'],
59
            ['user' => $user])
60
            ->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name . ' robot'])
61
            ->setTo($this->email)
62
            ->setSubject(Module::t('module', 'Password reset for') . ' ' . Yii::$app->name)
63
            ->send();
64
    }
65
}
66