ResetPassword   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 28
dl 0
loc 76
c 0
b 0
f 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A resetPassword() 0 7 1
A __construct() 0 17 6
A rules() 0 6 1
A isPasswordResetTokenValid() 0 9 2
1
<?php
2
3
namespace toir427\admin\models\form;
4
5
use toir427\admin\components\UserStatus;
6
use toir427\admin\models\User;
7
use Yii;
8
use yii\base\InvalidParamException;
9
use yii\base\Model;
10
use yii\helpers\ArrayHelper;
11
12
/**
13
 * Password reset form
14
 */
15
class ResetPassword extends Model
16
{
17
    public $password;
18
    public $retypePassword;
19
    /**
20
     * @var User
21
     */
22
    private $_user;
23
24
    /**
25
     * Creates a form model given a token.
26
     *
27
     * @param  string $token
28
     * @param  array $config name-value pairs that will be used to initialize the object properties
29
     * @throws InvalidParamException if token is empty or not valid
30
     */
31
    public function __construct($token, $config = [])
32
    {
33
        if (empty($token) || !is_string($token)) {
34
            throw new InvalidParamException('Password reset token cannot be blank.');
35
        }
36
        // check token
37
        $class = Yii::$app->getUser()->identityClass ?: 'toir427\admin\models\User';
38
        if (static::isPasswordResetTokenValid($token)) {
39
            $this->_user = $class::findOne([
40
                    'password_reset_token' => $token,
41
                    'status' => UserStatus::ACTIVE
42
            ]);
43
        }
44
        if (!$this->_user) {
45
            throw new InvalidParamException('Wrong password reset token.');
46
        }
47
        parent::__construct($config);
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function rules()
54
    {
55
        return [
56
            [['password', 'retypePassword'], 'required'],
57
            ['password', 'string', 'min' => 6],
58
            ['retypePassword', 'compare', 'compareAttribute' => 'password']
59
        ];
60
    }
61
62
    /**
63
     * Resets password.
64
     *
65
     * @return boolean if password was reset.
66
     */
67
    public function resetPassword()
68
    {
69
        $user = $this->_user;
70
        $user->setPassword($this->password);
71
        $user->removePasswordResetToken();
72
73
        return $user->save(false);
74
    }
75
76
    /**
77
     * Finds out if password reset token is valid
78
     *
79
     * @param string $token password reset token
80
     * @return boolean
81
     */
82
    public static function isPasswordResetTokenValid($token)
83
    {
84
        if (empty($token)) {
85
            return false;
86
        }
87
        $expire = ArrayHelper::getValue(Yii::$app->params, 'user.passwordResetTokenExpire', 24 * 3600);
88
        $parts = explode('_', $token);
89
        $timestamp = (int) end($parts);
90
        return $timestamp + $expire >= time();
91
    }
92
}
93