1
|
|
|
<?php |
2
|
|
|
namespace site\models; |
3
|
|
|
|
4
|
|
|
use common\models\User; |
5
|
|
|
use yii\base\Model; |
6
|
|
|
use Yii; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* change password form |
10
|
|
|
*/ |
11
|
|
|
class ChangePasswordForm extends Model |
12
|
|
|
{ |
13
|
|
|
public $old_password; |
14
|
|
|
public $new_password; |
15
|
|
|
private $user; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Creates a form model |
19
|
|
|
* |
20
|
|
|
* @param object $user |
21
|
|
|
* @param array $config name-value pairs that will be used to initialize the object properties |
22
|
|
|
*/ |
23
|
|
|
public function __construct(\common\interfaces\UserInterface $user, $config = []) { |
24
|
|
|
$this->user = $user; |
25
|
|
|
parent::__construct($config); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @inheritdoc |
30
|
|
|
*/ |
31
|
|
|
public function rules() |
32
|
|
|
{ |
33
|
|
|
return [ |
34
|
|
|
['old_password', 'string', 'min' => 6], |
35
|
|
|
['new_password', 'string', 'min' => 8], |
36
|
|
|
[['old_password', 'new_password'], 'required'], |
37
|
|
|
['old_password', 'validatePassword'], |
38
|
|
|
]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function attributeLabels() { |
42
|
|
|
return [ |
43
|
|
|
'old_password' => 'Current Password', |
44
|
|
|
'new_password' => 'New Password', |
45
|
|
|
]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Validates the password. |
50
|
|
|
* This method serves as the inline validation for password. |
51
|
|
|
*/ |
52
|
|
|
public function validatePassword() |
53
|
|
|
{ |
54
|
|
|
if (!$this->hasErrors()) { |
55
|
|
|
if (!$this->user->validatePassword($this->old_password)) { |
|
|
|
|
56
|
|
|
$this->addError('old_password', 'Incorrect email or password.'); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* changes user's password. |
63
|
|
|
* |
64
|
|
|
* @return Boolean whether or not the save was successful |
65
|
|
|
*/ |
66
|
|
|
public function changePassword() |
67
|
|
|
{ |
68
|
|
|
$this->user->setPassword($this->new_password); |
|
|
|
|
69
|
|
|
return $this->user->save(); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|