|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Obblm\Core\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
6
|
|
|
use Obblm\Core\Entity\Coach; |
|
7
|
|
|
use Obblm\Core\Event\ActivateCoachEvent; |
|
8
|
|
|
use Obblm\Core\Event\RegisterCoachEvent; |
|
9
|
|
|
use Obblm\Core\Event\ResetPasswordCoachEvent; |
|
10
|
|
|
use Obblm\Core\Form\Security\ForgotPasswordForm; |
|
11
|
|
|
use Obblm\Core\Form\Security\PasswordConfirmType; |
|
12
|
|
|
use Obblm\Core\Form\Security\RegistrationForm; |
|
13
|
|
|
use LogicException; |
|
14
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
15
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
18
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
19
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
|
20
|
|
|
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @Route("/", name="obblm_") |
|
24
|
|
|
*/ |
|
25
|
|
|
class SecurityController extends AbstractController |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @Route("/login", name="login") |
|
29
|
|
|
*/ |
|
30
|
|
|
public function login(AuthenticationUtils $authenticationUtils, Request $request): Response |
|
|
|
|
|
|
31
|
|
|
{ |
|
32
|
|
|
if ($this->getUser()) { |
|
33
|
|
|
return $this->redirectToRoute('obblm_dashboard'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
// get the login error if there is one |
|
37
|
|
|
$error = $authenticationUtils->getLastAuthenticationError(); |
|
38
|
|
|
// last username entered by the user |
|
39
|
|
|
$lastUsername = $authenticationUtils->getLastUsername(); |
|
40
|
|
|
|
|
41
|
|
|
return $this->render('@ObblmCore/security/login.html.twig', [ |
|
42
|
|
|
'last_username' => $lastUsername, |
|
43
|
|
|
'error' => $error, |
|
44
|
|
|
]); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @Route("/logout", name="logout") |
|
49
|
|
|
*/ |
|
50
|
|
|
public function logout() |
|
51
|
|
|
{ |
|
52
|
|
|
throw new LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @Route("/register", name="register") |
|
57
|
|
|
*/ |
|
58
|
|
|
public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder, EventDispatcherInterface $dispatcher):Response |
|
59
|
|
|
{ |
|
60
|
|
|
$coach = new Coach(); |
|
61
|
|
|
$form = $this->createForm(RegistrationForm::class, $coach); |
|
62
|
|
|
|
|
63
|
|
|
$form->handleRequest($request); |
|
64
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
65
|
|
|
$password = $passwordEncoder->encodePassword($coach, $coach->getPlainPassword()); |
|
|
|
|
|
|
66
|
|
|
$coach->setPassword($password) |
|
67
|
|
|
->setHash($this->getHashFor($coach->getEmail())); |
|
68
|
|
|
|
|
69
|
|
|
$entityManager = $this->getDoctrine()->getManager(); |
|
70
|
|
|
$entityManager->persist($coach); |
|
71
|
|
|
|
|
72
|
|
|
$event = new RegisterCoachEvent($coach); |
|
73
|
|
|
$dispatcher->dispatch($event, RegisterCoachEvent::NAME); |
|
74
|
|
|
|
|
75
|
|
|
$entityManager->flush(); |
|
76
|
|
|
|
|
77
|
|
|
$this->addFlash( |
|
78
|
|
|
'success', |
|
79
|
|
|
'obblm.flash.account.created' |
|
80
|
|
|
); |
|
81
|
|
|
return $this->redirectToRoute('obblm_login'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $this->render('@ObblmCore/security/register.html.twig', [ |
|
85
|
|
|
'form' => $form->createView(), |
|
86
|
|
|
]); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @Route("/activate/{hash}", name="activate_account") |
|
91
|
|
|
*/ |
|
92
|
|
|
public function activate(string $hash, EventDispatcherInterface $dispatcher):Response |
|
93
|
|
|
{ |
|
94
|
|
|
$entityManager = $this->getDoctrine()->getManager(); |
|
95
|
|
|
|
|
96
|
|
|
/** @var ?Coach $coach */ |
|
97
|
|
|
$coach = $entityManager->getRepository(Coach::class)->findOneByHash($hash); |
|
98
|
|
|
|
|
99
|
|
|
if (!$coach) { |
|
100
|
|
|
$this->addFlash( |
|
101
|
|
|
'error', |
|
102
|
|
|
'obblm.flash.account.not_found_by_hash' |
|
103
|
|
|
); |
|
104
|
|
|
|
|
105
|
|
|
return $this->redirectToRoute('obblm_login'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if (!$coach->isActive()) { |
|
109
|
|
|
$coach->setHash(null)->setActive(true); |
|
110
|
|
|
$entityManager->persist($coach); |
|
111
|
|
|
|
|
112
|
|
|
$event = new ActivateCoachEvent($coach); |
|
113
|
|
|
$dispatcher->dispatch($event, ActivateCoachEvent::NAME); |
|
114
|
|
|
$entityManager->flush(); |
|
115
|
|
|
|
|
116
|
|
|
$this->addFlash( |
|
117
|
|
|
'success', |
|
118
|
|
|
'obblm.flash.account.activated' |
|
119
|
|
|
); |
|
120
|
|
|
|
|
121
|
|
|
return $this->redirectToRoute('obblm_login'); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$this->addFlash( |
|
125
|
|
|
'success', |
|
126
|
|
|
'obblm.flash.account.already_activated' |
|
127
|
|
|
); |
|
128
|
|
|
|
|
129
|
|
|
return $this->redirectToRoute('obblm_login'); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @Route("/forgot-password", name="forgot_password") |
|
134
|
|
|
*/ |
|
135
|
|
|
public function forgotPassword(Request $request, EventDispatcherInterface $dispatcher):Response |
|
136
|
|
|
{ |
|
137
|
|
|
$form = $this->createForm(ForgotPasswordForm::class); |
|
138
|
|
|
|
|
139
|
|
|
$form->handleRequest($request); |
|
140
|
|
|
|
|
141
|
|
|
if($form->isSubmitted() && $form->isValid()) |
|
142
|
|
|
{ |
|
143
|
|
|
$data = $form->getData(); |
|
144
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
145
|
|
|
/** @var Coach $coach */ |
|
146
|
|
|
$coach = $em->getRepository(Coach::class)->findOneByEmail($data['email']); |
|
147
|
|
|
if($coach) { |
|
|
|
|
|
|
148
|
|
|
$coach |
|
149
|
|
|
->setResetPasswordAt(new \DateTime()) |
|
150
|
|
|
->setResetPasswordHash($this->getHashFor(random_bytes(10))); |
|
151
|
|
|
$em->persist($coach); |
|
152
|
|
|
$em->flush(); |
|
153
|
|
|
$dispatcher->dispatch(new ResetPasswordCoachEvent($coach), ResetPasswordCoachEvent::NAME); |
|
154
|
|
|
|
|
155
|
|
|
$this->addFlash( |
|
156
|
|
|
'success', |
|
157
|
|
|
'obblm.flash.account.forgot_password.sent' |
|
158
|
|
|
); |
|
159
|
|
|
|
|
160
|
|
|
return $this->redirectToRoute('obblm_login'); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
return $this->render('@ObblmCore/security/forgot-password.html.twig', [ |
|
165
|
|
|
'form' => $form->createView(), |
|
166
|
|
|
]); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @Route("/reset-password/{hash}", name="reset_password") |
|
171
|
|
|
*/ |
|
172
|
|
|
public function resetPassword(string $hash, Request $request, UserPasswordEncoderInterface $passwordEncoder, EventDispatcherInterface $dispatcher):Response |
|
|
|
|
|
|
173
|
|
|
{ |
|
174
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
175
|
|
|
|
|
176
|
|
|
/** @var Coach $coach */ |
|
177
|
|
|
$coach = $em->getRepository(Coach::class)->findOneForPasswordReset($hash); |
|
178
|
|
|
|
|
179
|
|
|
if($coach) { |
|
|
|
|
|
|
180
|
|
|
$form = $this->createForm(PasswordConfirmType::class); |
|
181
|
|
|
|
|
182
|
|
|
$form->handleRequest($request); |
|
183
|
|
|
|
|
184
|
|
|
if ($form->isSubmitted() && $form->isValid()) |
|
185
|
|
|
{ |
|
186
|
|
|
$data = $form->getData(); |
|
187
|
|
|
$password = $passwordEncoder->encodePassword($coach, $data['plainPassword']); |
|
188
|
|
|
$coach->setPassword($password) |
|
189
|
|
|
->setResetPasswordHash(null) |
|
190
|
|
|
->setResetPasswordAt(null); |
|
191
|
|
|
$em->persist($coach); |
|
192
|
|
|
$em->flush(); |
|
193
|
|
|
$this->addFlash( |
|
194
|
|
|
'success', |
|
195
|
|
|
'obblm.flash.account.forgot_password.changed' |
|
196
|
|
|
); |
|
197
|
|
|
return $this->redirectToRoute('obblm_login'); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
return $this->render('@ObblmCore/security/reset-password.html.twig', [ |
|
201
|
|
|
'form' => $form->createView(), |
|
202
|
|
|
]); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
$this->addFlash( |
|
206
|
|
|
'error', |
|
207
|
|
|
'obblm.flash.account.forgot_password.not_found' |
|
208
|
|
|
); |
|
209
|
|
|
return $this->redirectToRoute('obblm_forgot_password'); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
private function getHashFor($value) |
|
213
|
|
|
{ |
|
214
|
|
|
return hash('sha256', $value); |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.