1 <?php
declare(strict_types
=1);
4 * This file is part of the Rapsys UserBundle package.
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Rapsys\UserBundle\Controller
;
14 use Doctrine\DBAL\Exception\UniqueConstraintViolationException
;
16 use Rapsys\UserBundle\RapsysUserBundle
;
18 use Symfony\Bridge\Twig\Mime\TemplatedEmail
;
19 use Symfony\Component\Form\FormError
;
20 use Symfony\Component\HttpFoundation\Request
;
21 use Symfony\Component\HttpFoundation\Response
;
22 use Symfony\Component\HttpKernel\Exception\BadRequestHttpException
;
23 use Symfony\Component\Mailer\Exception\TransportExceptionInterface
;
24 use Symfony\Component\Mime\Address
;
25 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
;
26 use Symfony\Component\Security\Http\Authentication\AuthenticationUtils
;
31 class UserController
extends AbstractController
{
35 * @param Request $request The request
36 * @return Response The response
38 public function index(Request
$request): Response
{
40 if (!$this->checker
->isGranted($this->config
['default']['admin'])) {
42 throw $this->createAccessDeniedException($this->translator
->trans('Unable to list users', [], $this->alias
));
46 $this->context
['count'] = $this->doctrine
->getRepository($this->config
['class']['user'])->findCountAsInt();
48 //With not enough users
49 if ($this->context
['count'] - $this->page
* $this->limit
< 0) {
51 throw $this->createNotFoundException($this->translator
->trans('Unable to find users', [], $this->alias
));
55 $this->context
['groups'] = $this->doctrine
->getRepository($this->config
['class']['user'])->findIndexByGroupId($this->page
, $this->limit
);
60 $this->config
['index']['view']['name'],
62 $this->context+
$this->config
['index']['view']['context']
67 * Confirm account from mail link
69 * @param Request $request The request
70 * @param string $hash The hashed password
71 * @param string $mail The shorted mail address
72 * @return Response The response
74 public function confirm(Request
$request, string $hash, string $mail): Response
{
76 if ($hash != $this->slugger
->hash($mail)) {
78 throw new BadRequestHttpException($this->translator
->trans('Invalid %field% field: %value%', ['%field%' => 'hash', '%value%' => $hash], $this->alias
));
82 $mail = $this->slugger
->unshort($smail = $mail);
85 if (filter_var($mail, FILTER_VALIDATE_EMAIL
) === false) {
87 //XXX: prevent slugger reverse engineering by not displaying decoded mail
88 throw new BadRequestHttpException($this->translator
->trans('Invalid %field% field: %value%', ['%field%' => 'mail', '%value%' => $smail], $this->alias
));
91 //Without existing registrant
92 if (!($user = $this->doctrine
->getRepository($this->config
['class']['user'])->findOneByMail($mail))) {
93 //Add error message mail already exists
94 //XXX: prevent slugger reverse engineering by not displaying decoded mail
95 $this->addFlash('error', $this->translator
->trans('The account do not exists', [], $this->alias
));
97 //Redirect to register view
98 return $this->redirectToRoute($this->config
['route']['register']['name'], $this->config
['route']['register']['context']);
102 $user->setActive(true);
105 $this->manager
->persist($user);
108 $this->manager
->flush();
110 //Add error message mail already exists
111 $this->addFlash('notice', $this->translator
->trans('Your account has been activated', [], $this->alias
));
113 //Redirect to user view
114 return $this->redirectToRoute($this->config
['route']['edit']['name'], ['mail' => $smail, 'hash' => $this->slugger
->hash($smail)]+
$this->config
['route']['edit']['context']);
118 * Edit account by shorted mail
120 * @param Request $request The request
121 * @param string $hash The hashed password
122 * @param string $mail The shorted mail address
123 * @return Response The response
125 public function edit(Request
$request, string $hash, string $mail): Response
{
127 if ($hash != $this->slugger
->hash($mail)) {
129 throw new BadRequestHttpException($this->translator
->trans('Invalid %field% field: %value%', ['%field%' => 'hash', '%value%' => $hash], $this->alias
));
133 $mail = $this->slugger
->unshort($smail = $mail);
135 //With existing subscriber
136 if (empty($user = $this->doctrine
->getRepository($this->config
['class']['user'])->findOneByMail($mail))) {
138 //XXX: prevent slugger reverse engineering by not displaying decoded mail
139 throw $this->createNotFoundException($this->translator
->trans('Unable to find account', [], $this->alias
));
142 //Prevent access when not admin, user is not guest and not currently logged user
143 if (!$this->checker
->isGranted($this->config
['default']['admin']) && $user != $this->security
->getUser() || !$this->checker
->isGranted('IS_AUTHENTICATED_FULLY')) {
144 //Throw access denied
145 //XXX: prevent slugger reverse engineering by not displaying decoded mail
146 throw $this->createAccessDeniedException($this->translator
->trans('Unable to access user', [], $this->alias
));
149 //Create the EditType form and give the proper parameters
150 $edit = $this->factory
->create($this->config
['edit']['view']['edit'], $user, [
151 //Set action to edit route name and context
152 'action' => $this->generateUrl($this->config
['route']['edit']['name'], ['mail' => $smail, 'hash' => $this->slugger
->hash($smail)]+
$this->config
['route']['edit']['context']),
154 'civility_class' => $this->config
['class']['civility'],
155 //Set civility default
156 'civility_default' => $this->doctrine
->getRepository($this->config
['class']['civility'])->findOneByTitle($this->config
['default']['civility']),
158 'mail' => $this->checker
->isGranted('ROLE_ADMIN'),
164 'translation_domain' => $this->alias
165 ]+
($this->checker
->isGranted($this->config
['default']['admin'])?$this->config
['edit']['admin']:$this->config
['edit']['field']));
168 if ($this->checker
->isGranted($this->config
['default']['admin'])) {
169 //Create the EditType form and give the proper parameters
170 $reset = $this->factory
->create($this->config
['edit']['view']['reset'], $user, [
171 //Set action to edit route name and context
172 'action' => $this->generateUrl($this->config
['route']['edit']['name'], ['mail' => $smail, 'hash' => $this->slugger
->hash($smail)]+
$this->config
['route']['edit']['context']),
176 'translation_domain' => $this->alias
180 if ($request->isMethod('POST')) {
181 //Refill the fields in case the form is not valid.
182 $reset->handleRequest($request);
184 //With reset submitted and valid
185 if ($reset->isSubmitted() && $reset->isValid()) {
187 $data = $reset->getData();
190 $data->setPassword($this->hasher
->hashPassword($data, $data->getPassword()));
193 $this->manager
->persist($data);
195 //Flush to get the ids
196 $this->manager
->flush();
199 $this->addFlash('notice', $this->translator
->trans('Account password updated', [], $this->alias
));
201 //Redirect to cleanup the form
202 return $this->redirectToRoute($this->config
['route']['edit']['name'], ['mail' => $smail = $this->slugger
->short($mail), 'hash' => $this->slugger
->hash($smail)]+
$this->config
['route']['edit']['context']);
207 $this->config
['edit']['view']['context']['reset'] = $reset->createView();
211 if ($request->isMethod('POST')) {
212 //Refill the fields in case the form is not valid.
213 $edit->handleRequest($request);
215 //With edit submitted and valid
216 if ($edit->isSubmitted() && $edit->isValid()) {
218 $data = $edit->getData();
221 $this->manager
->persist($data);
223 //Try saving in database
225 //Flush to get the ids
226 $this->manager
->flush();
229 $this->addFlash('notice', $this->translator
->trans('Account updated', [], $this->alias
));
231 //Redirect to cleanup the form
232 return $this->redirectToRoute($this->config
['route']['edit']['name'], ['mail' => $smail = $this->slugger
->short($mail), 'hash' => $this->slugger
->hash($smail)]+
$this->config
['route']['edit']['context']);
233 //Catch double slug or mail
234 } catch (UniqueConstraintViolationException
$e) {
235 //Add error message mail already exists
236 $this->addFlash('error', $this->translator
->trans('The account already exists', [], $this->alias
));
240 //XXX: prefer a reset on login to force user unspam action
241 } elseif (!$this->checker
->isGranted($this->config
['default']['admin'])) {
243 $this->addFlash('notice', $this->translator
->trans('To change your password login with your mail and any password then follow the procedure', [], $this->alias
));
247 return $this->render(
249 $this->config
['edit']['view']['name'],
251 ['register' => $edit->createView()]+
$this->config
['edit']['view']['context']
258 * @param Request $request The request
259 * @param AuthenticationUtils $authenticationUtils The authentication utils
260 * @param ?string $hash The hashed password
261 * @param ?string $mail The shorted mail address
262 * @return Response The response
264 public function login(Request
$request, AuthenticationUtils
$authenticationUtils, ?string $hash, ?string $mail): Response
{
265 //Create the LoginType form and give the proper parameters
266 $login = $this->factory
->create($this->config
['login']['view']['form'], null, [
267 //Set action to login route name and context
268 'action' => $this->generateUrl($this->config
['route']['login']['name'], $this->config
['route']['login']['context']),
272 'translation_domain' => $this->alias
279 if (!empty($mail) && !empty($hash)) {
281 if ($hash != $this->slugger
->hash($mail)) {
283 throw new BadRequestHttpException($this->translator
->trans('Invalid %field% field: %value%', ['%field%' => 'hash', '%value%' => $hash], $this->alias
));
287 $mail = $this->slugger
->unshort($smail = $mail);
290 if (filter_var($mail, FILTER_VALIDATE_EMAIL
) === false) {
292 throw new BadRequestHttpException($this->translator
->trans('Invalid %field% field: %value%', ['%field%' => 'mail', '%value%' => $smail], $this->alias
));
296 $login->get('mail')->setData($mail);
297 //Last username entered by the user
298 } elseif ($lastUsername = $authenticationUtils->getLastUsername()) {
299 $login->get('mail')->setData($lastUsername);
302 //Get the login error if there is one
303 if ($error = $authenticationUtils->getLastAuthenticationError()) {
304 //Get translated error
305 $error = $this->translator
->trans($error->getMessageKey(), [], $this->alias
);
307 //Add error message to mail field
308 $login->get('mail')->addError(new FormError($error));
310 //Create the RecoverType form and give the proper parameters
311 $recover = $this->factory
->create($this->config
['recover']['view']['form'], null, [
312 //Set action to recover route name and context
313 'action' => $this->generateUrl($this->config
['route']['recover']['name'], $this->config
['route']['recover']['context']),
319 'translation_domain' => $this->alias
322 //Get recover mail entity
323 $recover->get('mail')
324 //Set mail from login form
325 ->setData($login->get('mail')->getData())
327 ->addError(new FormError($this->translator
->trans('Use this form to recover your account', [], $this->alias
)));
329 //Add recover form to context
330 $context['recover'] = $recover->createView();
333 $this->addFlash('notice', $this->translator
->trans('To change your password login with your mail and any password then follow the procedure', [], $this->alias
));
337 return $this->render(
339 $this->config
['login']['view']['name'],
341 ['login' => $login->createView(), 'disabled' => $request->query
->get('disabled', 0)]+
$context+
$this->config
['login']['view']['context']
348 * @param Request $request The request
349 * @param ?string $hash The hashed password
350 * @param ?string $pass The shorted password
351 * @param ?string $mail The shorted mail address
352 * @return Response The response
354 public function recover(Request
$request, ?string $hash, ?string $pass, ?string $mail): Response
{
361 //With mail, pass and hash
362 if (!empty($mail) && !empty($pass) && !empty($hash)) {
364 if ($hash != $this->slugger
->hash($mail.$pass)) {
366 throw new BadRequestHttpException($this->translator
->trans('Invalid %field% field: %value%', ['%field%' => 'hash', '%value%' => $hash], $this->alias
));
370 $mail = $this->slugger
->unshort($smail = $mail);
373 if (filter_var($mail, FILTER_VALIDATE_EMAIL
) === false) {
375 //XXX: prevent slugger reverse engineering by not displaying decoded mail
376 throw new BadRequestHttpException($this->translator
->trans('Invalid %field% field: %value%', ['%field%' => 'mail', '%value%' => $smail], $this->alias
));
379 //With existing subscriber
380 if (empty($user = $this->doctrine
->getRepository($this->config
['class']['user'])->findOneByMail($mail))) {
382 //XXX: prevent slugger reverse engineering by not displaying decoded mail
383 throw $this->createNotFoundException($this->translator
->trans('Unable to find account', [], $this->alias
));
386 //With unmatched pass
387 if ($pass != $this->slugger
->hash($user->getPassword())) {
389 //XXX: prevent use of outdated recover link
390 throw $this->createNotFoundException($this->translator
->trans('Outdated recover link', [], $this->alias
));
394 $context = ['mail' => $smail, 'pass' => $pass, 'hash' => $hash];
397 //Create the LoginType form and give the proper parameters
398 $form = $this->factory
->create($this->config
['recover']['view']['form'], $user, [
399 //Set action to recover route name and context
400 'action' => $this->generateUrl($this->config
['route']['recover']['name'], $context+
$this->config
['route']['recover']['context']),
401 //With user disable mail
402 'mail' => ($user === null),
403 //With user enable password
404 'password' => ($user !== null),
408 'translation_domain' => $this->alias
412 if ($request->isMethod('POST')) {
413 //Refill the fields in case the form is not valid.
414 $form->handleRequest($request);
416 //With form submitted and valid
417 if ($form->isSubmitted() && $form->isValid()) {
419 $data = $form->getData();
422 if ($user !== null) {
423 //Set hashed password
424 $hashed = $this->hasher
->hashPassword($user, $user->getPassword());
427 $pass = $this->slugger
->hash($hashed);
430 $user->setPassword($hashed);
433 $this->manager
->persist($user);
436 $this->manager
->flush();
439 $this->addFlash('notice', $this->translator
->trans('Account password updated', [], $this->alias
));
441 //Redirect to user login
442 return $this->redirectToRoute($this->config
['route']['login']['name'], ['mail' => $smail, 'hash' => $this->slugger
->hash($smail)]+
$this->config
['route']['login']['context']);
443 //Find user by data mail
444 } elseif ($user = $this->doctrine
->getRepository($this->config
['class']['user'])->findOneByMail($data['mail'])) {
447 'recipient_mail' => $user->getMail(),
448 'recipient_name' => $user->getRecipientName()
449 ] +
array_replace_recursive(
450 $this->config
['context'],
451 $this->config
['recover']['view']['context'],
452 $this->config
['recover']['mail']['context']
455 //Generate each route route
456 foreach($this->config
['recover']['route'] as $route => $tag) {
457 //Only process defined routes
458 if (!empty($this->config
['route'][$route])) {
459 //Process for recover mail url
460 if ($route == 'recover') {
461 //Set the url in context
462 $context[$tag] = $this->router
->generate(
463 $this->config
['route'][$route]['name'],
464 //Prepend recover context with tag
466 'mail' => $smail = $this->slugger
->short($context['recipient_mail']),
467 'pass' => $spass = $this->slugger
->hash($pass = $user->getPassword()),
468 'hash' => $this->slugger
->hash($smail.$spass)
469 ]+
$this->config
['route'][$route]['context'],
470 UrlGeneratorInterface
::ABSOLUTE_URL
477 $context['subject'] = $subject = ucfirst(
478 $this->translator
->trans(
479 $this->config
['recover']['mail']['subject'],
480 $this->slugger
->flatten($context, null, '.', '%', '%'),
486 $message = (new TemplatedEmail())
488 ->from(new Address($this->config
['contact']['address'], $this->translator
->trans($this->config
['contact']['name'], [], $this->alias
)))
490 //XXX: remove the debug set in vendor/symfony/mime/Address.php +46
491 ->to(new Address($context['recipient_mail'], $context['recipient_name']))
493 ->subject($context['subject'])
495 //Set path to twig templates
496 ->htmlTemplate($this->config
['recover']['mail']['html'])
497 ->textTemplate($this->config
['recover']['mail']['text'])
503 $this->addFlash('notice', $this->translator
->trans('Account recovered', [], $this->alias
));
505 //Try sending message
506 //XXX: mail delivery may silently fail
509 $this->mailer
->send($message);
512 $this->addFlash('notice', $this->translator
->trans('Your recovery mail has been sent, to retrieve your account follow the recuperate link inside', [], $this->alias
));
515 $this->addFlash('warning', $this->translator
->trans('If you did not receive a recovery mail, check your Spam or Junk mail folder', [], $this->alias
));
517 //Redirect on home route to cleanup form
518 return $this->redirectToRoute($this->config
['route']['home']['name'], $this->config
['route']['home']['context']);
519 //Catch obvious transport exception
520 } catch(TransportExceptionInterface
$e) {
521 //Add error message mail unreachable
522 $form->get('mail')->addError(new FormError($this->translator
->trans('Unable to reach account', [], $this->alias
)));
529 return $this->render(
531 $this->config
['recover']['view']['name'],
533 ['recover' => $form->createView()]+
$this->config
['recover']['view']['context']
538 * Register an account
540 * @param Request $request The request
541 * @return Response The response
543 public function register(Request
$request): Response
{
545 if (!empty($_POST['register']['mail'])) {
547 $this->logger
->emergency(
548 $this->translator
->trans(
549 'register: mail=%mail% locale=%locale% confirm=%confirm% ip=%ip%',
551 '%mail%' => $postMail = $_POST['register']['mail'],
552 '%locale%' => $request->getLocale(),
553 '%confirm%' => $this->router
->generate(
554 $this->config
['route']['confirm']['name'],
555 //Prepend subscribe context with tag
557 'mail' => $postSmail = $this->slugger
->short($postMail),
558 'hash' => $this->slugger
->hash($postSmail)
559 ]+
$this->config
['route']['confirm']['context'],
560 UrlGeneratorInterface
::ABSOLUTE_URL
562 '%ip%' => $request->getClientIp()
570 $reflection = new \
ReflectionClass($this->config
['class']['user']);
573 $user = $reflection->newInstance('', '');
575 //Create the RegisterType form and give the proper parameters
576 $form = $this->factory
->create($this->config
['register']['view']['form'], $user, [
577 //Set action to register route name and context
578 'action' => $this->generateUrl($this->config
['route']['register']['name'], $this->config
['route']['register']['context']),
582 'civility_class' => $this->config
['class']['civility'],
583 //Set civility default
584 'civility_default' => $this->doctrine
->getRepository($this->config
['class']['civility'])->findOneByTitle($this->config
['default']['civility']),
588 'translation_domain' => $this->alias
589 ]+
($this->checker
->isGranted($this->config
['default']['admin'])?$this->config
['register']['admin']:$this->config
['register']['field']));
592 if ($request->isMethod('POST')) {
593 //Refill the fields in case the form is not valid.
594 $form->handleRequest($request);
596 //With form submitted and valid
597 if ($form->isSubmitted() && $form->isValid()) {
599 $data = $form->getData();
602 $user->setPassword($this->hasher
->hashPassword($user, $user->getPassword()));
605 $this->manager
->persist($user);
607 //Iterate on default group
608 foreach($this->config
['default']['group'] as $i => $groupTitle) {
610 if (($group = $this->doctrine
->getRepository($this->config
['class']['group'])->findOneByTitle($groupTitle))) {
612 //XXX: see vendor/symfony/security-core/Role/Role.php
613 $user->addGroup($group);
617 //XXX: consider missing group as fatal
618 throw new \
Exception(sprintf('Group %s listed in %s.default.group[%d] not found by title', $groupTitle, $this->alias
, $i));
624 'recipient_mail' => $user->getMail(),
625 'recipient_name' => $user->getRecipientName()
626 ] +
array_replace_recursive(
627 $this->config
['context'],
628 $this->config
['register']['view']['context'],
629 $this->config
['register']['mail']['context']
632 //Generate each route route
633 foreach($this->config
['register']['route'] as $route => $tag) {
634 //Only process defined routes
635 if (!empty($this->config
['route'][$route])) {
636 //Process for confirm mail url
637 if ($route == 'confirm') {
638 //Set the url in context
639 $context[$tag] = $this->router
->generate(
640 $this->config
['route'][$route]['name'],
641 //Prepend register context with tag
643 'mail' => $smail = $this->slugger
->short($context['recipient_mail']),
644 'hash' => $this->slugger
->hash($smail)
645 ]+
$this->config
['route'][$route]['context'],
646 UrlGeneratorInterface
::ABSOLUTE_URL
653 $context['subject'] = $subject = ucfirst(
654 $this->translator
->trans(
655 $this->config
['register']['mail']['subject'],
656 $this->slugger
->flatten($context, null, '.', '%', '%'),
662 $message = (new TemplatedEmail())
664 ->from(new Address($this->config
['contact']['address'], $this->translator
->trans($this->config
['contact']['name'], [], $this->alias
)))
666 //XXX: remove the debug set in vendor/symfony/mime/Address.php +46
667 ->to(new Address($context['recipient_mail'], $context['recipient_name']))
669 ->subject($context['subject'])
671 //Set path to twig templates
672 ->htmlTemplate($this->config
['register']['mail']['html'])
673 ->textTemplate($this->config
['register']['mail']['text'])
678 //Try saving in database
681 $this->manager
->flush();
684 $this->addFlash('notice', $this->translator
->trans('Account created', [], $this->alias
));
686 //Try sending message
687 //XXX: mail delivery may silently fail
690 $this->mailer
->send($message);
692 //Add verification notice
693 $this->addFlash('notice', $this->translator
->trans('Your verification mail has been sent, to activate your account you must follow the confirmation link inside', [], $this->alias
));
696 $this->addFlash('warning', $this->translator
->trans('If you did not receive a verification mail, check your Spam or Junk mail folders', [], $this->alias
));
698 //Redirect on home route to cleanup form
699 return $this->redirectToRoute($this->config
['route']['home']['name'], $this->config
['route']['home']['context']);
700 //Catch obvious transport exception
701 } catch(TransportExceptionInterface
$e) {
702 //Add error message mail unreachable
703 $form->get('mail')->addError(new FormError($this->translator
->trans('Unable to reach account', [], $this->alias
)));
705 //Catch double subscription
706 } catch (UniqueConstraintViolationException
$e) {
707 //Add error message mail already exists
708 $this->addFlash('error', $this->translator
->trans('The account already exists', [], $this->alias
));
714 return $this->render(
716 $this->config
['register']['view']['name'],
718 ['register' => $form->createView()]+
$this->config
['register']['view']['context']