src/Security/Voter/DeviceGroupVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\DeviceGroup;
  4. use App\Entity\User;
  5. use App\Repository\DeviceGroupRepository;
  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 DeviceGroupVoter extends Voter
  13. {
  14.     const DEVICE_GROUP_VIEW 'DEVICE_GROUP_VIEW';
  15.     const DEVICE_GROUP_CREATE 'DEVICE_GROUP_CREATE';
  16.     const DEVICE_GROUP_EDIT 'DEVICE_GROUP_EDIT';
  17.     const DEVICE_GROUP_DELETE 'DEVICE_GROUP_DELETE';
  18.     const DEVICE_GROUP_MULTI_DELETE 'DEVICE_GROUP_MULTI_DELETE';
  19.     const DEVICE_GROUP_SHOW 'DEVICE_GROUP_SHOW';
  20.     /**
  21.      * @var Security
  22.      */
  23.     private $security;
  24.     /**
  25.      * @var Commons
  26.      */
  27.     private $commons;
  28.     /**
  29.      * @var array
  30.      */
  31.     private $permissions;
  32.     /**
  33.      * @var DeviceGroupRepository
  34.      */
  35.     private $deviceGroupRepository;
  36.     /**
  37.      * DeviceGroupVoter constructor.
  38.      * @param Security $security
  39.      * @param Commons $commons
  40.      * @param DeviceGroupRepository $deviceGroupRepository
  41.      */
  42.     public function __construct(Security $securityCommons $commonsDeviceGroupRepository $deviceGroupRepository)
  43.     {
  44.         $this->security $security;
  45.         $this->commons $commons;
  46.         $this->deviceGroupRepository $deviceGroupRepository;
  47.         $this->permissions = [];
  48.     }
  49.     /**
  50.      * @param string $attribute
  51.      * @param mixed $subject
  52.      * @return bool
  53.      */
  54.     protected function supports($attribute$subject): bool
  55.     {
  56.         // if the attribute isn't one we support, return false
  57.         if (!in_array($attribute,
  58.             [self::DEVICE_GROUP_VIEWself::DEVICE_GROUP_MULTI_DELETEself::DEVICE_GROUP_SHOWself::DEVICE_GROUP_EDITself::DEVICE_GROUP_CREATEself::DEVICE_GROUP_DELETE]
  59.         )) {
  60.             return false;
  61.         }
  62.         return true;
  63.     }
  64.     /**
  65.      * @param string $attribute
  66.      * @param mixed $subject
  67.      * @param TokenInterface $token
  68.      * @return bool
  69.      */
  70.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  71.     {
  72.         /** @var User $user */
  73.         $user $token->getUser();
  74.         // if the user is anonymous, do not grant access
  75.         if (!$user instanceof UserInterface) {
  76.             return false;
  77.         }
  78.         $this->permissions $this->commons->getUserPermissions($user);
  79.         switch ($attribute) {
  80.             case self::DEVICE_GROUP_VIEW:
  81.                 return $this->canView();
  82.             case self::DEVICE_GROUP_CREATE:
  83.                 return $this->canCreate();
  84.             case self::DEVICE_GROUP_EDIT:
  85.                 return ($this->canEdit() && $this->isOwner($user$subject));
  86.             case self::DEVICE_GROUP_SHOW:
  87.                 return ($this->canShow() && $this->isOwner($user$subject));
  88.             case self::DEVICE_GROUP_DELETE:
  89.                 return ($this->canDelete() && $this->isOwner($user$subject));
  90.             case self::DEVICE_GROUP_MULTI_DELETE:
  91.                 return $this->canMultiDelete();
  92.         }
  93.         throw new LogicException('Invalid attribute: ' $attribute);
  94.     }
  95.     /**
  96.      * @param User|null $user
  97.      * @param $subject
  98.      * @return bool
  99.      */
  100.     private function isOwner(User $user$subject): bool
  101.     {
  102.         if (!is_object($subject)) {
  103.             $subject $this->deviceGroupRepository->findOneBy(["id" => intval($subject)]);
  104.         }
  105.         if (!$subject instanceof DeviceGroup) {
  106.             return false;
  107.         }
  108.         return $user->getWorkspace()->getId() === $subject->getWorkspace()->getId();
  109.     }
  110.     /** Return True if have View Permission else return false
  111.      * @return bool
  112.      */
  113.     private function canView(): bool
  114.     {
  115.         if ($this->security->isGranted('ROLE_ADMIN')) {
  116.             return true;
  117.         }
  118.         if (array_key_exists('deviceGroup'$this->permissions)) {
  119.             if (in_array('View'$this->permissions['deviceGroup'])) {
  120.                 return true;
  121.             }
  122.         }
  123.         return false;
  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('deviceGroup'$this->permissions)) {
  134.             if (in_array('Edit'$this->permissions['deviceGroup'])) {
  135.                 return true;
  136.             }
  137.         }
  138.         return false;
  139.     }
  140.     /** Return True if have Edit Permission else return false
  141.      * @return bool
  142.      */
  143.     private function canShow(): bool
  144.     {
  145.         if ($this->security->isGranted('ROLE_ADMIN')) {
  146.             return true;
  147.         }
  148.         if (array_key_exists('deviceGroup'$this->permissions)) {
  149.             if (in_array('Show'$this->permissions['deviceGroup'])) {
  150.                 return true;
  151.             }
  152.         }
  153.         return false;
  154.     }
  155.     /** Return True if have Create Permission else return false
  156.      * @return bool
  157.      */
  158.     private function canCreate(): bool
  159.     {
  160.         if ($this->security->isGranted('ROLE_ADMIN')) {
  161.             return true;
  162.         }
  163.         if (array_key_exists('deviceGroup'$this->permissions)) {
  164.             if (in_array('Create'$this->permissions['deviceGroup'])) {
  165.                 return true;
  166.             }
  167.         }
  168.         return false;
  169.     }
  170.     /** Return True if have Delete Permission else return false
  171.      * @return bool
  172.      */
  173.     private function canDelete(): bool
  174.     {
  175.         if ($this->security->isGranted('ROLE_ADMIN')) {
  176.             return true;
  177.         }
  178.         if (array_key_exists('deviceGroup'$this->permissions)) {
  179.             if (in_array('Delete'$this->permissions['deviceGroup'])) {
  180.                 return true;
  181.             }
  182.         }
  183.         return false;
  184.     }
  185.     /**
  186.      * @return bool
  187.      */
  188.     private function canMultiDelete(): bool
  189.     {
  190.         if ($this->security->isGranted('ROLE_ADMIN')) {
  191.             return true;
  192.         }
  193.         if (array_key_exists('deviceGroup'$this->permissions)) {
  194.             if (in_array('Delete'$this->permissions['deviceGroup'])) {
  195.                 return true;
  196.             }
  197.         }
  198.         return false;
  199.     }
  200. }