src/Security/Voter/UserGroupVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\User;
  4. use App\Entity\UserGroup;
  5. use App\Repository\UserGroupRepository;
  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 UserGroupVoter extends Voter
  13. {
  14.     const USER_GROUP_VIEW 'USER_GROUP_VIEW';
  15.     const USER_GROUP_SHOW 'USER_GROUP_SHOW';
  16.     const USER_GROUP_CREATE 'USER_GROUP_CREATE';
  17.     const USER_GROUP_EDIT 'USER_GROUP_EDIT';
  18.     const USER_GROUP_DELETE 'USER_GROUP_DELETE';
  19.     const USER_GROUP_MULTI_DELETE 'USER_GROUP_MULTI_DELETE';
  20.     const USER_GROUP_CLONE 'USER_GROUP_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 UserGroupRepository
  35.      */
  36.     private $userGroupRepository;
  37.     /**
  38.      * DeviceGroupVoter constructor.
  39.      * @param Security $security
  40.      * @param Commons $commons
  41.      * @param UserGroupRepository $userGroupRepository
  42.      */
  43.     public function __construct(Security $securityCommons $commonsUserGroupRepository $userGroupRepository)
  44.     {
  45.         $this->security $security;
  46.         $this->commons $commons;
  47.         $this->userGroupRepository $userGroupRepository;
  48.         $this->permissions = [];
  49.     }
  50.     /**
  51.      * @param string $attribute
  52.      * @param mixed $subject
  53.      * @return bool
  54.      */
  55.     protected function supports($attribute$subject): bool
  56.     {
  57.         // if the attribute isn't one we support, return false
  58.         if (!in_array($attribute,
  59.             [self::USER_GROUP_VIEWself::USER_GROUP_SHOWself::USER_GROUP_EDITself::USER_GROUP_CREATEself::USER_GROUP_DELETEself::USER_GROUP_CLONEself::USER_GROUP_MULTI_DELETE]
  60.         )) {
  61.             return false;
  62.         }
  63.         return true;
  64.     }
  65.     /**
  66.      * @param string $attribute
  67.      * @param mixed $subject
  68.      * @param TokenInterface $token
  69.      * @return bool
  70.      */
  71.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  72.     {
  73.         /** @var User $user */
  74.         $user $token->getUser();
  75.         // if the user is anonymous, do not grant access
  76.         if (!$user instanceof UserInterface) {
  77.             return false;
  78.         }
  79.         $this->permissions $this->commons->getUserPermissions($user);
  80.         switch ($attribute) {
  81.             case self::USER_GROUP_VIEW:
  82.                 return $this->canView();
  83.             case self::USER_GROUP_SHOW:
  84.                 return ($this->canShow() && $this->isOwner($user$subject));
  85.             case self::USER_GROUP_CREATE:
  86.                 return $this->canCreate();
  87.             case self::USER_GROUP_MULTI_DELETE:
  88.                 return $this->canMultiDelete();
  89.             case self::USER_GROUP_EDIT:
  90.                 return ($this->canEdit() && $this->isOwner($user$subject));
  91.             case self::USER_GROUP_DELETE:
  92.                 return ($this->canDelete() && $this->isOwner($user$subject));
  93.             case self::USER_GROUP_CLONE:
  94.                 return ($this->canClone() && $this->isOwner($user$subject));
  95.         }
  96.         throw new LogicException('Invalid attribute: ' $attribute);
  97.     }
  98.     /**
  99.      * @param User|null $user
  100.      * @param $subject
  101.      * @return bool
  102.      */
  103.     private function isOwner(User $user$subject): bool
  104.     {
  105.         if (!is_object($subject)) {
  106.             $subject $this->userGroupRepository->findOneBy(["id" => intval($subject)]);
  107.         }
  108.         if (!$subject instanceof UserGroup) {
  109.             return false;
  110.         }
  111.         return $user->getWorkspace()->getId() === $subject->getWorkspace()->getId();
  112.     }
  113.     /**
  114.      * Return True if have View Permission else return false
  115.      * @return bool
  116.      */
  117.     private function canView(): bool
  118.     {
  119.         if ($this->security->isGranted('ROLE_ADMIN')) {
  120.             return true;
  121.         }
  122.         if (array_key_exists('userGroup'$this->permissions)) {
  123.             if (in_array('View'$this->permissions['userGroup'])) {
  124.                 return true;
  125.             }
  126.         }
  127.         return false;
  128.     }
  129.     /**
  130.      * Return True if have View Permission else return false
  131.      * @return bool
  132.      */
  133.     private function canShow(): bool
  134.     {
  135.         if ($this->security->isGranted('ROLE_ADMIN')) {
  136.             return true;
  137.         }
  138.         if (array_key_exists('userGroup'$this->permissions)) {
  139.             if (in_array('Show'$this->permissions['userGroup'])) {
  140.                 return true;
  141.             }
  142.         }
  143.         return false;
  144.     }
  145.     /**
  146.      * Return True if have Edit Permission else return false
  147.      * @return bool
  148.      */
  149.     private function canEdit(): bool
  150.     {
  151.         if ($this->security->isGranted('ROLE_ADMIN')) {
  152.             return true;
  153.         }
  154.         if (array_key_exists('userGroup'$this->permissions)) {
  155.             if (in_array('Edit'$this->permissions['userGroup'])) {
  156.                 return true;
  157.             }
  158.         }
  159.         return false;
  160.     }
  161.     /**
  162.      * Return True if have Create Permission else return false
  163.      * @return bool
  164.      */
  165.     private function canCreate(): bool
  166.     {
  167.         if ($this->security->isGranted('ROLE_ADMIN')) {
  168.             return true;
  169.         }
  170.         if (array_key_exists('userGroup'$this->permissions)) {
  171.             if (in_array('Create'$this->permissions['userGroup'])) {
  172.                 return true;
  173.             }
  174.         }
  175.         return false;
  176.     }
  177.     /**
  178.      * Return True if have Delete Permission else return false
  179.      * @return bool
  180.      */
  181.     private function canDelete(): bool
  182.     {
  183.         if ($this->security->isGranted('ROLE_ADMIN')) {
  184.             return true;
  185.         }
  186.         if (array_key_exists('userGroup'$this->permissions)) {
  187.             if (in_array('Delete'$this->permissions['userGroup'])) {
  188.                 return true;
  189.             }
  190.         }
  191.         return false;
  192.     }
  193.     /**
  194.      * Return True if have Clone Permission else return false
  195.      * @return bool
  196.      */
  197.     private function canClone(): bool
  198.     {
  199.         if ($this->security->isGranted('ROLE_ADMIN')) {
  200.             return true;
  201.         }
  202.         if (array_key_exists('userGroup'$this->permissions)) {
  203.             if (in_array('Clone'$this->permissions['userGroup'])) {
  204.                 return true;
  205.             }
  206.         }
  207.         return false;
  208.     }
  209.     /**
  210.      * @return bool
  211.      */
  212.     private function canMultiDelete(): bool
  213.     {
  214.         if ($this->security->isGranted('ROLE_ADMIN')) {
  215.             return true;
  216.         }
  217.         if (array_key_exists('userGroup'$this->permissions)) {
  218.             if (in_array('Delete'$this->permissions['userGroup'])) {
  219.                 return true;
  220.             }
  221.         }
  222.         return false;
  223.     }
  224. }