|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace modules\users\models; |
|
4
|
|
|
|
|
5
|
|
|
use yii\base\Model; |
|
6
|
|
|
use yii\base\InvalidArgumentException; |
|
7
|
|
|
use modules\users\Module; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class ResetPasswordForm |
|
11
|
|
|
* @package modules\users\models |
|
12
|
|
|
* |
|
13
|
|
|
* @property string $password Password |
|
14
|
|
|
*/ |
|
15
|
|
|
class ResetPasswordForm extends Model |
|
16
|
|
|
{ |
|
17
|
|
|
public $password; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var \modules\users\models\User |
|
21
|
|
|
*/ |
|
22
|
|
|
private $_user; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Creates a form model given a token. |
|
26
|
|
|
* |
|
27
|
|
|
* @param mixed $token |
|
28
|
|
|
* @param array $config name-value pairs that will be used to initialize the object properties |
|
29
|
|
|
* @throws \yii\base\InvalidArgumentException 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 InvalidArgumentException(Module::t('module', 'Password reset token cannot be blank.')); |
|
35
|
|
|
} |
|
36
|
|
|
$this->_user = User::findByPasswordResetToken($token); |
|
37
|
|
|
if (!$this->_user) { |
|
38
|
|
|
throw new InvalidArgumentException(Module::t('module', 'Wrong password reset token.')); |
|
39
|
|
|
} |
|
40
|
|
|
parent::__construct($config); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @inheritdoc |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
|
|
public function rules() |
|
48
|
|
|
{ |
|
49
|
|
|
return [ |
|
50
|
|
|
['password', 'required'], |
|
51
|
|
|
['password', 'string', 'min' => User::LENGTH_STRING_PASSWORD_MIN, 'max' => User::LENGTH_STRING_PASSWORD_MAX], |
|
52
|
|
|
]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @inheritdoc |
|
57
|
|
|
* @return array |
|
58
|
|
|
*/ |
|
59
|
|
|
public function attributeLabels() |
|
60
|
|
|
{ |
|
61
|
|
|
return [ |
|
62
|
|
|
'password' => Module::t('module', 'New Password'), |
|
63
|
|
|
]; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Resets password. |
|
68
|
|
|
* |
|
69
|
|
|
* @return bool if password was reset. |
|
70
|
|
|
* @throws \yii\base\Exception |
|
71
|
|
|
*/ |
|
72
|
|
|
public function resetPassword() |
|
73
|
|
|
{ |
|
74
|
|
|
$user = $this->_user; |
|
75
|
|
|
$user->setPassword($this->password); |
|
76
|
|
|
$user->removePasswordResetToken(); |
|
77
|
|
|
return $user->save(false); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|