1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* _ __ __ _____ _____ ___ ____ _____ |
5
|
|
|
* | | / // // ___//_ _// || __||_ _| |
6
|
|
|
* | |/ // /(__ ) / / / /| || | | | |
7
|
|
|
* |___//_//____/ /_/ /_/ |_||_| |_| |
8
|
|
|
* @link https://vistart.me/ |
9
|
|
|
* @copyright Copyright (c) 2016 - 2017 vistart |
10
|
|
|
* @license https://vistart.me/license/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace rhosocial\user\forms; |
14
|
|
|
|
15
|
|
|
use rhosocial\user\User; |
16
|
|
|
use Yii; |
17
|
|
|
use yii\base\Model; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @version 1.0 |
21
|
|
|
* @author vistart <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class ChangePasswordForm extends Model |
24
|
|
|
{ |
25
|
|
|
public $password; |
26
|
|
|
public $new_password; |
27
|
|
|
public $new_password_repeat; |
28
|
|
|
private $_user = false; |
29
|
|
|
|
30
|
|
|
const SCENARIO_ADMIN = 'admin'; |
31
|
|
|
|
32
|
|
|
public function attributeLabels() |
33
|
|
|
{ |
34
|
|
|
return [ |
35
|
|
|
'password' => Yii::t('user', 'Password'), |
36
|
|
|
'new_password' => Yii::t('user', 'New Password'), |
37
|
|
|
'new_password_repeat' => Yii::t('user', 'New Password Repeat'), |
38
|
|
|
]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function rules() |
42
|
|
|
{ |
43
|
|
|
return [ |
44
|
|
|
['password', 'required', 'on' =>self::SCENARIO_DEFAULT], |
45
|
|
|
[['new_password', 'new_password_repeat'], 'required'], |
46
|
|
|
['password', 'string', 'min' => 6, 'max' => 32, 'on' => self::SCENARIO_DEFAULT], |
47
|
|
|
[['new_password', 'new_password_repeat'], 'string', 'min' => 6, 'max' => 32], |
48
|
|
|
['password', 'validatePassword', 'on' => self::SCENARIO_DEFAULT], |
49
|
|
|
['new_password', 'compare'], |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function clearAttributes() |
54
|
|
|
{ |
55
|
|
|
$this->password = ''; |
56
|
|
|
$this->new_password = ''; |
57
|
|
|
$this->new_password_repeat = ''; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Validates the password. |
62
|
|
|
* This method serves as the inline validation for password. |
63
|
|
|
* |
64
|
|
|
* @param string $attribute the attribute currently being validated |
65
|
|
|
* @param array $params the additional name-value pairs given in the rule |
66
|
|
|
*/ |
67
|
|
|
public function validatePassword($attribute, $params) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
if (!$this->hasErrors()) { |
70
|
|
|
$user = $this->getUser(); |
71
|
|
|
|
72
|
|
|
if (!$user || !$user->validatePassword($this->password)) { |
|
|
|
|
73
|
|
|
$this->addError($attribute, Yii::t('user', 'Incorrect password.')); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Change password. |
80
|
|
|
* @return boolean Whether the password changed. |
81
|
|
|
*/ |
82
|
|
|
public function changePassword() |
83
|
|
|
{ |
84
|
|
|
if ($this->validate()) { |
85
|
|
|
if (!($user = $this->getUser())) { |
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
if (!$user->applyForNewPassword()) { |
|
|
|
|
89
|
|
|
return false; |
90
|
|
|
} |
91
|
|
|
return $user->resetPassword($this->new_password, $user->getPasswordResetToken()); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Set user. |
98
|
|
|
* @param User $user |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
public function setUser($user) |
102
|
|
|
{ |
103
|
|
|
if ($user instanceof User) { |
104
|
|
|
$this->_user = $user; |
|
|
|
|
105
|
|
|
return true; |
106
|
|
|
} |
107
|
|
|
$this->_user = null; |
108
|
|
|
return false; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Finds user. |
113
|
|
|
* |
114
|
|
|
* @return User|null |
115
|
|
|
*/ |
116
|
|
|
public function getUser() |
117
|
|
|
{ |
118
|
|
|
return $this->_user; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* This method adds ADMIN scenario, because the administrator does not need |
123
|
|
|
* to know the original password when changing it. |
124
|
|
|
* @return array scenarios. |
125
|
|
|
*/ |
126
|
|
|
public function scenarios() |
127
|
|
|
{ |
128
|
|
|
return array_merge(parent::scenarios(), [ |
129
|
|
|
self::SCENARIO_ADMIN => ['new_password', 'new_password_repeat'], |
130
|
|
|
]); |
131
|
|
|
} |
132
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.