Test Failed
Branch develop (bbb827)
by Stone
08:03
created

ValidationController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 109
rs 10
c 0
b 0
f 0
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 42 5
A __construct() 0 3 1
B resetPassword() 0 42 7
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\Form\ResetpasswordFormType;
9
use App\Security\UserAutoLogon;
10
use App\Services\FlashMessageCategory;
11
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\Routing\Annotation\Route;
15
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
16
17
class ValidationController 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("/validate/{token}", name="app_validate", methods={"GET"}, requirements={
32
     *     "token": "[a-h0-9]*"
33
     * })
34
     */
35
    public function validate(
36
        string $token,
37
        AuthorizationCheckerInterface $authChecker,
38
        UserAutoLogon $autoLogon
39
    ) {
40
        //if we are authenticated, no reason to be here
41
        if ($authChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
42
            return $this->redirectToRoute('trick.home');
43
        }
44
45
        $user = $this->getDoctrine()
46
            ->getRepository(User::class)
47
            ->findUserByHash($token);
48
49
        if (!$user) {
50
            //no user found
51
            $this->addFlash(FlashMessageCategory::ERROR, 'Invalid Token, please use this form to resend a link');
52
            return $this->redirectToRoute('app_forgotpassword');
53
        }
54
55
        if ($user->getVerified()) {
56
            //Account already active
57
            $this->addFlash(FlashMessageCategory::INFO, 'Mail already verified');
58
            return $this->redirectToRoute('app_login');
59
        }
60
61
        //checking the date
62
        if ($user->isVerifiedDateTimeValid()) {
63
64
            $event = new UserValidatedEvent($user);
65
            $this->dispatcher->dispatch(UserValidatedEvent::NAME, $event);
66
67
            //autologon
68
            $autoLogon->autoLogon($user);
69
70
            return $this->redirectToRoute('trick.home');
71
        }
72
73
        //Error, redirect to the forgot password
74
        $this->addFlash(FlashMessageCategory::ERROR,
75
            'Your verification link is no longer valid, please use this form to resend a link');
76
        return $this->redirectToRoute('app_forgotpassword');
77
    }
78
79
    /**
80
     * @Route("/resetpassword/{token}", name="app_resetpassword", methods={"GET", "POST"}, requirements={
81
     *     "token": "[a-h0-9]*"
82
     * })
83
     */
84
    public function resetPassword(string $token, Request $request, AuthorizationCheckerInterface $authChecker)
85
    {
86
87
        //TODO: this will probably use the same validation so make private function ?
88
        //if we are authenticated, no reason to be here
89
        if ($authChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
90
            return $this->redirectToRoute('trick.home');
91
        }
92
93
        $user = $this->getDoctrine()
94
            ->getRepository(User::class)
95
            ->findUserByHash($token);
96
97
        if (!$user) {
98
            //no user found
99
            $this->addFlash(FlashMessageCategory::ERROR, 'Invalid Token, please use this form to resend a link');
100
            return $this->redirectToRoute('app_forgotpassword');
101
        }
102
103
        //If we got here then we followed a reset link from email. We can verify mail
104
        if (!$user->getVerified() && $user->isVerifiedDateTimeValid()) {
105
            $event = new UserValidatedEvent($user);
106
            $this->dispatcher->dispatch(UserValidatedEvent::NAME, $event);
107
        }
108
109
        $form = $this->createForm(ResetpasswordFormType::class);
110
        $form->handleRequest($request);
111
112
        if ($form->isSubmitted() && $form->isValid()) {
113
114
            $event = new UserResetpasswordEvent($user, $form->get('plainPassword')->getData());
115
            $this->dispatcher->dispatch(UserResetpasswordEvent::NAME, $event);
116
117
            $this->addFlash(FlashMessageCategory::SUCCESS, "Success");
118
            return $this->redirectToRoute('app_forgotpassword');
119
        }
120
121
        //TODO: take care of submitted form
122
123
        return $this->render('validation/resetpassword.html.twig', [
124
            'resetpasswordForm' => $form->createView(),
125
            'userEmail' => $user->getEmail(),
126
        ]);
127
    }
128
129
}
130