Completed
Push — master ( c7f57e...d77439 )
by Rafał
09:30
created

RegistrationController::verifyUserEmail()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 8.9848
c 0
b 0
f 0
cc 5
nc 5
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
107
            $mailer->sendConfirmationEmail($user, $url);
108
109
            return new JsonResponse([
110
                'message' => sprintf(
111
                    'The user has been created successfully.
112
                 An email has been sent to %s. It contains an activation link you must click to activate your account.',
113
                    $user->getEmail()
114
                ),
115
                'url' => $url,
116
            ]);
117
        }
118
119
        return new SingleResourceResponse($form, new ResponseContext(400));
120
    }
121
122
    /**
123
     * @Route("/verify/email", name="swp_user_verify_email")
124
     */
125
    public function verifyUserEmail(Request $request, GuardAuthenticatorHandler $guardHandler, LoginAuthenticator $authenticator): Response
126
    {
127
        $id = (int) $request->get('id'); // retrieve the user id from the url
128
129
        if ($request->isXmlHttpRequest()) {
130
            return $this->verifyUserEmailFromPWA($id, $request);
131
        }
132
133
        // Verify the user id exists and is not null
134
        if (null === $id) {
135
            return $this->redirectToRoute('homepage');
136
        }
137
138
        $user = $this->userManager->find($id);
139
140
        // Ensure the user exists in persistence
141
        if (null === $user) {
142
            return $this->redirectToRoute('homepage');
143
        }
144
        // validate email confirmation link, sets User::isVerified=true and persists
145
        try {
146
            $this->emailVerifier->handleEmailConfirmation($request, $user);
147
        } catch (VerifyEmailExceptionInterface $exception) {
148
            $this->addFlash('verify_email_error', $exception->getReason());
149
150
            return $this->redirectToRoute('homepage');
151
        }
152
153
        $guardHandler->authenticateUserAndHandleSuccess(
154
            $user,
155
            $request,
156
            $authenticator,
157
            'main' // firewall name in security.yaml
158
        );
159
160
        $this->addFlash('success', 'The user has been created successfully.');
161
162
        return $this->redirectToRoute('swp_user_registration_confirmed');
163
    }
164
165
    /**
166
     * Tell the user his account is now confirmed.
167
     */
168
    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...
169
    {
170
        $user = $this->getUser();
171
        if (!is_object($user) || !$user instanceof UserInterface) {
172
            $this->createAccessDeniedException('This user does not have access to this section.');
173
        }
174
175
        return $this->render('@SWPUser/Registration/confirmed.html.twig', [
176
            'user' => $user,
177
        ]);
178
    }
179
180
    /**
181
     * @throws NotFoundHttpException
182
     */
183
    private function ensureThatRegistrationIsEnabled()
184
    {
185
        $settingName = 'registration_enabled';
186
        $setting = $this->settingsManager->getOneSettingByName($settingName);
187
        $registrationEnabled = $this->settingsManager
188
            ->get($settingName, $setting['scope'], $this->scopeContext->getScopeOwner($setting['scope']));
189
        if (!$registrationEnabled) {
190
            throw new NotFoundHttpException('Registration is disabled.');
191
        }
192
    }
193
194
    private function verifyUserEmailFromPWA(int $id, Request $request): JsonResponse
195
    {
196
        // Verify the user id exists and is not null
197
        if (null === $id) {
198
            return new JsonResponse(
199
                ['error' => 'User does not exist']
200
            );
201
        }
202
203
        $user = $this->userManager->find($id);
204
205
        // Ensure the user exists in persistence
206
        if (null === $user) {
207
            return new JsonResponse(
208
                ['error' => 'User does not exist']
209
            );
210
        }
211
212
        // validate email confirmation link, sets User::isVerified=true and persists
213
        try {
214
            $this->emailVerifier->handleEmailConfirmation($request, $user);
215
        } catch (VerifyEmailExceptionInterface $exception) {
216
            return new JsonResponse(
217
                ['error' => 'Registration confirmation invalid']
218
            );
219
        }
220
221
        return new JsonResponse(
222
            ['message' => 'The user has been created successfully.']
223
        );
224
    }
225
}
226