1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\AdminBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Kunstmaan\AdminBundle\Event\ChangePasswordSuccessEvent; |
7
|
|
|
use Kunstmaan\AdminBundle\Event\Events; |
8
|
|
|
use Kunstmaan\AdminBundle\Form\NewPasswordType; |
9
|
|
|
use Kunstmaan\AdminBundle\Form\PasswordRequestType; |
10
|
|
|
use Kunstmaan\AdminBundle\Service\PasswordMailerInterface; |
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
12
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
13
|
|
|
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy; |
14
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
17
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
18
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
19
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
20
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
21
|
|
|
|
22
|
|
|
class ResettingController extends AbstractController |
23
|
|
|
{ |
24
|
|
|
/** @var string */ |
25
|
|
|
private $userClass; |
26
|
|
|
|
27
|
|
|
/** @var PasswordMailerInterface */ |
28
|
|
|
private $passwordMailer; |
29
|
|
|
|
30
|
|
|
/** @var EventDispatcherInterface */ |
31
|
|
|
private $eventDispatcher; |
32
|
|
|
|
33
|
|
|
/** @var EntityManagerInterface */ |
34
|
|
|
private $em; |
35
|
|
|
|
36
|
|
|
/** @var UserPasswordEncoderInterface */ |
37
|
|
|
private $encoder; |
38
|
|
|
|
39
|
|
|
/** @var TokenStorageInterface */ |
40
|
|
|
private $tokenStorage; |
41
|
|
|
|
42
|
|
|
/** @var SessionInterface */ |
43
|
|
|
private $session; |
44
|
|
|
|
45
|
|
|
public function __construct( |
46
|
|
|
string $userClass, |
47
|
|
|
PasswordMailerInterface $passwordMailer, |
48
|
|
|
EventDispatcherInterface $eventDispatcher, |
49
|
|
|
EntityManagerInterface $em, |
50
|
|
|
UserPasswordEncoderInterface $encoder, |
51
|
|
|
TokenStorageInterface $tokenStorage, |
52
|
|
|
SessionInterface $session |
53
|
|
|
) { |
54
|
|
|
$this->userClass = $userClass; |
55
|
|
|
$this->passwordMailer = $passwordMailer; |
56
|
|
|
$this->eventDispatcher = $eventDispatcher; |
57
|
|
|
$this->em = $em; |
58
|
|
|
$this->encoder = $encoder; |
59
|
|
|
$this->tokenStorage = $tokenStorage; |
60
|
|
|
$this->session = $session; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @Route("/resetting/request", name="cms_reset_password", methods={"GET", "POST"}) |
65
|
|
|
* @Route("/resetting/request", name="fos_user_resetting_request", methods={"GET", "POST"}) |
66
|
|
|
*/ |
67
|
|
|
public function resetPasswordAction(Request $request) |
68
|
|
|
{ |
69
|
|
|
$form = $this->createForm(PasswordRequestType::class); |
70
|
|
|
$form->handleRequest($request); |
71
|
|
|
|
72
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
73
|
|
|
$email = $form->get('email')->getData(); |
74
|
|
|
$token = bin2hex(random_bytes(32)); |
75
|
|
|
$user = $this->em->getRepository($this->userClass)->findOneBy(['email' => $email]); |
76
|
|
View Code Duplication |
if (!$user instanceof $this->userClass) { |
|
|
|
|
77
|
|
|
$user = $this->em->getRepository($this->userClass)->findOneBy(['username' => $email]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ($user instanceof $this->userClass) { |
81
|
|
|
$user->setConfirmationToken($token); |
82
|
|
|
$this->em->flush(); |
83
|
|
|
$this->passwordMailer->sendPasswordForgotMail($user, $request->getLocale()); |
84
|
|
|
$this->addFlash('success', 'security.resetting.send_email_success'); |
85
|
|
|
|
86
|
|
|
return $this->redirectToRoute('cms_reset_password'); |
87
|
|
|
} else { |
88
|
|
|
$this->addFlash('danger', 'security.resetting.send_email_failure'); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this->render('@KunstmaanAdmin/Security/reset-password.html.twig', ['form' => $form->createView()]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @Route("/reset_password/confirm/{token}", name="cms_reset_password_confirm", methods={"GET", "POST"}) |
97
|
|
|
*/ |
98
|
|
|
public function resetPasswordCheckAction( |
99
|
|
|
Request $request, |
100
|
|
|
string $token |
101
|
|
|
) { |
102
|
|
|
$user = $this->em->getRepository($this->userClass)->findOneBy(['confirmationToken' => $token]); |
103
|
|
|
|
104
|
|
|
if (!$token || !$user instanceof $this->userClass) { |
105
|
|
|
$this->addFlash('danger', 'security.resetting.user_not_found'); |
106
|
|
|
|
107
|
|
|
return $this->redirectToRoute('cms_reset_password'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$form = $this->createForm(NewPasswordType::class); |
111
|
|
|
$form->handleRequest($request); |
112
|
|
|
|
113
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
114
|
|
|
$plainPassword = $form->get('plainPassword')->getData(); |
115
|
|
|
$password = $this->encoder->encodePassword($user, $plainPassword); |
116
|
|
|
$user->setPassword($password); |
117
|
|
|
$user->setConfirmationToken(null); |
118
|
|
|
$this->em->flush(); |
119
|
|
|
|
120
|
|
|
$token = new UsernamePasswordToken($user, $password, 'main'); |
121
|
|
|
$this->tokenStorage->setToken($token); |
122
|
|
|
$this->session->set('_security_main', serialize($token)); |
123
|
|
|
|
124
|
|
|
$url = $this->generateUrl('KunstmaanAdminBundle_homepage'); |
125
|
|
|
$response = new RedirectResponse($url); |
126
|
|
|
|
127
|
|
|
$this->dispatch(new ChangePasswordSuccessEvent($user, $request, $response), Events::CHANGE_PASSWORD_COMPLETED); |
128
|
|
|
|
129
|
|
|
$this->addFlash('success', 'security.resetting.password_set_success'); |
130
|
|
|
|
131
|
|
|
return $response; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this->render('@KunstmaanAdmin/Security/reset-password-confirm.html.twig', ['form' => $form->createView()]); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param object $event |
139
|
|
|
* @param string $eventName |
140
|
|
|
* |
141
|
|
|
* @return object |
142
|
|
|
*/ |
143
|
|
View Code Duplication |
private function dispatch($event, string $eventName) |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
if (class_exists(LegacyEventDispatcherProxy::class)) { |
146
|
|
|
$eventDispatcher = LegacyEventDispatcherProxy::decorate($this->eventDispatcher); |
147
|
|
|
|
148
|
|
|
return $eventDispatcher->dispatch($event, $eventName); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $this->eventDispatcher->dispatch($eventName, $event); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.