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\CoreBundle\Model\OutputChannelInterface; |
20
|
|
|
use SWP\Bundle\SettingsBundle\Context\ScopeContextInterface; |
21
|
|
|
use SWP\Bundle\SettingsBundle\Manager\SettingsManagerInterface; |
22
|
|
|
use SWP\Bundle\UserBundle\Form\RegistrationFormType; |
23
|
|
|
use SWP\Bundle\UserBundle\Mailer\MailerInterface; |
24
|
|
|
use SWP\Bundle\UserBundle\Model\UserManagerInterface; |
25
|
|
|
use SWP\Bundle\UserBundle\Security\EmailVerifier; |
26
|
|
|
use SWP\Bundle\UserBundle\Security\LoginAuthenticator; |
27
|
|
|
use SWP\Component\Common\Response\ResponseContext; |
28
|
|
|
use SWP\Component\Common\Response\SingleResourceResponse; |
29
|
|
|
use SWP\Component\MultiTenancy\Context\TenantContextInterface; |
30
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
31
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
32
|
|
|
use Symfony\Component\HttpFoundation\Request; |
33
|
|
|
use Symfony\Component\HttpFoundation\Response; |
34
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
35
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
36
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
37
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
38
|
|
|
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler; |
39
|
|
|
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface; |
40
|
|
|
|
41
|
|
|
class RegistrationController extends AbstractController |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* @var SettingsManagerInterface |
45
|
|
|
*/ |
46
|
|
|
private $settingsManager; |
47
|
|
|
/** |
48
|
|
|
* @var ScopeContextInterface |
49
|
|
|
*/ |
50
|
|
|
private $scopeContext; |
51
|
|
|
/** |
52
|
|
|
* @var EmailVerifier |
53
|
|
|
*/ |
54
|
|
|
private $emailVerifier; |
55
|
|
|
/** |
56
|
|
|
* @var UserManagerInterface |
57
|
|
|
*/ |
58
|
|
|
private $userManager; |
59
|
|
|
|
60
|
|
|
private TenantContextInterface $tenantContext; |
|
|
|
|
61
|
|
|
|
62
|
|
|
public function __construct( |
63
|
|
|
SettingsManagerInterface $settingsManager, |
64
|
|
|
ScopeContextInterface $scopeContext, |
65
|
|
|
EmailVerifier $emailVerifier, |
66
|
|
|
UserManagerInterface $userManager, |
67
|
|
|
TenantContextInterface $tenantContext |
68
|
|
|
) { |
69
|
|
|
$this->settingsManager = $settingsManager; |
70
|
|
|
$this->scopeContext = $scopeContext; |
71
|
|
|
$this->emailVerifier = $emailVerifier; |
72
|
|
|
$this->userManager = $userManager; |
73
|
|
|
$this->tenantContext = $tenantContext; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @Route("/api/{version}/users/register/", methods={"POST"}, options={"expose"=true}, defaults={"version"="v2"}, name="swp_api_core_register_user") |
78
|
|
|
*/ |
79
|
|
|
public function registerAction( |
80
|
|
|
Request $request, |
81
|
|
|
UserPasswordEncoderInterface $passwordEncoder, |
82
|
|
|
UserManagerInterface $userManager, |
83
|
|
|
MailerInterface $mailer |
84
|
|
|
) { |
85
|
|
|
try { |
86
|
|
|
$this->ensureThatRegistrationIsEnabled(); |
87
|
|
|
} catch (NotFoundHttpException $e) { |
88
|
|
|
return new SingleResourceResponse(null, new ResponseContext(404)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$user = $userManager->createUser(); |
92
|
|
|
$form = $this->createForm(RegistrationFormType::class, $user); |
93
|
|
|
|
94
|
|
|
$form->handleRequest($request); |
95
|
|
|
|
96
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
97
|
|
|
$user->addRole('ROLE_USER'); |
98
|
|
|
// encode the plain password |
99
|
|
|
$user->setPassword( |
100
|
|
|
$passwordEncoder->encodePassword( |
101
|
|
|
$user, |
102
|
|
|
$form->get('plainPassword')->getData() |
103
|
|
|
) |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
$entityManager = $this->getDoctrine()->getManager(); |
107
|
|
|
$entityManager->persist($user); |
108
|
|
|
$entityManager->flush(); |
109
|
|
|
|
110
|
|
|
$signatureComponents = $this->emailVerifier->getSignatureComponents('swp_user_verify_email', $user); |
111
|
|
|
$url = $signatureComponents->getSignedUrl(); |
112
|
|
|
$url = $this->applyPWAUrl($url); |
113
|
|
|
|
114
|
|
|
$mailer->sendConfirmationEmail($user, $url); |
115
|
|
|
|
116
|
|
|
return new JsonResponse([ |
117
|
|
|
'message' => sprintf( |
118
|
|
|
'The user has been created successfully. |
119
|
|
|
An email has been sent to %s. It contains an activation link you must click to activate your account.', |
120
|
|
|
$user->getEmail() |
121
|
|
|
), |
122
|
|
|
'url' => $url, |
123
|
|
|
]); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return new SingleResourceResponse($form, new ResponseContext(400)); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @Route("/verify/email", name="swp_user_verify_email") |
131
|
|
|
*/ |
132
|
|
|
public function verifyUserEmail(Request $request, GuardAuthenticatorHandler $guardHandler, LoginAuthenticator $authenticator): Response |
133
|
|
|
{ |
134
|
|
|
$id = (int) $request->get('id'); // retrieve the user id from the url |
135
|
|
|
|
136
|
|
|
// Verify the user id exists and is not null |
137
|
|
|
if (null === $id) { |
138
|
|
|
return $this->redirectToRoute('homepage'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$user = $this->userManager->find($id); |
142
|
|
|
|
143
|
|
|
// Ensure the user exists in persistence |
144
|
|
|
if (null === $user) { |
145
|
|
|
return $this->redirectToRoute('homepage'); |
146
|
|
|
} |
147
|
|
|
// validate email confirmation link, sets User::isVerified=true and persists |
148
|
|
|
try { |
149
|
|
|
$this->emailVerifier->handleEmailConfirmation($request, $user); |
150
|
|
|
} catch (VerifyEmailExceptionInterface $exception) { |
151
|
|
|
$this->addFlash('verify_email_error', $exception->getReason()); |
152
|
|
|
|
153
|
|
|
return $this->redirectToRoute('homepage'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$guardHandler->authenticateUserAndHandleSuccess( |
157
|
|
|
$user, |
158
|
|
|
$request, |
159
|
|
|
$authenticator, |
160
|
|
|
'main' // firewall name in security.yaml |
161
|
|
|
); |
162
|
|
|
|
163
|
|
|
$this->addFlash('success', 'The user has been created successfully.'); |
164
|
|
|
|
165
|
|
|
return $this->redirectToRoute('swp_user_registration_confirmed'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Tell the user his account is now confirmed. |
170
|
|
|
*/ |
171
|
|
|
public function confirmedAction(Request $request) |
172
|
|
|
{ |
173
|
|
|
$user = $this->getUser(); |
174
|
|
|
if (!is_object($user) || !$user instanceof UserInterface) { |
175
|
|
|
$this->createAccessDeniedException('This user does not have access to this section.'); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $this->render('@SWPUser/Registration/confirmed.html.twig', [ |
179
|
|
|
'user' => $user, |
180
|
|
|
]); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @throws NotFoundHttpException |
185
|
|
|
*/ |
186
|
|
|
private function ensureThatRegistrationIsEnabled() |
187
|
|
|
{ |
188
|
|
|
$settingName = 'registration_enabled'; |
189
|
|
|
$setting = $this->settingsManager->getOneSettingByName($settingName); |
190
|
|
|
$registrationEnabled = $this->settingsManager |
191
|
|
|
->get($settingName, $setting['scope'], $this->scopeContext->getScopeOwner($setting['scope'])); |
192
|
|
|
if (!$registrationEnabled) { |
193
|
|
|
throw new NotFoundHttpException('Registration is disabled.'); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
private function applyPWAUrl (string $url): string { |
198
|
|
|
if ($this->tenantContext->getTenant() && |
199
|
|
|
$this->tenantContext->getTenant()->getPWAConfig() && |
200
|
|
|
$this->tenantContext->getTenant()->getPWAConfig()->getUrl() |
201
|
|
|
) { |
202
|
|
|
$PWAUrlParts = parse_url($this->tenantContext->getTenant()->getPWAConfig()['url']); |
203
|
|
|
$urlParts = parse_url($url); |
204
|
|
|
$url = $PWAUrlParts['scheme'].'://'.$PWAUrlParts['host'].':'.$PWAUrlParts['port'].$urlParts['path'].'?'.$urlParts['query']; |
205
|
|
|
} |
206
|
|
|
return $url; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|