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 UpdatePasswordForm
|
11
|
|
|
* @package modules\users\models
|
12
|
|
|
*/
|
13
|
|
|
class UpdatePasswordForm extends Model
|
14
|
|
|
{
|
15
|
|
|
public $newPassword;
|
16
|
|
|
public $newPasswordRepeat;
|
17
|
|
|
public $currentPassword;
|
18
|
|
|
|
19
|
|
|
/**
|
20
|
|
|
* @var \modules\users\models\User
|
21
|
|
|
*/
|
22
|
|
|
private $_user;
|
23
|
|
|
|
24
|
|
|
/**
|
25
|
|
|
* UpdatePasswordForm constructor.
|
26
|
|
|
* @param User $user
|
27
|
|
|
* @param array $config
|
28
|
|
|
*/
|
29
|
|
|
public function __construct(User $user, $config = [])
|
30
|
|
|
{
|
31
|
|
|
$this->_user = $user;
|
32
|
|
|
if (!$this->_user) {
|
33
|
|
|
throw new InvalidArgumentException(Module::t('module', 'User not found.'));
|
34
|
|
|
}
|
35
|
|
|
parent::__construct($config);
|
36
|
|
|
}
|
37
|
|
|
|
38
|
|
|
/**
|
39
|
|
|
* @inheritdoc
|
40
|
|
|
* @return array
|
41
|
|
|
*/
|
42
|
|
|
public function rules()
|
43
|
|
|
{
|
44
|
|
|
return [
|
45
|
|
|
[['newPassword', 'newPasswordRepeat', 'currentPassword'], 'required'],
|
46
|
|
|
['newPassword', 'string', 'min' => User::LENGTH_STRING_PASSWORD_MIN, 'max' => User::LENGTH_STRING_PASSWORD_MAX],
|
47
|
|
|
['newPasswordRepeat', 'compare', 'compareAttribute' => 'newPassword'],
|
48
|
|
|
['currentPassword', 'validateCurrentPassword', 'skipOnEmpty' => false, 'skipOnError' => false],
|
49
|
|
|
];
|
50
|
|
|
}
|
51
|
|
|
|
52
|
|
|
/**
|
53
|
|
|
* @param string $attribute
|
54
|
|
|
*/
|
55
|
|
|
public function validateCurrentPassword($attribute)
|
56
|
|
|
{
|
57
|
|
|
if (!empty($this->newPassword) && !empty($this->newPasswordRepeat) && !$this->hasErrors()) {
|
58
|
|
|
$this->processValidatePassword($attribute);
|
59
|
|
|
} else {
|
60
|
|
|
$this->addError($attribute, Module::t('module', 'Not all fields are filled in correctly.'));
|
61
|
|
|
}
|
62
|
|
|
}
|
63
|
|
|
|
64
|
|
|
/**
|
65
|
|
|
* @param string $attribute
|
66
|
|
|
*/
|
67
|
|
|
protected function processValidatePassword($attribute)
|
68
|
|
|
{
|
69
|
|
|
if ($attribute) {
|
70
|
|
|
if (!$this->_user->validatePassword($this->$attribute))
|
71
|
|
|
$this->addError($attribute, Module::t('module', 'Incorrect current password.'));
|
72
|
|
|
} else {
|
73
|
|
|
$this->addError($attribute, Module::t('module', 'Enter your current password.'));
|
74
|
|
|
}
|
75
|
|
|
}
|
76
|
|
|
|
77
|
|
|
/**
|
78
|
|
|
* @inheritdoc
|
79
|
|
|
* @return array
|
80
|
|
|
*/
|
81
|
|
|
public function attributeLabels()
|
82
|
|
|
{
|
83
|
|
|
return [
|
84
|
|
|
'newPassword' => Module::t('module', 'New Password'),
|
85
|
|
|
'newPasswordRepeat' => Module::t('module', 'Repeat Password'),
|
86
|
|
|
'currentPassword' => Module::t('module', 'Current Password'),
|
87
|
|
|
];
|
88
|
|
|
}
|
89
|
|
|
|
90
|
|
|
/**
|
91
|
|
|
* Resets password.
|
92
|
|
|
*
|
93
|
|
|
* @return bool if password was reset.
|
94
|
|
|
* @throws \yii\base\Exception
|
95
|
|
|
*/
|
96
|
|
|
public function resetPassword()
|
97
|
|
|
{
|
98
|
|
|
$user = $this->_user;
|
99
|
|
|
$user->setPassword($this->newPassword);
|
100
|
|
|
return $user->save();
|
101
|
|
|
}
|
102
|
|
|
}
|
103
|
|
|
|