Completed
Push — master ( affb41...f007be )
by Rafał
09:54
created

RegistrationController::verifyUserEmail()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 9.36
c 0
b 0
f 0
cc 4
nc 4
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher User Bundle.
7
 *
8
 * Copyright 2021 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @Copyright 2021 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\UserBundle\Controller;
18
19
use SWP\Bundle\SettingsBundle\Context\ScopeContextInterface;
20
use SWP\Bundle\SettingsBundle\Manager\SettingsManagerInterface;
21
use SWP\Bundle\UserBundle\Form\RegistrationFormType;
22
use SWP\Bundle\UserBundle\Mailer\MailerInterface;
23
use SWP\Bundle\UserBundle\Model\UserManagerInterface;
24
use SWP\Bundle\UserBundle\Security\EmailVerifier;
25
use SWP\Bundle\UserBundle\Security\LoginAuthenticator;
26
use SWP\Component\Common\Response\ResponseContext;
27
use SWP\Component\Common\Response\SingleResourceResponse;
28
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
29
use Symfony\Component\HttpFoundation\JsonResponse;
30
use Symfony\Component\HttpFoundation\Request;
31
use Symfony\Component\HttpFoundation\Response;
32
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
33
use Symfony\Component\Routing\Annotation\Route;
34
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
35
use Symfony\Component\Security\Core\User\UserInterface;
36
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
37
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
38
39
class RegistrationController extends AbstractController
40
{
41
    /**
42
     * @var SettingsManagerInterface
43
     */
44
    private $settingsManager;
45
    /**
46
     * @var ScopeContextInterface
47
     */
48
    private $scopeContext;
49
    /**
50
     * @var EmailVerifier
51
     */
52
    private $emailVerifier;
53
    /**
54
     * @var UserManagerInterface
55
     */
56
    private $userManager;
57
58
    public function __construct(
59
        SettingsManagerInterface $settingsManager,
60
        ScopeContextInterface $scopeContext,
61
        EmailVerifier $emailVerifier,
62
        UserManagerInterface $userManager
63
    ) {
64
        $this->settingsManager = $settingsManager;
65
        $this->scopeContext = $scopeContext;
66
        $this->emailVerifier = $emailVerifier;
67
        $this->userManager = $userManager;
68
    }
69
70
    /**
71
     * @Route("/api/{version}/users/register/", methods={"POST"}, options={"expose"=true}, defaults={"version"="v2"}, name="swp_api_core_register_user")
72
     */
73
    public function registerAction(
74
        Request $request,
75
        UserPasswordEncoderInterface $passwordEncoder,
76
        UserManagerInterface $userManager,
77
        MailerInterface $mailer
78
    ) {
79
        try {
80
            $this->ensureThatRegistrationIsEnabled();
81
        } catch (NotFoundHttpException $e) {
82
            return new SingleResourceResponse(null, new ResponseContext(404));
83
        }
84
85
        $user = $userManager->createUser();
86
        $form = $this->createForm(RegistrationFormType::class, $user);
87
88
        $form->handleRequest($request);
89
90
        if ($form->isSubmitted() && $form->isValid()) {
91
            $user->addRole('ROLE_USER');
92
            // encode the plain password
93
            $user->setPassword(
94
                $passwordEncoder->encodePassword(
95
                    $user,
96
                    $form->get('plainPassword')->getData()
97
                )
98
            );
99
100
            $entityManager = $this->getDoctrine()->getManager();
101
            $entityManager->persist($user);
102
            $entityManager->flush();
103
104
            $signatureComponents = $this->emailVerifier->getSignatureComponents('swp_user_verify_email', $user);
105
            $url = $signatureComponents->getSignedUrl();
106
            $mailer->sendConfirmationEmail($user, $url);
107
108
            return new JsonResponse([
109
                'message' => sprintf(
110
                    'The user has been created successfully.
111
                 An email has been sent to %s. It contains an activation link you must click to activate your account.',
112
                    $user->getEmail()
113
                ),
114
                'url' => $url,
115
            ]);
116
        }
117
118
        return new SingleResourceResponse($form, new ResponseContext(400));
119
    }
120
121
    /**
122
     * @Route("/verify/email", name="swp_user_verify_email")
123
     */
124
    public function verifyUserEmail(Request $request, GuardAuthenticatorHandler $guardHandler, LoginAuthenticator $authenticator): Response
125
    {
126
        $id = (int) $request->get('id'); // retrieve the user id from the url
127
128
        // Verify the user id exists and is not null
129
        if (null === $id) {
130
            return $this->redirectToRoute('homepage');
131
        }
132
133
        $user = $this->userManager->find($id);
134
135
        // Ensure the user exists in persistence
136
        if (null === $user) {
137
            return $this->redirectToRoute('homepage');
138
        }
139
        // validate email confirmation link, sets User::isVerified=true and persists
140
        try {
141
            $this->emailVerifier->handleEmailConfirmation($request, $user);
142
        } catch (VerifyEmailExceptionInterface $exception) {
143
            $this->addFlash('verify_email_error', $exception->getReason());
144
145
            return $this->redirectToRoute('homepage');
146
        }
147
148
        $guardHandler->authenticateUserAndHandleSuccess(
149
            $user,
150
            $request,
151
            $authenticator,
152
            'main' // firewall name in security.yaml
153
        );
154
155
        $this->addFlash('success', 'The user has been created successfully.');
156
157
        return $this->redirectToRoute('swp_user_registration_confirmed');
158
    }
159
160
    /**
161
     * Tell the user his account is now confirmed.
162
     */
163
    public function confirmedAction(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
164
    {
165
        $user = $this->getUser();
166
        if (!is_object($user) || !$user instanceof UserInterface) {
167
            $this->createAccessDeniedException('This user does not have access to this section.');
168
        }
169
170
        return $this->render('@SWPUser/Registration/confirmed.html.twig', [
171
            'user' => $user,
172
        ]);
173
    }
174
175
    /**
176
     * @throws NotFoundHttpException
177
     */
178
    private function ensureThatRegistrationIsEnabled()
179
    {
180
        $settingName = 'registration_enabled';
181
        $setting = $this->settingsManager->getOneSettingByName($settingName);
182
        $registrationEnabled = $this->settingsManager
183
            ->get($settingName, $setting['scope'], $this->scopeContext->getScopeOwner($setting['scope']));
184
        if (!$registrationEnabled) {
185
            throw new NotFoundHttpException('Registration is disabled.');
186
        }
187
    }
188
}
189