vendor/ibexa/user/src/lib/Form/Processor/UserRegisterFormProcessor.php line 59

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. namespace Ibexa\User\Form\Processor;
  7. use Ibexa\ContentForms\Event\FormActionEvent;
  8. use Ibexa\Contracts\Core\Repository\Repository;
  9. use Ibexa\Contracts\Core\Repository\RoleService;
  10. use Ibexa\Contracts\Core\Repository\UserService;
  11. use Ibexa\User\Form\Data\UserRegisterData;
  12. use Ibexa\User\Form\UserFormEvents;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\RedirectResponse;
  15. use Symfony\Component\Routing\RouterInterface;
  16. /**
  17.  * Listens for and processes User register events.
  18.  */
  19. class UserRegisterFormProcessor implements EventSubscriberInterface
  20. {
  21.     /** @var \Ibexa\Contracts\Core\Repository\UserService */
  22.     private $userService;
  23.     /** @var \Symfony\Component\Routing\Generator\UrlGeneratorInterface */
  24.     private $urlGenerator;
  25.     /** @var \Ibexa\Core\Repository\Repository */
  26.     private $repository;
  27.     private RoleService $roleService;
  28.     public function __construct(
  29.         Repository $repository,
  30.         UserService $userService,
  31.         RouterInterface $router,
  32.         RoleService $roleService
  33.     ) {
  34.         $this->userService $userService;
  35.         $this->urlGenerator $router;
  36.         $this->repository $repository;
  37.         $this->roleService $roleService;
  38.     }
  39.     public static function getSubscribedEvents()
  40.     {
  41.         return [
  42.             UserFormEvents::USER_REGISTER => ['processRegister'20],
  43.         ];
  44.     }
  45.     /**
  46.      * @param \Ibexa\ContentForms\Event\FormActionEvent $event
  47.      *
  48.      * @throws \Exception
  49.      */
  50.     public function processRegister(FormActionEvent $event)
  51.     {
  52.         /** @var \Ibexa\User\Form\Data\UserRegisterData $data */
  53.         if (!($data $event->getData()) instanceof UserRegisterData) {
  54.             return;
  55.         }
  56.         $form $event->getForm();
  57.         $this->createUser($data$form->getConfig()->getOption('languageCode'));
  58.         $redirectUrl $this->urlGenerator->generate('ibexa.user.register_confirmation');
  59.         $event->setResponse(new RedirectResponse($redirectUrl));
  60.         $event->stopPropagation();
  61.     }
  62.     /**
  63.      * @param \Ibexa\User\Form\Data\UserRegisterData $data
  64.      * @param $languageCode
  65.      *
  66.      * @return \Ibexa\Contracts\Core\Repository\Values\User\User
  67.      *
  68.      * @throws \Exception
  69.      */
  70.     private function createUser(UserRegisterData $data$languageCode)
  71.     {
  72.         foreach ($data->fieldsData as $fieldDefIdentifier => $fieldData) {
  73.             $data->setField($fieldDefIdentifier$fieldData->value$languageCode);
  74.         }
  75.         return $this->repository->sudo(
  76.             function () use ($data) {
  77.                 $user $this->userService->createUser($data$data->getParentGroups());
  78.                 if ($data->getRole() !== null) {
  79.                     $this->roleService->assignRoleToUser($data->getRole(), $user$data->getRoleLimitation());
  80.                 }
  81.                 return $user;
  82.             }
  83.         );
  84.     }
  85. }
  86. class_alias(UserRegisterFormProcessor::class, 'EzSystems\EzPlatformUser\Form\Processor\UserRegisterFormProcessor');