src/EventSubscriber/UserSubscriber.php line 206

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\User;
  4. use App\Entity\JobQueue;
  5. use App\Service\EsCache;
  6. use App\Service\EsJobQueue;
  7. use App\Handler\UserHandler;
  8. use App\Entity\RoleContainer;
  9. use App\Event\EsEventLogEvent;
  10. use App\Event\UserPreRemoveEvent;
  11. use App\Event\UserPreUpdateEvent;
  12. use App\Event\UserPrePersistEvent;
  13. use App\Event\UserPostPersistEvent;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Symfony\Component\Security\Core\Security;
  16. use App\Service\JobQueueAction\ElasticDependentUpdateAction;
  17. use Symfony\Component\Validator\Validator\ValidatorInterface;
  18. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  21. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  22. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  23. class UserSubscriber implements EventSubscriberInterface
  24. {
  25.     public function __construct(
  26.         private UserHandler $handler,
  27.         private UserPasswordHasherInterface $passwordEncoder,
  28.         private EntityManagerInterface $_em,
  29.         private EventDispatcherInterface $dispatcher,
  30.         private ValidatorInterface $validator,
  31.         private Security $security,
  32.         private EsJobQueue $esJobQueue,
  33.         private EsCache $esCache,
  34.         private $appEnvType
  35.     ) {
  36.     }
  37.     public static function getSubscribedEvents(): array
  38.     {
  39.         return [
  40.             UserPostPersistEvent::NAME => 'onPostCreate',
  41.             UserPrePersistEvent::NAME => 'onPreCreate',
  42.             UserPreUpdateEvent::NAME => 'onPreUpdate',
  43.             UserPreRemoveEvent::NAME => 'onPreRemove'
  44.         ];
  45.     }
  46.     public function onPostCreate(UserPostPersistEvent $event)
  47.     {
  48.         $object $event->getObject();
  49.         $this->handler->assignContainerUserGroup($object);
  50.         $this->handler->attchUserToCompany([$object->getId()]);
  51.         $eventObj = new EsEventLogEvent($objectEsEventLogEvent::EVENT_USER_CREATE);
  52.         $this->dispatcher->dispatch($eventObjEsEventLogEvent::EVENT_USER_CREATE);
  53.     }
  54.     public function onPreCreate(UserPrePersistEvent $event)
  55.     {
  56.         $object $event->getObject();
  57.         $violations $this->validator->validate($object);
  58.         if (count($violations) > 0) {
  59.             foreach ($violations as $v) {
  60.                 throw new BadRequestHttpException($v->getMessage());
  61.                 break;
  62.             }
  63.         }
  64.         if (empty($this->security->getUser())) {
  65.             return;
  66.         }
  67.         if (!$this->security->isGranted(RoleContainer::ROLE_ADMIN) && $this->security->isGranted(RoleContainer::ROLE_OPERATOR)) {
  68.             $allowRoles = [
  69.                 RoleContainer::ROLE_SPEAKER,
  70.                 RoleContainer::ROLE_MODERATOR,
  71.                 RoleContainer::ROLE_OPERATOR
  72.             ];
  73.             if (!in_array($object->getRole(), $allowRoles)) {
  74.                 throw new AccessDeniedException();
  75.             }
  76.         }
  77.         $userContainer $this->esCache->getContainer(false);
  78.         if (!$userContainer) {
  79.             throw new BadRequestHttpException("User creation container not found!");
  80.         }
  81.         $object->setUserContainer($userContainer);
  82.         $object->addNewsfeedContainerSubscription($userContainer);
  83.         if (($object->getIsConsiderSpeaker() && !$object->getIsDisplayAsGuest()) ||
  84.             ($object->getIsConsiderModerator() && !$object->getIsDisplayAsGuest())) {
  85.             $object->setIsDisplayAsGuest(true);
  86.         }
  87.         if ($object->getIsConsiderSpeaker() || $object->getIsConsiderModerator()) {
  88.             $object->setIsOnboarded(true);
  89.         }
  90.         if ($object->getPlainPassword()) {
  91.             $object->setPassword(
  92.                 $this->passwordEncoder->hashPassword($object$object->getPlainPassword())
  93.             );
  94.             $object->eraseCredentials();
  95.         }
  96.         $object->setIsPwdGenerated(true);
  97.         $object->setIsEmailOff(false);
  98.         $object->setIsUnsubscribedInvite(false);
  99.         if ($object->getSpecialTitles()) {
  100.             foreach ($object->getSpecialTitles() as $title) {
  101.                 $nfCategory $title->getNewsfeedCategory();
  102.                 if ($nfCategory) {
  103.                     $object->addNewsfeedSubscription($nfCategory);
  104.                 }
  105.             }
  106.         }
  107.     }
  108.     public function onPreUpdate(UserPreUpdateEvent $event)
  109.     {
  110.         $object $event->getObject();
  111.         $violations $this->validator->validate($object);
  112.         if (count($violations) > 0) {
  113.             foreach ($violations as $v) {
  114.                 throw new BadRequestHttpException($v->getMessage());
  115.                 break;
  116.             }
  117.         }
  118.         if ($object->getPlainPassword()) {
  119.             $object->setPassword(
  120.                 $this->passwordEncoder->hashPassword($object$object->getPlainPassword())
  121.             );
  122.             $object->eraseCredentials();
  123.         }
  124.         if (($object->getIsConsiderSpeaker() && !$object->getIsDisplayAsGuest()) ||
  125.             ($object->getIsConsiderModerator() && !$object->getIsDisplayAsGuest())) {
  126.             $object->setIsDisplayAsGuest(true);
  127.         }
  128.         if ($object->getIsConsiderSpeaker() || $object->getIsConsiderModerator()) {
  129.             $object->setIsOnboarded(true);
  130.         }
  131.         if (empty($this->security->getUser())) {
  132.             return;
  133.         }
  134.         if ($object->getIsUpdateNewsfeedSubscriptions() && $object->getSpecialTitles()) {
  135.             foreach ($object->getSpecialTitles() as $title) {
  136.                 $nfCategory $title->getNewsfeedCategory();
  137.                 if ($nfCategory) {
  138.                     $object->addNewsfeedSubscription($nfCategory);
  139.                 }
  140.             }
  141.         }
  142.         if (!$this->security->isGranted(RoleContainer::ROLE_ADMIN) && $this->security->isGranted(RoleContainer::ROLE_OPERATOR)) {
  143.             $allowRoles = [
  144.                 RoleContainer::ROLE_SPEAKER,
  145.                 RoleContainer::ROLE_MODERATOR,
  146.                 RoleContainer::ROLE_OPERATOR
  147.             ];
  148.             if (!in_array($object->getRole(), $allowRoles)) {
  149.                 throw new AccessDeniedException();
  150.             }
  151.         }
  152.         $originalEvent $event->getEvent();
  153.         $changeSet $originalEvent->getEntityChangeSet();
  154.         if (isset($changeSet['isOnboarded'])) {
  155.             if ($changeSet['isOnboarded'][0] !== $changeSet['isOnboarded'][1]) {
  156.                 $object->setOnboardedAt(new \DateTime());
  157.                 $eventObj = new EsEventLogEvent($objectEsEventLogEvent::EVENT_ONBOARDING);
  158.                 $this->dispatcher->dispatch($eventObjEsEventLogEvent::EVENT_ONBOARDING);
  159.             }
  160.         }
  161.         if (isset($changeSet['firstName']) || isset($changeSet['lastName']) || isset($changeSet['imageName'])) {
  162.             $payload = [...$object->getQueueInfo(), 'operation' => 'UPDATE'];
  163.             $this->esJobQueue->create(
  164.                 ElasticDependentUpdateAction::NAME,
  165.                 json_encode($payload),
  166.                 JobQueue::PRIORITY_LOW,
  167.                 null,
  168.                 1,
  169.                 null,
  170.                 false,
  171.                 false
  172.             );
  173.         }
  174.         /*
  175.         if (is_array($changeSet) && count($changeSet) > 0) {
  176.             $eventObj = new EsEventLogEvent($object, EsEventLogEvent::EVENT_CHANGE_PROFILE);
  177.             $this->dispatcher->dispatch($eventObj, EsEventLogEvent::EVENT_CHANGE_PROFILE);
  178.         }
  179.         */
  180.     }
  181.     public function onPreRemove(UserPreRemoveEvent $event)
  182.     {
  183.         $user $this->security->getUser();
  184.         if (empty($user)) {
  185.             return;
  186.         }
  187.         $allowRoles = [];
  188.         if ($this->security->isGranted(RoleContainer::ROLE_OPERATOR)) {
  189.             $allowRoles = [
  190.                 RoleContainer::ROLE_SPEAKER,
  191.                 RoleContainer::ROLE_MODERATOR,
  192.                 RoleContainer::ROLE_OPERATOR
  193.             ];
  194.         }
  195.         if ($this->security->isGranted(RoleContainer::ROLE_ADMIN) || $this->security->isGranted(RoleContainer::ROLE_SUPPORT)) {
  196.             $allowRoles = [
  197.                 RoleContainer::ROLE_GUEST_VOTER,
  198.                 RoleContainer::ROLE_READER,
  199.                 RoleContainer::ROLE_USER,
  200.                 RoleContainer::ROLE_INSTRUCTOR,
  201.                 RoleContainer::ROLE_STAFF,
  202.                 RoleContainer::ROLE_RELATION_MANAGER,
  203.                 RoleContainer::ROLE_SPEAKER,
  204.                 RoleContainer::ROLE_MODERATOR,
  205.                 RoleContainer::ROLE_SUPPORT,
  206.                 RoleContainer::ROLE_OPERATOR,
  207.                 RoleContainer::ROLE_ADMIN
  208.             ];
  209.         }
  210.         if ($this->security->isGranted(RoleContainer::ROLE_SUPER_ADMIN)) {
  211.             if ($this->appEnvType === 'CS') {
  212.                 $allowRoles = [
  213.                     RoleContainer::ROLE_ADMIN,
  214.                     RoleContainer::ROLE_OPERATOR
  215.                 ];
  216.             } else {
  217.                 $allowRoles = [
  218.                     ...$allowRoles,
  219.                     RoleContainer::ROLE_SUPER_ADMIN
  220.                 ];
  221.             }
  222.         }
  223.         $object $event->getObject();
  224.         if (!in_array($object->getRole(), $allowRoles) && $user->getId() !== $object->getId()) {
  225.             throw new AccessDeniedException();
  226.         }
  227.         $payload = [...$object->getQueueInfo(), 'operation' => 'DELETE'];
  228.         $this->esJobQueue->create(
  229.             ElasticDependentUpdateAction::NAME,
  230.             json_encode($payload),
  231.             JobQueue::PRIORITY_LOW
  232.         );
  233.         $eventObj = new EsEventLogEvent($objectEsEventLogEvent::EVENT_USER_DELETE);
  234.         $this->dispatcher->dispatch($eventObjEsEventLogEvent::EVENT_USER_DELETE);
  235.     }
  236. }