|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SixtyEightPublishers\User\ForgotPassword\Control\ResetPassword; |
|
6
|
|
|
|
|
7
|
|
|
use Throwable; |
|
8
|
|
|
use Nette\Security\User; |
|
9
|
|
|
use Nette\Application\UI\Form; |
|
10
|
|
|
use SixtyEightPublishers\SmartNetteComponent\UI\Control; |
|
11
|
|
|
use SixtyEightPublishers\User\Common\Logger\LoggerInterface; |
|
12
|
|
|
use SixtyEightPublishers\TranslationBridge\TranslatorAwareTrait; |
|
13
|
|
|
use SixtyEightPublishers\TranslationBridge\TranslatorAwareInterface; |
|
14
|
|
|
use SixtyEightPublishers\User\ForgotPassword\Entity\PasswordRequestInterface; |
|
15
|
|
|
use SixtyEightPublishers\User\ForgotPassword\Mail\PasswordHasBeenResetEmailInterface; |
|
16
|
|
|
use SixtyEightPublishers\User\ForgotPassword\Exception\PasswordRequestProcessException; |
|
17
|
|
|
use SixtyEightPublishers\User\ForgotPassword\PasswordRequest\PasswordRequestManagerInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @method void onSuccess(PasswordRequestInterface $request) |
|
21
|
|
|
* @method void onError(PasswordRequestInterface $request, PasswordRequestProcessException $e) |
|
22
|
|
|
* @method void onFormCreation(Form $form) |
|
23
|
|
|
*/ |
|
24
|
|
|
class ResetPasswordControl extends Control implements TranslatorAwareInterface |
|
25
|
|
|
{ |
|
26
|
|
|
use TranslatorAwareTrait; |
|
27
|
|
|
|
|
28
|
|
|
/** @var \SixtyEightPublishers\User\ForgotPassword\Entity\PasswordRequestInterface */ |
|
29
|
|
|
private $passwordRequest; |
|
30
|
|
|
|
|
31
|
|
|
/** @var \SixtyEightPublishers\User\Common\Logger\LoggerInterface */ |
|
32
|
|
|
private $logger; |
|
33
|
|
|
|
|
34
|
|
|
/** @var \SixtyEightPublishers\User\ForgotPassword\Mail\PasswordHasBeenResetEmailInterface */ |
|
35
|
|
|
private $passwordHasBeenResetEmail; |
|
36
|
|
|
|
|
37
|
|
|
/** @var \SixtyEightPublishers\User\ForgotPassword\PasswordRequest\PasswordRequestManagerInterface */ |
|
38
|
|
|
private $passwordRequestManager; |
|
39
|
|
|
|
|
40
|
|
|
/** @var \Nette\Security\User */ |
|
41
|
|
|
private $user; |
|
42
|
|
|
|
|
43
|
|
|
/** @var bool */ |
|
44
|
|
|
private $autoLogin = FALSE; |
|
45
|
|
|
|
|
46
|
|
|
/** @var callable[] */ |
|
47
|
|
|
public $onSuccess = []; |
|
48
|
|
|
|
|
49
|
|
|
/** @var callable[] */ |
|
50
|
|
|
public $onError = []; |
|
51
|
|
|
|
|
52
|
|
|
/** @var callable[] */ |
|
53
|
|
|
public $onFormCreation = []; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param \SixtyEightPublishers\User\ForgotPassword\Entity\PasswordRequestInterface $passwordRequest |
|
57
|
|
|
* @param \SixtyEightPublishers\User\Common\Logger\LoggerInterface $logger |
|
58
|
|
|
* @param \SixtyEightPublishers\User\ForgotPassword\Mail\PasswordHasBeenResetEmailInterface $passwordHasBeenResetEmail |
|
59
|
|
|
* @param \SixtyEightPublishers\User\ForgotPassword\PasswordRequest\PasswordRequestManagerInterface $passwordRequestManager |
|
60
|
|
|
* @param \Nette\Security\User $user |
|
61
|
|
|
*/ |
|
62
|
|
|
public function __construct( |
|
63
|
|
|
PasswordRequestInterface $passwordRequest, |
|
64
|
|
|
LoggerInterface $logger, |
|
65
|
|
|
PasswordHasBeenResetEmailInterface $passwordHasBeenResetEmail, |
|
66
|
|
|
PasswordRequestManagerInterface $passwordRequestManager, |
|
67
|
|
|
User $user |
|
68
|
|
|
) { |
|
69
|
|
|
$this->passwordRequest = $passwordRequest; |
|
70
|
|
|
$this->logger = $logger; |
|
71
|
|
|
$this->passwordHasBeenResetEmail = $passwordHasBeenResetEmail; |
|
72
|
|
|
$this->passwordRequestManager = $passwordRequestManager; |
|
73
|
|
|
$this->user = $user; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param bool $autoLogin |
|
78
|
|
|
* |
|
79
|
|
|
* @return void |
|
80
|
|
|
*/ |
|
81
|
|
|
public function setAutoLogin(bool $autoLogin): void |
|
82
|
|
|
{ |
|
83
|
|
|
$this->autoLogin = $autoLogin; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return void |
|
88
|
|
|
*/ |
|
89
|
|
|
public function render(): void |
|
90
|
|
|
{ |
|
91
|
|
|
$this->template->setTranslator($this->getPrefixedTranslator()); |
|
|
|
|
|
|
92
|
|
|
$this->doRender(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return \Nette\Application\UI\Form |
|
97
|
|
|
*/ |
|
98
|
|
View Code Duplication |
protected function createComponentForm(): Form |
|
|
|
|
|
|
99
|
|
|
{ |
|
100
|
|
|
$form = new Form(); |
|
101
|
|
|
|
|
102
|
|
|
$form->setTranslator($this->getPrefixedTranslator()); |
|
103
|
|
|
|
|
104
|
|
|
$form->addPassword('password', 'password.field') |
|
105
|
|
|
->setRequired('password.required') |
|
106
|
|
|
->setHtmlAttribute('autocomplete', 'new-password'); |
|
107
|
|
|
|
|
108
|
|
|
$form->addProtection('protection.rule'); |
|
109
|
|
|
|
|
110
|
|
|
$form->onSuccess[] = [$this, 'processForm']; |
|
111
|
|
|
|
|
112
|
|
|
$this->onFormCreation($form); |
|
113
|
|
|
|
|
114
|
|
|
$form->addSubmit('send', 'send.field'); |
|
115
|
|
|
|
|
116
|
|
|
return $form; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @internal |
|
121
|
|
|
* |
|
122
|
|
|
* @param \Nette\Application\UI\Form $form |
|
123
|
|
|
* |
|
124
|
|
|
* @return void |
|
125
|
|
|
* @throws \Nette\Security\AuthenticationException |
|
126
|
|
|
*/ |
|
127
|
|
|
public function processForm(Form $form): void |
|
128
|
|
|
{ |
|
129
|
|
|
try { |
|
130
|
|
|
$request = $this->passwordRequest; |
|
131
|
|
|
$password = $form->values->password; |
|
132
|
|
|
|
|
133
|
|
|
$this->passwordRequestManager->reset($request, $password); |
|
134
|
|
|
|
|
135
|
|
|
try { |
|
136
|
|
|
$this->passwordHasBeenResetEmail->send($request->getUser()); |
|
137
|
|
|
$this->logger->info(sprintf( |
|
138
|
|
|
'Mail %s was successfully sent to %s', |
|
139
|
|
|
get_class($this->passwordHasBeenResetEmail), |
|
140
|
|
|
$request->getUser()->getEmail() |
|
141
|
|
|
)); |
|
142
|
|
|
} catch (Throwable $e) { |
|
143
|
|
|
$this->logger->error((string) $e); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
if ($this->autoLogin) { |
|
147
|
|
|
$this->user->login($request->getUser()->getUsername(), $password); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
$this->onSuccess($this->passwordRequest); |
|
151
|
|
|
} catch (PasswordRequestProcessException $e) { |
|
152
|
|
|
$this->onError($this->passwordRequest, $e); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.