Passed
Pull Request — master (#106)
by
unknown
07:55
created

ResetPasswordController::resetPassword()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 33
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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