Completed
Push — master ( 3c8880...e2c218 )
by Dmitry
06:42 queued 02:46
created

PasswordResetRequestForm::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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