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
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace FSi\Bundle\AdminSecurityBundle\Controller\PasswordReset; |
13
|
|
|
|
14
|
|
|
use FSi\Bundle\AdminBundle\Message\FlashMessages; |
15
|
|
|
use FSi\Bundle\AdminSecurityBundle\Event\AdminSecurityEvents; |
16
|
|
|
use FSi\Bundle\AdminSecurityBundle\Event\ChangePasswordEvent; |
17
|
|
|
use FSi\Bundle\AdminSecurityBundle\Security\User\ResettablePasswordInterface; |
18
|
|
|
use FSi\Bundle\AdminSecurityBundle\Security\User\UserRepositoryInterface; |
19
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
20
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
21
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
22
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
23
|
|
|
use Symfony\Component\HttpFoundation\Request; |
24
|
|
|
use Symfony\Component\HttpFoundation\Response; |
25
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
26
|
|
|
use Symfony\Component\Routing\RouterInterface; |
27
|
|
|
|
28
|
|
|
class ChangePasswordController |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var EngineInterface |
32
|
|
|
*/ |
33
|
|
|
private $templating; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $changePasswordActionTemplate; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var UserRepositoryInterface |
42
|
|
|
*/ |
43
|
|
|
private $userRepository; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var RouterInterface |
47
|
|
|
*/ |
48
|
|
|
private $router; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var FormFactoryInterface |
52
|
|
|
*/ |
53
|
|
|
private $formFactory; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var EventDispatcherInterface |
57
|
|
|
*/ |
58
|
|
|
private $eventDispatcher; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var FlashMessages |
62
|
|
|
*/ |
63
|
|
|
private $flashMessages; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var string |
67
|
|
|
*/ |
68
|
|
|
private $formType; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var array |
72
|
|
|
*/ |
73
|
|
|
private $formValidationGroups; |
74
|
|
|
|
75
|
|
|
public function __construct( |
76
|
|
|
EngineInterface $templating, |
77
|
|
|
$changePasswordActionTemplate, |
78
|
|
|
UserRepositoryInterface $userRepository, |
79
|
|
|
RouterInterface $router, |
80
|
|
|
FormFactoryInterface $formFactory, |
81
|
|
|
EventDispatcherInterface $eventDispatcher, |
82
|
|
|
FlashMessages $flashMessages, |
83
|
|
|
$formType, |
84
|
|
|
array $formValidationGroups |
85
|
|
|
) { |
86
|
|
|
$this->templating = $templating; |
87
|
|
|
$this->changePasswordActionTemplate = $changePasswordActionTemplate; |
88
|
|
|
$this->userRepository = $userRepository; |
89
|
|
|
$this->router = $router; |
90
|
|
|
$this->formFactory = $formFactory; |
91
|
|
|
$this->eventDispatcher = $eventDispatcher; |
92
|
|
|
$this->flashMessages = $flashMessages; |
93
|
|
|
$this->formType = $formType; |
94
|
|
|
$this->formValidationGroups = $formValidationGroups; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function changePasswordAction(Request $request, string $token): Response |
98
|
|
|
{ |
99
|
|
|
$user = $this->userRepository->findUserByPasswordResetToken($token); |
100
|
|
|
|
101
|
|
|
if (null === $user) { |
102
|
|
|
throw new NotFoundHttpException(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (!$user->getPasswordResetToken()->isNonExpired()) { |
106
|
|
|
throw new NotFoundHttpException(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$form = $this->formFactory->create( |
110
|
|
|
$this->formType, |
111
|
|
|
$user, |
112
|
|
|
['validation_groups' => $this->formValidationGroups] |
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
if ($form->handleRequest($request)->isSubmitted() && $form->isValid()) { |
116
|
|
|
$user->removePasswordResetToken(); |
117
|
|
|
|
118
|
|
|
$this->eventDispatcher->dispatch( |
119
|
|
|
AdminSecurityEvents::CHANGE_PASSWORD, |
|
|
|
|
120
|
|
|
new ChangePasswordEvent($user) |
|
|
|
|
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
$this->flashMessages->success( |
124
|
|
|
'admin.password_reset.change_password.message.success', |
125
|
|
|
[], |
126
|
|
|
'FSiAdminSecurity' |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
return new RedirectResponse($this->router->generate('fsi_admin_security_user_login')); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this->templating->renderResponse( |
133
|
|
|
$this->changePasswordActionTemplate, |
134
|
|
|
['form' => $form->createView()] |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|