/
PasswordChangeForm.php
84 lines (75 loc) · 1.9 KB
/
PasswordChangeForm.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
namespace app\modules\user\forms\frontend;
use app\modules\user\models\User;
use app\modules\user\Module;
use yii\base\Model;
/**
* Password reset form
*/
class PasswordChangeForm extends Model
{
public $currentPassword;
public $newPassword;
public $newPasswordRepeat;
/**
* @var User
*/
private $_user;
/**
* @param User $user
* @param array $config
*/
public function __construct(User $user, $config = [])
{
$this->_user = $user;
parent::__construct($config);
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['currentPassword', 'newPassword', 'newPasswordRepeat'], 'required'],
['currentPassword', 'validatePassword'],
['newPassword', 'string', 'min' => 6],
['newPasswordRepeat', 'compare', 'compareAttribute' => 'newPassword'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'newPassword' => Module::t('module', 'USER_NEW_PASSWORD'),
'newPasswordRepeat' => Module::t('module', 'USER_REPEAT_PASSWORD'),
'currentPassword' => Module::t('module', 'USER_CURRENT_PASSWORD'),
];
}
/**
* @param string $attribute
* @param array $params
*/
public function validatePassword($attribute, $params)
{
if (!$this->hasErrors()) {
if (!$this->_user->validatePassword($this->$attribute)) {
$this->addError($attribute, Module::t('module', 'ERROR_WRONG_CURRENT_PASSWORD'));
}
}
}
/**
* @return boolean
*/
public function changePassword()
{
if ($this->validate()) {
$user = $this->_user;
$user->setPassword($this->newPassword);
return $user->save();
} else {
return false;
}
}
}