src/Core/Application/Security/ModuleConfigVoter.php line 27

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of the educat package.
  4.  *
  5.  * (c) Solvee
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace App\Core\Application\Security;
  12. use App\Common\Model\Core\ModuleConfigInterface;
  13. use App\Common\Model\Core\UserInterface;
  14. use LogicException;
  15. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  16. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  17. use Symfony\Component\Security\Core\Security;
  18. /**
  19.  * Class ModuleConfigVoter
  20.  *
  21.  * @author MichaƂ Rybnik <michal.rybnik@solvee.pl>
  22.  */
  23. class ModuleConfigVoter extends Voter
  24. {
  25.     public const LIST_MODULE_CONFIG   'list_module_config';
  26.     public const VIEW_MODULE_CONFIG   'view_module_config';
  27.     public const UPDATE_MODULE_CONFIG 'update_module_config';
  28.     public const DELETE_MODULE_CONFIG 'delete_module_config';
  29.     /** @var Security */
  30.     private Security $security;
  31.     /**
  32.      * ModuleConfigVoter constructor.
  33.      *
  34.      * @param Security $security
  35.      */
  36.     public function __construct(Security $security)
  37.     {
  38.         $this->security $security;
  39.     }
  40.     /**
  41.      * @param string $attribute
  42.      * @param mixed  $subject
  43.      *
  44.      * @return bool
  45.      */
  46.     protected function supports($attribute$subject)
  47.     {
  48.         if (self::LIST_MODULE_CONFIG === $attribute) {
  49.             return true;
  50.         }
  51.         if (!in_array(
  52.             $attribute,
  53.             [
  54.                 self::VIEW_MODULE_CONFIG,
  55.                 self::UPDATE_MODULE_CONFIG,
  56.                 self::DELETE_MODULE_CONFIG,
  57.             ]
  58.         )) {
  59.             return false;
  60.         }
  61.         return $attribute && $subject instanceof ModuleConfigInterface;
  62.     }
  63.     /**
  64.      * @inheritDoc
  65.      */
  66.     protected function voteOnAttribute(mixed $attributemixed $subjectTokenInterface $token)
  67.     {
  68.         $user $token->getUser();
  69.         if (!$user instanceof UserInterface) {
  70.             return false;
  71.         }
  72.         switch ($attribute) {
  73.             case self::VIEW_MODULE_CONFIG:
  74.                 return $this->canViewModuleConfig($subject$user);
  75.             case self::LIST_MODULE_CONFIG:
  76.                 return $this->canListModuleConfig($user);
  77.             case self::UPDATE_MODULE_CONFIG:
  78.                 return $this->canUpdateModuleConfig($subject$user);
  79.             case self::DELETE_MODULE_CONFIG:
  80.                 return $this->canDeleteModuleConfig($subject$user);
  81.             default:
  82.                 throw new LogicException();
  83.         }
  84.     }
  85.     /**
  86.      * @param ModuleConfigInterface $moduleConfig
  87.      * @param UserInterface         $user
  88.      *
  89.      * @return bool
  90.      */
  91.     private function canViewModuleConfig(ModuleConfigInterface $moduleConfigUserInterface $user): bool
  92.     {
  93.         return true// todo better module config rules
  94.     }
  95.     /**
  96.      * @param ModuleConfigInterface $moduleConfig
  97.      * @param UserInterface         $user
  98.      *
  99.      * @return bool
  100.      */
  101.     private function canDeleteModuleConfig(ModuleConfigInterface $moduleConfigUserInterface $user): bool
  102.     {
  103.         return $this->security->isGranted('ROLE_ADMIN');
  104.     }
  105.     /**
  106.      * @param UserInterface $user
  107.      *
  108.      * @return bool
  109.      */
  110.     private function canListModuleConfig(UserInterface $user): bool
  111.     {
  112.         return $this->security->isGranted('ROLE_ADMIN');
  113.     }
  114.     /**
  115.      * @param ModuleConfigInterface $moduleConfig
  116.      * @param UserInterface         $user
  117.      *
  118.      * @return bool
  119.      */
  120.     private function canUpdateModuleConfig(ModuleConfigInterface $moduleConfigUserInterface $user): bool
  121.     {
  122.         if ($this->security->isGranted('ROLE_ADMIN')) {
  123.             return true;
  124.         }
  125.         $account $moduleConfig->getAccount();
  126.         return $account->getUser() === $user;
  127.     }
  128. }