1 | <?php |
||
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() |
|
107 | |||
108 | /** |
||
109 | * @return User|null|\yii\web\IdentityInterface |
||
110 | */ |
||
111 | 1 | public function getUser() |
|
119 | |||
120 | /** |
||
121 | * Saves new account settings. |
||
122 | * |
||
123 | * @return bool |
||
124 | */ |
||
125 | public function save() |
||
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.