1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the 2amigos/yii2-usuario project. |
5
|
|
|
* |
6
|
|
|
* (c) 2amigOS! <http://2amigos.us/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Da\User\Form; |
13
|
|
|
|
14
|
|
|
use Da\User\Factory\EmailChangeStrategyFactory; |
15
|
|
|
use Da\User\Helper\SecurityHelper; |
16
|
|
|
use Da\User\Model\User; |
17
|
|
|
use Da\User\Traits\ContainerAwareTrait; |
18
|
|
|
use Da\User\Traits\ModuleAwareTrait; |
19
|
|
|
use Yii; |
20
|
|
|
use yii\base\Model; |
21
|
|
|
|
22
|
|
|
class SettingsForm extends Model |
23
|
|
|
{ |
24
|
|
|
use ModuleAwareTrait; |
25
|
|
|
use ContainerAwareTrait; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
public $email; |
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
public $username; |
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
public $new_password; |
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
public $current_password; |
43
|
|
|
/** |
44
|
|
|
* @var SecurityHelper |
45
|
|
|
*/ |
46
|
|
|
protected $securityHelper; |
47
|
|
|
|
48
|
|
|
/** @var User */ |
49
|
|
|
protected $user; |
50
|
|
|
|
51
|
1 |
|
public function __construct(SecurityHelper $securityHelper, array $config = []) |
|
|
|
|
52
|
|
|
{ |
53
|
1 |
|
$this->securityHelper = $securityHelper; |
54
|
|
|
$config = [ |
55
|
1 |
|
'username' => $this->getUser()->username, |
|
|
|
|
56
|
1 |
|
'email' => $this->getUser()->unconfirmed_email? : $this->getUser()->email |
|
|
|
|
57
|
|
|
]; |
58
|
1 |
|
parent::__construct($config); |
59
|
1 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
1 |
|
public function rules() |
65
|
|
|
{ |
66
|
|
|
return [ |
67
|
1 |
|
'usernameRequired' => ['username', 'required'], |
68
|
|
|
'usernameTrim' => ['username', 'filter', 'filter' => 'trim'], |
69
|
|
|
'usernameLength' => ['username', 'string', 'min' => 3, 'max' => 255], |
70
|
|
|
'usernamePattern' => ['username', 'match', 'pattern' => '/^[-a-zA-Z0-9_\.@]+$/'], |
71
|
|
|
'emailRequired' => ['email', 'required'], |
72
|
|
|
'emailTrim' => ['email', 'filter', 'filter' => 'trim'], |
73
|
|
|
'emailPattern' => ['email', 'email'], |
74
|
|
|
'emailUsernameUnique' => [ |
75
|
|
|
['email', 'username'], |
76
|
1 |
|
'unique', |
77
|
1 |
|
'when' => function ($model, $attribute) { |
78
|
|
|
return $this->getUser()->$attribute != $model->$attribute; |
79
|
1 |
|
}, |
80
|
1 |
|
'targetClass' => $this->getClassMap()->get(User::class), |
81
|
|
|
], |
82
|
|
|
'newPasswordLength' => ['new_password', 'string', 'max' => 72, 'min' => 6], |
83
|
|
|
'currentPasswordRequired' => ['current_password', 'required'], |
84
|
|
|
'currentPasswordValidate' => [ |
85
|
1 |
|
'current_password', |
86
|
1 |
|
function ($attribute) { |
87
|
|
|
if (!$this->securityHelper->validatePassword($this->$attribute, $this->getUser()->password_hash)) { |
|
|
|
|
88
|
|
|
$this->addError($attribute, Yii::t('usuario', 'Current password is not valid')); |
89
|
|
|
} |
90
|
1 |
|
}, |
91
|
|
|
], |
92
|
|
|
]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
1 |
|
public function attributeLabels() |
99
|
|
|
{ |
100
|
|
|
return [ |
101
|
1 |
|
'email' => Yii::t('usuario', 'Email'), |
102
|
1 |
|
'username' => Yii::t('usuario', 'Username'), |
103
|
1 |
|
'new_password' => Yii::t('usuario', 'New password'), |
104
|
1 |
|
'current_password' => Yii::t('usuario', 'Current password'), |
105
|
|
|
]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return User|null|\yii\web\IdentityInterface |
110
|
|
|
*/ |
111
|
1 |
|
public function getUser() |
112
|
|
|
{ |
113
|
1 |
|
if ($this->user == null) { |
114
|
1 |
|
$this->user = Yii::$app->user->identity; |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
return $this->user; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Saves new account settings. |
122
|
|
|
* |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
|
public function save() |
126
|
|
|
{ |
127
|
|
|
if ($this->validate()) { |
128
|
|
|
$user = $this->getUser(); |
129
|
|
|
if ($user instanceof User) { |
130
|
|
|
$user->scenario = 'settings'; |
131
|
|
|
$user->username = $this->username; |
132
|
|
|
$user->password = $this->new_password; |
133
|
|
|
if ($this->email == $user->email && $user->unconfirmed_email != null) { |
134
|
|
|
$user->unconfirmed_email = null; |
135
|
|
|
} elseif ($this->email != $user->email) { |
136
|
|
|
$strategy = EmailChangeStrategyFactory::makeByStrategyType( |
137
|
|
|
$this->getModule()->emailChangeStrategy, |
138
|
|
|
$this |
139
|
|
|
); |
140
|
|
|
|
141
|
|
|
return $strategy->run(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $user->save(); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return false; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.