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

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