1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mdm\admin\models\form; |
4
|
|
|
|
5
|
|
|
use mdm\admin\components\UserStatus; |
6
|
|
|
use mdm\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 ?: 'mdm\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
|
|
View Code Duplication |
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
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: