-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChangePasswordFormTrait.php
More file actions
120 lines (96 loc) · 4.04 KB
/
Copy pathChangePasswordFormTrait.php
File metadata and controls
120 lines (96 loc) · 4.04 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
declare(strict_types=1);
namespace ADT\FancyAdmin\UI\Components\Forms\ChangePassword;
use ADT\FancyAdmin\DI\Injects\AuthenticatorInject;
use ADT\FancyAdmin\DI\Injects\BreachedPasswordCheckerInject;
use ADT\FancyAdmin\DI\Injects\ConfigurationQueryFactoryInject;
use ADT\FancyAdmin\DI\Injects\EntityManagerInject;
use ADT\FancyAdmin\DI\Injects\FancyAdminInject;
use ADT\FancyAdmin\DI\Injects\SecurityUserInject;
use ADT\FancyAdmin\Model\Entities\Identity;
use ADT\Forms\Form;
trait ChangePasswordFormTrait
{
use AuthenticatorInject;
use BreachedPasswordCheckerInject;
use ConfigurationQueryFactoryInject;
use FancyAdminInject;
use SecurityUserInject;
use EntityManagerInject;
public function initForm(Form $form): void
{
$form->addPassword('currentPassword', 'fcadmin.forms.changePassword.labels.currentPassword')
->setRequired('fcadmin.forms.changePassword.errors.required');
$form->addPassword('password', 'fcadmin.forms.changePassword.labels.password')
->setRequired('fcadmin.forms.changePassword.errors.required')
->addRule($form::MinLength, 'fcadmin.forms.newPassword.errors.minLength', 8);
$form->addPassword('passwordRepeat', 'fcadmin.forms.changePassword.labels.passwordRepeat')
->setRequired('fcadmin.forms.changePassword.errors.required');
$form->addSubmit('submit', 'fcadmin.forms.changePassword.labels.submit');
}
public function validateForm(array $values, Form $form): void
{
/** @var Identity $identity */
$identity = $this->getEntity();
if (!password_verify($values['currentPassword'], $identity->getPassword())) {
$form->getComponentTextInput('currentPassword')->addError('fcadmin.forms.changePassword.errors.wrongPassword');
}
if ($values['password'] !== $values['passwordRepeat']) {
$form->getComponentTextInput('passwordRepeat')->addError('fcadmin.forms.newPassword.errors.noMatch');
}
if ($this->_breachedPasswordChecker->isBreached($values['password'])) {
$form->getComponentTextInput('password')->addError('fcadmin.forms.newPassword.errors.breachedPassword');
}
$this->validatePasswordPolicy($values['password'], $form);
}
private function validatePasswordPolicy(string $password, Form $form): void
{
/** @var Identity $identity */
$identity = $this->getEntity();
if ($identity->isAdmin()) {
$policyKey = 'password.policy.admin';
} elseif ($identity->isAllowed($this->_fancyAdmin->getBackofficeAclResource())) {
$policyKey = 'password.policy.backoffice';
} else {
return;
}
$config = $this->_configurationQueryFactory->create()->byKey($policyKey)->fetchOneOrNull();
if (!$config) {
return;
}
$policy = json_decode($config->getValue(), true);
if (!($policy['enabled'] ?? false)) {
return;
}
$field = $form->getComponentTextInput('password');
if (mb_strlen($password) < ($policy['minLength'] ?? 8)) {
$field->addError($this->getTranslator()->translate('fcadmin.forms.newPassword.errors.minLength', $policy['minLength']));
}
if (($policy['requireUppercase'] ?? false) && !preg_match('/[A-Z]/', $password)) {
$field->addError('fcadmin.forms.newPassword.errors.requireUppercase');
}
if (($policy['requireLowercase'] ?? false) && !preg_match('/[a-z]/', $password)) {
$field->addError('fcadmin.forms.newPassword.errors.requireLowercase');
}
if (($policy['requireDigit'] ?? false) && !preg_match('/\d/', $password)) {
$field->addError('fcadmin.forms.newPassword.errors.requireDigit');
}
if (($policy['requireSpecialChar'] ?? false) && !preg_match('/[^a-zA-Z0-9]/', $password)) {
$field->addError('fcadmin.forms.newPassword.errors.requireSpecialChar');
}
}
public function processForm(array $values): void
{
/** @var Identity $identity */
$identity = $this->getEntity();
$identity->setPassword($values['password']);
$this->_em->flush();
$this->_authenticator->clearIdentity($identity->getAuthObjectId());
$this->_securityUser->logout(true);
$this->_securityUser->login($identity, context: $this->_fancyAdmin->getContext());
}
public function getEntityClass(): ?string
{
return $this->_em->findEntityClassByInterface(Identity::class);
}
}