core/lib/Thelia/Action/Profile.php line 50

  1. <?php
  2. /*
  3.  * This file is part of the Thelia package.
  4.  * http://www.thelia.net
  5.  *
  6.  * (c) OpenStudio <info@thelia.net>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Thelia\Action;
  12. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Thelia\Core\Event\Profile\ProfileEvent;
  15. use Thelia\Core\Event\TheliaEvents;
  16. use Thelia\Core\Security\AccessManager;
  17. use Thelia\Model\ModuleQuery;
  18. use Thelia\Model\Profile as ProfileModel;
  19. use Thelia\Model\ProfileModule;
  20. use Thelia\Model\ProfileModuleQuery;
  21. use Thelia\Model\ProfileQuery;
  22. use Thelia\Model\ProfileResource;
  23. use Thelia\Model\ProfileResourceQuery;
  24. use Thelia\Model\ResourceQuery;
  25. class Profile extends BaseAction implements EventSubscriberInterface
  26. {
  27.     public function create(ProfileEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  28.     {
  29.         $profile = new ProfileModel();
  30.         $profile
  31.             ->setCode($event->getCode())
  32.             ->setLocale($event->getLocale())
  33.             ->setTitle($event->getTitle())
  34.             ->setChapo($event->getChapo())
  35.             ->setDescription($event->getDescription())
  36.             ->setPostscriptum($event->getPostscriptum())
  37.         ;
  38.         $profile->save();
  39.         $event->setProfile($profile);
  40.     }
  41.     public function update(ProfileEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  42.     {
  43.         if (null !== $profile ProfileQuery::create()->findPk($event->getId())) {
  44.             $profile
  45.                 ->setLocale($event->getLocale())
  46.                 ->setTitle($event->getTitle())
  47.                 ->setChapo($event->getChapo())
  48.                 ->setDescription($event->getDescription())
  49.                 ->setPostscriptum($event->getPostscriptum())
  50.             ;
  51.             $profile->save();
  52.             $event->setProfile($profile);
  53.         }
  54.     }
  55.     public function updateResourceAccess(ProfileEvent $event): void
  56.     {
  57.         if (null !== $profile ProfileQuery::create()->findPk($event->getId())) {
  58.             ProfileResourceQuery::create()->filterByProfileId($event->getId())->delete();
  59.             foreach ($event->getResourceAccess() as $resourceCode => $accesses) {
  60.                 $manager = new AccessManager(0);
  61.                 $manager->build($accesses);
  62.                 $profileResource = new ProfileResource();
  63.                 $profileResource->setProfileId($event->getId())
  64.                     ->setResource(ResourceQuery::create()->findOneByCode($resourceCode))
  65.                     ->setAccess($manager->getAccessValue());
  66.                 $profileResource->save();
  67.             }
  68.             $event->setProfile($profile);
  69.         }
  70.     }
  71.     public function updateModuleAccess(ProfileEvent $event): void
  72.     {
  73.         if (null !== $profile ProfileQuery::create()->findPk($event->getId())) {
  74.             ProfileModuleQuery::create()->filterByProfileId($event->getId())->delete();
  75.             foreach ($event->getModuleAccess() as $moduleCode => $accesses) {
  76.                 $manager = new AccessManager(0);
  77.                 $manager->build($accesses);
  78.                 $profileModule = new ProfileModule();
  79.                 $profileModule->setProfileId($event->getId())
  80.                     ->setModule(ModuleQuery::create()->findOneByCode($moduleCode))
  81.                     ->setAccess($manager->getAccessValue());
  82.                 $profileModule->save();
  83.             }
  84.             $event->setProfile($profile);
  85.         }
  86.     }
  87.     public function delete(ProfileEvent $event): void
  88.     {
  89.         if (null !== $profile ProfileQuery::create()->findPk($event->getId())) {
  90.             $profile
  91.                 ->delete()
  92.             ;
  93.             $event->setProfile($profile);
  94.         }
  95.     }
  96.     /**
  97.      * {@inheritDoc}
  98.      */
  99.     public static function getSubscribedEvents()
  100.     {
  101.         return [
  102.             TheliaEvents::PROFILE_CREATE => ['create'128],
  103.             TheliaEvents::PROFILE_UPDATE => ['update'128],
  104.             TheliaEvents::PROFILE_DELETE => ['delete'128],
  105.             TheliaEvents::PROFILE_RESOURCE_ACCESS_UPDATE => ['updateResourceAccess'128],
  106.             TheliaEvents::PROFILE_MODULE_ACCESS_UPDATE => ['updateModuleAccess'128],
  107.         ];
  108.     }
  109. }