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

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::POST_DELETE => 'onPostDelete',
  56.         ];
  57.     }
  58.     /**
  59.      * @param DataObjectEvent $dataObjectEvent
  60.      *
  61.      * @throws \Exception
  62.      */
  63.     public function onPreUpdate(DataObjectEvent $dataObjectEvent)
  64.     {
  65.         $user $dataObjectEvent->getObject();
  66.         if ($user instanceof PortalUserInterface) {
  67.             $existingPortalUser PortalUser::getByEmail($user->getEmail())
  68.                     ->addConditionParam('o_id != ? and externalUserId is null'$user->getId())
  69.                     ->count() > 0;
  70.             if ($existingPortalUser) {
  71.                 throw new ValidationException(sprintf('PortalUser with email %s already exists'$user->getEmail()));
  72.             }
  73.             if (empty($user->getPimcoreUser()) && method_exists($user'setPimcoreUser')) {
  74.                 $user->setPimcoreUser($this->getFallbackPimcoreUser()->getId());
  75.             }
  76.         }
  77.     }
  78.     /**
  79.      * @param DataObjectEvent $dataObjectEvent
  80.      *
  81.      * @throws \Exception
  82.      */
  83.     public function onPostDelete(DataObjectEvent $dataObjectEvent)
  84.     {
  85.         $user $dataObjectEvent->getElement();
  86.         if ($user instanceof PortalUserInterface) {
  87.             $this->collectionService->cleanupDeletedUser($user);
  88.             $this->publicShareService->cleanupDeletedUser($user);
  89.         }
  90.     }
  91.     /**
  92.      * @return User
  93.      *
  94.      * @throws \Exception
  95.      */
  96.     protected function getFallbackPimcoreUser(): User
  97.     {
  98.         $user User::getByName(self::FALLBACK_USER_NAME);
  99.         if (empty($user)) {
  100.             $user = new User();
  101.             $user->setName(self::FALLBACK_USER_NAME);
  102.             $user->setActive(true);
  103.             $user->setParentId(0);
  104.             $user->save();
  105.         }
  106.         return $user;
  107.     }
  108. }