1 | <?php |
||
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); |
||
209 |