1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller\User; |
4
|
|
|
|
5
|
|
|
use App\Entity\User; |
6
|
|
|
use App\Event\User\UserResetpasswordEvent; |
7
|
|
|
use App\Event\User\UserValidatedEvent; |
8
|
|
|
use App\FlashMessage\FlashMessageCategory; |
9
|
|
|
use App\Form\ResetpasswordFormType; |
10
|
|
|
use App\Security\UserValidator; |
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
12
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
15
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
16
|
|
|
|
17
|
|
|
class ResetPasswordController extends AbstractController |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var EventDispatcherInterface |
22
|
|
|
*/ |
23
|
|
|
private $dispatcher; |
24
|
|
|
|
25
|
|
|
public function __construct(EventDispatcherInterface $dispatcher) |
26
|
|
|
{ |
27
|
|
|
$this->dispatcher = $dispatcher; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @Route("/user/resetpassword/{token}", name="app_resetpassword", methods={"GET", "POST"}, requirements={ |
32
|
|
|
* "token": "[a-h0-9]*" |
33
|
|
|
* }) |
34
|
|
|
*/ |
35
|
|
|
public function resetPassword( |
36
|
|
|
string $token, |
37
|
|
|
Request $request, |
38
|
|
|
AuthorizationCheckerInterface $authChecker, |
39
|
|
|
UserValidator $userValidator |
40
|
|
|
) { |
41
|
|
|
//if we are authenticated, no reason to be here |
42
|
|
|
if ($authChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')) { |
43
|
|
|
return $this->redirectToRoute('home'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
//If we got here then we followed a reset link from email. We can verify mail |
47
|
|
|
if ($userValidator->doesResetpasswordTokenValidateEmail($token)) { |
48
|
|
|
$event = new UserValidatedEvent($user); |
|
|
|
|
49
|
|
|
$this->dispatcher->dispatch(UserValidatedEvent::NAME, $event); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$form = $this->createForm(ResetpasswordFormType::class); |
53
|
|
|
$form->handleRequest($request); |
54
|
|
|
|
55
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
56
|
|
|
$event = new UserResetpasswordEvent($userValidator->retrieveUserFromToken($token), $form->get('plainPassword')->getData()); |
57
|
|
|
$this->dispatcher->dispatch(UserResetpasswordEvent::NAME, $event); |
58
|
|
|
|
59
|
|
|
$this->addFlash(FlashMessageCategory::SUCCESS, "A reset password link has been sent"); |
60
|
|
|
return $this->redirectToRoute('app_login'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $this->render('validation/resetpassword.html.twig', [ |
64
|
|
|
'resetpasswordForm' => $form->createView(), |
65
|
|
|
'userEmail' => $user->getEmail(), |
66
|
|
|
]); |
67
|
|
|
} |
68
|
|
|
} |