1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Frontend\Modules\Profiles\Actions; |
4
|
|
|
|
5
|
|
|
use Frontend\Core\Engine\Base\Block as FrontendBaseBlock; |
6
|
|
|
use Frontend\Core\Engine\Form as FrontendForm; |
7
|
|
|
use Frontend\Core\Language\Language as FL; |
8
|
|
|
use Frontend\Core\Engine\Navigation as FrontendNavigation; |
9
|
|
|
use Frontend\Modules\Profiles\Engine\Authentication as FrontendProfilesAuthentication; |
10
|
|
|
use Frontend\Modules\Profiles\Engine\Profile; |
11
|
|
|
use Symfony\Component\Security\Core\Exception\InsufficientAuthenticationException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Change the password of the current logged in profile. |
15
|
|
|
*/ |
16
|
|
|
class ChangePassword extends FrontendBaseBlock |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var FrontendForm |
20
|
|
|
*/ |
21
|
|
|
private $form; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The current profile. |
25
|
|
|
* |
26
|
|
|
* @var Profile |
27
|
|
|
*/ |
28
|
|
|
private $profile; |
29
|
|
|
|
30
|
|
|
public function execute(): void |
31
|
|
|
{ |
32
|
|
|
if (!FrontendProfilesAuthentication::isLoggedIn()) { |
33
|
|
|
throw new InsufficientAuthenticationException('You need to log in to change your email'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
parent::execute(); |
37
|
|
|
$this->getData(); |
38
|
|
|
$this->loadTemplate(); |
39
|
|
|
$this->buildForm(); |
40
|
|
|
$this->handleForm(); |
41
|
|
|
$this->parse(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
private function getData(): void |
45
|
|
|
{ |
46
|
|
|
$this->profile = FrontendProfilesAuthentication::getProfile(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private function buildForm(): void |
50
|
|
|
{ |
51
|
|
|
$this->form = new FrontendForm('updatePassword', null, null, 'updatePasswordForm'); |
52
|
|
|
$this->form |
53
|
|
|
->addPassword('old_password') |
54
|
|
|
->setAttribute('autocomplete', 'current-password') |
55
|
|
|
->makeRequired() |
56
|
|
|
; |
57
|
|
|
$this->form |
58
|
|
|
->addPassword('new_password') |
59
|
|
|
->setAttribute('data-role', 'fork-new-password') |
60
|
|
|
->setAttribute('autocomplete', 'new-password') |
61
|
|
|
->makeRequired() |
62
|
|
|
; |
63
|
|
|
$this->form |
64
|
|
|
->addPassword('verify_new_password') |
65
|
|
|
->setAttribute('data-role', 'fork-new-password') |
66
|
|
|
->setAttribute('autocomplete', 'new-password') |
67
|
|
|
->makeRequired() |
68
|
|
|
; |
69
|
|
|
$this->form->addCheckbox('show_password')->setAttribute('data-role', 'fork-toggle-visible-password'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function parse(): void |
73
|
|
|
{ |
74
|
|
|
// show the success message when the password was changed |
75
|
|
|
$this->template->assign('updatePasswordSuccess', $this->url->getParameter('changedPassword') === 'true'); |
76
|
|
|
$this->form->parse($this->template); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function isValidLoginCredentials(string $email, string $password): bool |
80
|
|
|
{ |
81
|
|
|
$loginStatus = FrontendProfilesAuthentication::getLoginStatus($email, $password); |
82
|
|
|
|
83
|
|
|
return $loginStatus === FrontendProfilesAuthentication::LOGIN_ACTIVE; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function validateForm(): bool |
87
|
|
|
{ |
88
|
|
|
$txtOldPassword = $this->form->getField('old_password'); |
89
|
|
|
$txtNewPassword = $this->form->getField('new_password'); |
90
|
|
|
|
91
|
|
|
if (!$txtOldPassword->isFilled(FL::getError('PasswordIsRequired'))) { |
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (!$this->isValidLoginCredentials($this->profile->getEmail(), $txtOldPassword->getValue())) { |
96
|
|
|
$txtOldPassword->addError(FL::getError('InvalidPassword')); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ($txtNewPassword->isFilled(FL::getError('PasswordIsRequired')) |
100
|
|
|
&& $txtNewPassword->getValue() !== $this->form->getField('verify_new_password')->getValue()) { |
101
|
|
|
$this->form->getField('verify_new_password')->addError(FL::err('PasswordsDontMatch')); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->form->isCorrect(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
private function handleForm(): void |
108
|
|
|
{ |
109
|
|
|
if (!$this->form->isSubmitted()) { |
110
|
|
|
return; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if (!$this->validateForm()) { |
114
|
|
|
$this->template->assign('updatePasswordHasFormError', true); |
115
|
|
|
|
116
|
|
|
return; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
FrontendProfilesAuthentication::updatePassword( |
120
|
|
|
$this->profile->getId(), |
121
|
|
|
$this->form->getField('new_password')->getValue() |
122
|
|
|
); |
123
|
|
|
|
124
|
|
|
$this->redirect( |
125
|
|
|
FrontendNavigation::getUrlForBlock('Profiles', 'ChangePassword') . '?changedPassword=true' |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|