|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* (c) FSi sp. z o.o. <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace FSi\Bundle\AdminSecurityBundle\Controller\PasswordReset; |
|
11
|
|
|
|
|
12
|
|
|
use FSi\Bundle\AdminBundle\Message\FlashMessages; |
|
13
|
|
|
use FSi\Bundle\AdminSecurityBundle\Event\AdminSecurityEvents; |
|
14
|
|
|
use FSi\Bundle\AdminSecurityBundle\Event\ResetPasswordRequestEvent; |
|
15
|
|
|
use FSi\Bundle\AdminSecurityBundle\Security\User\ResettablePasswordInterface; |
|
16
|
|
|
use FSi\Bundle\AdminSecurityBundle\Security\User\UserRepositoryInterface; |
|
17
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
|
18
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
19
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
|
20
|
|
|
use Symfony\Component\Form\FormInterface; |
|
21
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
22
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
23
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
24
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
25
|
|
|
use Symfony\Component\Security\Core\User\AdvancedUserInterface; |
|
26
|
|
|
|
|
27
|
|
|
class ResetRequestController |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var EngineInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $templating; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
private $requestActionTemplate; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var FormFactoryInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
private $formFactory; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var RouterInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
private $router; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var UserRepositoryInterface |
|
51
|
|
|
*/ |
|
52
|
|
|
private $userRepository; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var EventDispatcherInterface |
|
56
|
|
|
*/ |
|
57
|
|
|
private $eventDispatcher; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var FlashMessages |
|
61
|
|
|
*/ |
|
62
|
|
|
private $flashMessages; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var string |
|
66
|
|
|
*/ |
|
67
|
|
|
private $formType; |
|
68
|
|
|
|
|
69
|
|
|
public function __construct( |
|
70
|
|
|
EngineInterface $templating, |
|
71
|
|
|
$requestActionTemplate, |
|
72
|
|
|
FormFactoryInterface $formFactory, |
|
73
|
|
|
RouterInterface $router, |
|
74
|
|
|
UserRepositoryInterface $userRepository, |
|
75
|
|
|
EventDispatcherInterface $eventDispatcher, |
|
76
|
|
|
FlashMessages $flashMessages, |
|
77
|
|
|
$formType |
|
78
|
|
|
) { |
|
79
|
|
|
$this->templating = $templating; |
|
80
|
|
|
$this->requestActionTemplate = $requestActionTemplate; |
|
81
|
|
|
$this->formFactory = $formFactory; |
|
82
|
|
|
$this->router = $router; |
|
83
|
|
|
$this->userRepository = $userRepository; |
|
84
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
85
|
|
|
$this->flashMessages = $flashMessages; |
|
86
|
|
|
$this->formType = $formType; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param Request $request |
|
91
|
|
|
* @return Response |
|
92
|
|
|
*/ |
|
93
|
|
|
public function requestAction(Request $request) |
|
94
|
|
|
{ |
|
95
|
|
|
$form = $this->formFactory->create($this->formType); |
|
96
|
|
|
|
|
97
|
|
|
if ($form->handleRequest($request)->isSubmitted() && $form->isValid()) { |
|
98
|
|
|
$user = $this->getUser($form); |
|
99
|
|
|
$redirectResponse = $this->addFlashAndRedirect('info', 'admin.password_reset.request.mail_sent_if_correct'); |
|
100
|
|
|
|
|
101
|
|
|
if (!($user instanceof ResettablePasswordInterface)) { |
|
102
|
|
|
return $redirectResponse; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
if (!$user->isEnabled()) { |
|
106
|
|
|
return $redirectResponse; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if ($this->hasNonExpiredPasswordResetToken($user)) { |
|
110
|
|
|
return $redirectResponse; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if (($user instanceof AdvancedUserInterface) && !$user->isAccountNonLocked()) { |
|
114
|
|
|
return $redirectResponse; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$this->eventDispatcher->dispatch( |
|
118
|
|
|
AdminSecurityEvents::RESET_PASSWORD_REQUEST, |
|
119
|
|
|
new ResetPasswordRequestEvent($user) |
|
120
|
|
|
); |
|
121
|
|
|
|
|
122
|
|
|
return $redirectResponse; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $this->templating->renderResponse( |
|
126
|
|
|
$this->requestActionTemplate, |
|
127
|
|
|
['form' => $form->createView()] |
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param string $type |
|
133
|
|
|
* @param string $message |
|
134
|
|
|
* @return RedirectResponse |
|
135
|
|
|
*/ |
|
136
|
|
|
private function addFlashAndRedirect($type, $message) |
|
137
|
|
|
{ |
|
138
|
|
|
$this->flashMessages->{$type}($message, 'FSiAdminSecurity'); |
|
139
|
|
|
|
|
140
|
|
|
return new RedirectResponse($this->router->generate('fsi_admin_security_user_login')); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param FormInterface $form |
|
145
|
|
|
* @return ResettablePasswordInterface|null |
|
146
|
|
|
*/ |
|
147
|
|
|
private function getUser(FormInterface $form) |
|
148
|
|
|
{ |
|
149
|
|
|
return $this->userRepository->findUserByEmail($form->get('email')->getData()); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param ResettablePasswordInterface $user |
|
154
|
|
|
* @return bool |
|
155
|
|
|
*/ |
|
156
|
|
|
private function hasNonExpiredPasswordResetToken(ResettablePasswordInterface $user) |
|
157
|
|
|
{ |
|
158
|
|
|
return $user->getPasswordResetToken() && $user->getPasswordResetToken()->isNonExpired(); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|