src/Security/Voter/RolloutVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Rollout;
  4. use App\Entity\User;
  5. use App\Repository\RolloutRepository;
  6. use App\Utils\Commons;
  7. use LogicException;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. use Symfony\Component\Security\Core\Security;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. class RolloutVoter extends Voter
  13. {
  14.     const ROLLOUT_VIEW 'ROLLOUT_VIEW';
  15.     const ROLLOUT_CREATE 'ROLLOUT_CREATE';
  16.     const ROLLOUT_EDIT 'ROLLOUT_EDIT';
  17.     const ROLLOUT_DELETE 'ROLLOUT_DELETE';
  18.     const ROLLOUT_MULTI_DELETE 'ROLLOUT_MULTI_DELETE';
  19.     const ROLLOUT_SHOW 'ROLLOUT_SHOW';
  20.     const ROLLOUT_CLONE 'ROLLOUT_CLONE';
  21.     /**
  22.      * @var Security
  23.      */
  24.     private $security;
  25.     /**
  26.      * @var Commons
  27.      */
  28.     private $commons;
  29.     /**
  30.      * @var array
  31.      */
  32.     private $permissions;
  33.     /**
  34.      * @var RolloutRepository
  35.      */
  36.     private $rolloutRepository;
  37.     public function __construct(Security $securityCommons $commonsRolloutRepository $rolloutRepository)
  38.     {
  39.         $this->security $security;
  40.         $this->commons $commons;
  41.         $this->rolloutRepository $rolloutRepository;
  42.         $this->permissions = [];
  43.     }
  44.     /**
  45.      * @param string $attribute
  46.      * @param mixed $subject
  47.      * @return bool
  48.      */
  49.     protected function supports($attribute$subject): bool
  50.     {
  51.         // if the attribute isn't one we support, return false
  52.         if (!in_array($attribute,
  53.             [self::ROLLOUT_VIEWself::ROLLOUT_EDITself::ROLLOUT_CREATEself::ROLLOUT_DELETEself::ROLLOUT_SHOWself::ROLLOUT_CLONEself::ROLLOUT_MULTI_DELETE]
  54.         )) {
  55.             return false;
  56.         }
  57.         return true;
  58.     }
  59.     /**
  60.      * @param string $attribute
  61.      * @param mixed $subject
  62.      * @param TokenInterface $token
  63.      * @return bool
  64.      */
  65.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  66.     {
  67.         /** @var User $user */
  68.         $user $token->getUser();
  69.         // if the user is anonymous, do not grant access
  70.         if (!$user instanceof UserInterface) {
  71.             return false;
  72.         }
  73.         $this->permissions $this->commons->getUserPermissions($user);
  74.         // ... (check conditions and return true to grant permission) ...
  75.         switch ($attribute) {
  76.             case self::ROLLOUT_VIEW:
  77.                 return $this->canView();
  78.             case self::ROLLOUT_CREATE:
  79.                 return $this->canCreate();
  80.             case self::ROLLOUT_EDIT:
  81.                 return ($this->canEdit() && $this->isOwner($user$subject));
  82.             case self::ROLLOUT_DELETE:
  83.                 return ($this->canDelete() && $this->isOwner($user$subject));
  84.             case self::ROLLOUT_MULTI_DELETE:
  85.                 return $this->canMultiDelete();
  86.             case self::ROLLOUT_SHOW:
  87.                 return ($this->canShow() && $this->isOwner($user$subject));
  88.             case self::ROLLOUT_CLONE:
  89.                 return ($this->canClone() && $this->isOwner($user$subject));
  90.         }
  91.         throw new LogicException('Invalid attribute: ' $attribute);
  92.     }
  93.     /**
  94.      * @param User|null $user
  95.      * @param $subject
  96.      * @return bool
  97.      */
  98.     private function isOwner(User $user$subject): bool
  99.     {
  100.         if (!is_object($subject)) {
  101.             $subject $this->rolloutRepository->findOneBy(["id" => intval($subject)]);
  102.         }
  103.         if (!$subject instanceof Rollout) {
  104.             return false;
  105.         }
  106.         return $user->getWorkspace()->getId() === $subject->getWorkspace()->getId();
  107.     }
  108.     /**
  109.      * Return True if have View Permission else return false
  110.      * @return bool
  111.      */
  112.     private function canView(): bool
  113.     {
  114.         if ($this->security->isGranted('ROLE_ADMIN')) {
  115.             return true;
  116.         }
  117.         if (array_key_exists('rollout'$this->permissions)) {
  118.             if (in_array('View'$this->permissions['rollout'])) {
  119.                 return true;
  120.             }
  121.         }
  122.         return false;
  123.     }
  124.     /**
  125.      * Return True if have Edit Permission else return false
  126.      * @return bool
  127.      */
  128.     private function canEdit(): bool
  129.     {
  130.         if ($this->security->isGranted('ROLE_ADMIN')) {
  131.             return true;
  132.         }
  133.         if (array_key_exists('rollout'$this->permissions)) {
  134.             if (in_array('Edit'$this->permissions['rollout'])) {
  135.                 return true;
  136.             }
  137.         }
  138.         return false;
  139.     }
  140.     /**
  141.      * Return True if have Create Permission else return false
  142.      * @return bool
  143.      */
  144.     private function canCreate(): bool
  145.     {
  146.         if ($this->security->isGranted('ROLE_ADMIN')) {
  147.             return true;
  148.         }
  149.         if (array_key_exists('rollout'$this->permissions)) {
  150.             if (in_array('Create'$this->permissions['rollout'])) {
  151.                 return true;
  152.             }
  153.         }
  154.         return false;
  155.     }
  156.     /**
  157.      * Return True if have Delete Permission else return false
  158.      * @return bool
  159.      */
  160.     private function canDelete(): bool
  161.     {
  162.         if ($this->security->isGranted('ROLE_ADMIN')) {
  163.             return true;
  164.         }
  165.         if (array_key_exists('rollout'$this->permissions)) {
  166.             if (in_array('Delete'$this->permissions['rollout'])) {
  167.                 return true;
  168.             }
  169.         }
  170.         return false;
  171.     }
  172.     /**
  173.      * Return True if have Show Permission else return false
  174.      * @return bool
  175.      */
  176.     private function canShow(): bool
  177.     {
  178.         if ($this->security->isGranted('ROLE_ADMIN')) {
  179.             return true;
  180.         }
  181.         if (array_key_exists('rollout'$this->permissions)) {
  182.             if (in_array('Show'$this->permissions['rollout'])) {
  183.                 return true;
  184.             }
  185.         }
  186.         return false;
  187.     }
  188.     /**
  189.      * Return True if have Clone Permission else return false
  190.      * @return bool
  191.      */
  192.     private function canClone(): bool
  193.     {
  194.         if ($this->security->isGranted('ROLE_ADMIN')) {
  195.             return true;
  196.         }
  197.         if (array_key_exists('rollout'$this->permissions)) {
  198.             if (in_array('Clone'$this->permissions['rollout'])) {
  199.                 return true;
  200.             }
  201.         }
  202.         return false;
  203.     }
  204.     /**
  205.      * Return True if have MultiDelete Permission else return false
  206.      * @return bool
  207.      */
  208.     private function canMultiDelete(): bool
  209.     {
  210.         if ($this->security->isGranted('ROLE_ADMIN')) {
  211.             return true;
  212.         }
  213.         if (array_key_exists('rollout'$this->permissions)) {
  214.             if (in_array('Delete'$this->permissions['rollout'])) {
  215.                 return true;
  216.             }
  217.         }
  218.         return false;
  219.     }
  220. }