ChangePassword.php
2.31 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace frontend\modules\user\models\form;
use Yii;
//
use frontend\modules\user\models\User;
/**
* Class ChangePassword
*
* @package frontend\modules\user\models\form
* @author FilamentV <vortex.filament@gmail.com>
* @copyright (c), Thread
*/
class ChangePassword extends \yii\base\Model
{
public $password, $password_old, $password_confirmation;
//
const FLASH_KEY = 'ChangePassword';
//
protected $_password_min_length;
/**
*
*/
public function init()
{
parent::init();
/**
* @var $module \frontend\modules\user\User
*/
$module = Yii::$app->getModule('user');
$this->_password_min_length = $module->password_min_length;
}
/**
* @return array
*/
public function rules()
{
return [
[['password', 'password_confirmation', 'password_old'], 'required'],
[['password_old'], 'validateOLDPassword'],
[['password_confirmation'], 'compare', 'compareAttribute' => 'password',],
[['password', 'password_confirmation'], 'string', 'min' => $this->_password_min_length],
];
}
/**
* @return array
*/
public function scenarios()
{
return [
'passwordChange' => ['password', 'password_confirmation', 'password_old'],
];
}
/**
* @return array
*/
public function attributeLabels()
{
return [
'password_old' => Yii::t('user', 'Password old'),
'password' => Yii::t('user', 'Password'),
'password_confirmation' => Yii::t('user', 'Password confirmation'),
];
}
/**
*
*/
public function validateOLDPassword()
{
if (!$this->hasErrors()) {
$user = User::getUserById(Yii::$app->getUser()->id);
if (!$user || !$user->validatePassword($this->password_old)) {
$this->addError('password_old', Yii::t('user', 'Incorrect password'));
}
}
}
/**
* @param bool|true $value
*/
public function addFlash($value = true)
{
Yii::$app->getSession()->addFlash(self::FLASH_KEY, $value);
}
/**
* @return mixed
*/
public function getFlash()
{
return Yii::$app->getSession()->getFlash(self::FLASH_KEY, '');
}
}