vendor/pimcore/portal-engine/src/EventSubscriber/SaveUserSubscriber.php line 72

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under following license:
  6.  * - Pimcore Commercial License (PCL)
  7.  *
  8.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  9.  *  @license    http://www.pimcore.org/license     PCL
  10.  */
  11. namespace Pimcore\Bundle\PortalEngineBundle\EventSubscriber;
  12. use Pimcore\Bundle\PortalEngineBundle\Model\DataObject\PortalUserInterface;
  13. use Pimcore\Bundle\PortalEngineBundle\Service\Collection\CollectionService;
  14. use Pimcore\Bundle\PortalEngineBundle\Service\PublicShare\PublicShareService;
  15. use Pimcore\Event\DataObjectEvents;
  16. use Pimcore\Event\Model\DataObjectEvent;
  17. use Pimcore\Model\DataObject\PortalUser;
  18. use Pimcore\Model\Element\ValidationException;
  19. use Pimcore\Model\User;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. /**
  22.  * Class IndexUpdateListener
  23.  *
  24.  * @package Pimcore\Bundle\PortalEngineBundle\EventListener
  25.  */
  26. class SaveUserSubscriber implements EventSubscriberInterface
  27. {
  28.     const FALLBACK_USER_NAME 'portal-engine-default-user';
  29.     /**
  30.      * @var CollectionService
  31.      */
  32.     protected $collectionService;
  33.     /**
  34.      * @var PublicShareService
  35.      */
  36.     protected $publicShareService;
  37.     /**
  38.      * SaveUserSubscriber constructor.
  39.      *
  40.      * @param CollectionService $collectionService
  41.      * @param PublicShareService $publicShareService
  42.      */
  43.     public function __construct(CollectionService $collectionServicePublicShareService $publicShareService)
  44.     {
  45.         $this->collectionService $collectionService;
  46.         $this->publicShareService $publicShareService;
  47.     }
  48.     /**
  49.      * @return array
  50.      */
  51.     public static function getSubscribedEvents()
  52.     {
  53.         return [
  54.             DataObjectEvents::PRE_UPDATE => 'onPreUpdate',
  55.             DataObjectEvents::PRE_ADD => 'onPreUpdate',
  56.             DataObjectEvents::POST_DELETE => 'onPostDelete',
  57.         ];
  58.     }
  59.     /**
  60.      * @param DataObjectEvent $dataObjectEvent
  61.      *
  62.      * @throws \Exception
  63.      */
  64.     public function onPreUpdate(DataObjectEvent $dataObjectEvent)
  65.     {
  66.         $user $dataObjectEvent->getObject();
  67.         if ($user instanceof PortalUserInterface) {
  68.             $existingPortalUser PortalUser::getByEmail($user->getEmail())
  69.                     ->addConditionParam('o_id != ? and externalUserId is null'$user->getId())
  70.                     ->count() > 0;
  71.             if ($existingPortalUser) {
  72.                 throw new ValidationException(sprintf('PortalUser with email %s already exists'$user->getEmail()));
  73.             }
  74.             if (empty($user->getPimcoreUser()) && method_exists($user'setPimcoreUser')) {
  75.                 $user->setPimcoreUser($this->getFallbackPimcoreUser()->getId());
  76.             }
  77.         }
  78.     }
  79.     /**
  80.      * @param DataObjectEvent $dataObjectEvent
  81.      *
  82.      * @throws \Exception
  83.      */
  84.     public function onPostDelete(DataObjectEvent $dataObjectEvent)
  85.     {
  86.         $user $dataObjectEvent->getElement();
  87.         if ($user instanceof PortalUserInterface) {
  88.             $this->collectionService->cleanupDeletedUser($user);
  89.             $this->publicShareService->cleanupDeletedUser($user);
  90.         }
  91.     }
  92.     /**
  93.      * @return User
  94.      *
  95.      * @throws \Exception
  96.      */
  97.     protected function getFallbackPimcoreUser(): User
  98.     {
  99.         $user User::getByName(self::FALLBACK_USER_NAME);
  100.         if (empty($user)) {
  101.             $user = new User();
  102.             $user->setName(self::FALLBACK_USER_NAME);
  103.             $user->setActive(true);
  104.             $user->setParentId(0);
  105.             $user->save();
  106.         }
  107.         return $user;
  108.     }
  109. }