src/Security/Voter/DeviceVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Device;
  4. use App\Entity\User;
  5. use App\Repository\DeviceRepository;
  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 DeviceVoter extends Voter
  13. {
  14.     const DEVICE_VIEW 'DEVICE_VIEW';
  15.     const DEVICE_SHOW 'DEVICE_SHOW';
  16.     const DEVICE_CREATE 'DEVICE_CREATE';
  17.     const DEVICE_EDIT 'DEVICE_EDIT';
  18.     const DEVICE_GROUPING 'DEVICE_GROUPING';
  19.     const DEVICE_ADD_TO_BLACKLIST 'DEVICE_ADD_TO_BLACKLIST';
  20.     const DEVICE_UNGROUP 'DEVICE_UNGROUP';
  21.     const DEVICE_MULTI_GROUP 'DEVICE_MULTI_GROUP';
  22.     const DEVICE_MULTI_BLACKLIST 'DEVICE_MULTI_BLACKLIST';
  23.     const DEVICE_MULTI_UN_BLACKLIST 'DEVICE_MULTI_UN_BLACKLIST';
  24.     const DEVICE_MULTI_UNGROUP 'DEVICE_MULTI_UNGROUP';
  25.     /**
  26.      * @var Security
  27.      */
  28.     private $security;
  29.     /**
  30.      * @var Commons
  31.      */
  32.     private $commons;
  33.     /**
  34.      * @var array
  35.      */
  36.     private $permissions;
  37.     /**
  38.      * @var DeviceRepository
  39.      */
  40.     private $deviceRepository;
  41.     /**
  42.      * DeviceVoter constructor.
  43.      * @param Security $security
  44.      * @param Commons $commons
  45.      * @param DeviceRepository $deviceRepository
  46.      */
  47.     public function __construct(Security $securityCommons $commonsDeviceRepository $deviceRepository)
  48.     {
  49.         $this->security $security;
  50.         $this->commons $commons;
  51.         $this->permissions = [];
  52.         $this->deviceRepository $deviceRepository;
  53.     }
  54.     /**
  55.      * @param string $attribute
  56.      * @param mixed $subject
  57.      * @return bool
  58.      */
  59.     protected function supports($attribute$subject): bool
  60.     {
  61.         // if the attribute isn't one we support, return false
  62.         if (!in_array($attribute, [self::DEVICE_VIEWself::DEVICE_UNGROUPself::DEVICE_SHOWself::DEVICE_EDIT,
  63.             self::DEVICE_CREATEself::DEVICE_ADD_TO_BLACKLISTself::DEVICE_GROUPING,
  64.             self::DEVICE_MULTI_GROUPself::DEVICE_MULTI_BLACKLISTself::DEVICE_MULTI_UN_BLACKLISTself::DEVICE_MULTI_UNGROUP])) {
  65.             return false;
  66.         }
  67.         return true;
  68.     }
  69.     /**
  70.      * @param string $attribute
  71.      * @param mixed $subject
  72.      * @param TokenInterface $token
  73.      * @return bool
  74.      */
  75.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  76.     {
  77.         /** @var User $user */
  78.         $user $token->getUser();
  79.         // if the user is anonymous, do not grant access
  80.         if (!$user instanceof UserInterface) {
  81.             return false;
  82.         }
  83.         $this->permissions $this->commons->getUserPermissions($user);
  84.         switch ($attribute) {
  85.             case self::DEVICE_VIEW:
  86.                 return $this->canView();
  87.             case self::DEVICE_CREATE:
  88.                 return $this->canCreate();
  89.             case self::DEVICE_EDIT:
  90.                 return ($this->canEdit() && $this->isOwner($user$subject));
  91.             case self::DEVICE_SHOW:
  92.                 return ($this->canShow() && $this->isOwner($user$subject));
  93.             case self::DEVICE_GROUPING:
  94.                 return ($this->canGroup() && $this->isOwner($user$subject));
  95.             case self::DEVICE_ADD_TO_BLACKLIST:
  96.                 return ($this->canAddToBlacklist() && $this->isOwner($user$subject));
  97.             case self::DEVICE_UNGROUP:
  98.                 return ($this->canUngroup() && $this->isOwner($user$subject));
  99.             case self::DEVICE_MULTI_GROUP:
  100.                 return $this->canMultiGroup();
  101.             case self::DEVICE_MULTI_UNGROUP:
  102.                 return $this->canMultiUnGroup();
  103.             case self::DEVICE_MULTI_BLACKLIST:
  104.                 return $this->canMultiBlacklist();
  105.             case self::DEVICE_MULTI_UN_BLACKLIST:
  106.                 return $this->canMultiUnBlacklist();
  107.         }
  108.         throw new LogicException('Invalid attribute: ' $attribute);
  109.     }
  110.     /**
  111.      * @param User|null $user
  112.      * @param $subject
  113.      * @return bool
  114.      */
  115.     private function isOwner(User $user$subject): bool
  116.     {
  117.         if (!is_object($subject)) {
  118.             $subject $this->deviceRepository->findOneBy(["id" => intval($subject)]);
  119.         }
  120.         if (!$subject instanceof Device) {
  121.             return false;
  122.         }
  123.         return $user->getWorkspace()->getId() === $subject->getWorkspace()->getId();
  124.     }
  125.     /** Return True if have View Permission else return false
  126.      * @return bool
  127.      */
  128.     private function canView(): bool
  129.     {
  130.         if ($this->security->isGranted('ROLE_ADMIN')) {
  131.             return true;
  132.         }
  133.         if (array_key_exists('device'$this->permissions)) {
  134.             if (in_array('View'$this->permissions['device'])) {
  135.                 return true;
  136.             }
  137.         }
  138.         return false;
  139.     }
  140.     /** Return True if have View 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('device'$this->permissions)) {
  149.             if (in_array('Show'$this->permissions['device'])) {
  150.                 return true;
  151.             }
  152.         }
  153.         return false;
  154.     }
  155.     /** Return True if have Edit Permission else return false
  156.      * @return bool
  157.      */
  158.     private function canEdit(): bool
  159.     {
  160.         if ($this->security->isGranted('ROLE_ADMIN')) {
  161.             return true;
  162.         }
  163.         if (array_key_exists('device'$this->permissions)) {
  164.             if (in_array('Edit'$this->permissions['device'])) {
  165.                 return true;
  166.             }
  167.         }
  168.         return false;
  169.     }
  170.     /** Return True if have Create Permission else return false */
  171.     private function canCreate(): bool
  172.     {
  173.         if ($this->security->isGranted('ROLE_ADMIN')) {
  174.             return true;
  175.         }
  176.         if (array_key_exists('device'$this->permissions)) {
  177.             if (in_array('Create'$this->permissions['device'])) {
  178.                 return true;
  179.             }
  180.         }
  181.         return false;
  182.     }
  183.     /**
  184.      * Return True if have Grouping Permission else return false
  185.      * @return bool
  186.      */
  187.     private function canGroup(): bool
  188.     {
  189.         if ($this->security->isGranted('ROLE_ADMIN')) {
  190.             return true;
  191.         }
  192.         if (array_key_exists('device'$this->permissions)) {
  193.             if (in_array('GROUPING'$this->permissions['device'])) {
  194.                 return true;
  195.             }
  196.         }
  197.         return false;
  198.     }
  199.     /**
  200.      * Return True if have UnGrouping Permission else return false
  201.      * @return bool
  202.      */
  203.     private function canUngroup(): bool
  204.     {
  205.         if ($this->security->isGranted('ROLE_ADMIN')) {
  206.             return true;
  207.         }
  208.         if (array_key_exists('device'$this->permissions)) {
  209.             if (in_array('UNGROUP'$this->permissions['device'])) {
  210.                 return true;
  211.             }
  212.         }
  213.         return false;
  214.     }
  215.     /** Return True if have Add to Blacklist Permission else return false */
  216.     private function canAddToBlacklist(): bool
  217.     {
  218.         if ($this->security->isGranted('ROLE_ADMIN')) {
  219.             return true;
  220.         }
  221.         if (array_key_exists('device'$this->permissions)) {
  222.             if (in_array('ADD_TO_BLACKLIST'$this->permissions['device'])) {
  223.                 return true;
  224.             }
  225.         }
  226.         return false;
  227.     }
  228.     /**
  229.      * @return bool
  230.      */
  231.     private function canMultiGroup(): bool
  232.     {
  233.         if ($this->security->isGranted('ROLE_ADMIN')) {
  234.             return true;
  235.         }
  236.         if (array_key_exists('device'$this->permissions)) {
  237.             if (in_array('GROUPING'$this->permissions['device'])) {
  238.                 return true;
  239.             }
  240.         }
  241.         return false;
  242.     }
  243.     /**
  244.      * @return bool
  245.      */
  246.     private function canMultiUnBlacklist(): bool
  247.     {
  248.         if ($this->security->isGranted('ROLE_ADMIN')) {
  249.             return true;
  250.         }
  251.         if (array_key_exists('device'$this->permissions)) {
  252.             if (in_array('ADD_TO_BLACKLIST'$this->permissions['device'])) {
  253.                 return true;
  254.             }
  255.         }
  256.         return false;
  257.     }
  258.     /**
  259.      * @return bool
  260.      */
  261.     private function canMultiBlacklist(): bool
  262.     {
  263.         if ($this->security->isGranted('ROLE_ADMIN')) {
  264.             return true;
  265.         }
  266.         if (array_key_exists('device'$this->permissions)) {
  267.             if (in_array('ADD_TO_BLACKLIST'$this->permissions['device'])) {
  268.                 return true;
  269.             }
  270.         }
  271.         return false;
  272.     }
  273.     /**
  274.      * @return bool
  275.      */
  276.     private function canMultiUnGroup(): bool
  277.     {
  278.         if ($this->security->isGranted('ROLE_ADMIN')) {
  279.             return true;
  280.         }
  281.         if (array_key_exists('device'$this->permissions)) {
  282.             if (in_array('UNGROUP'$this->permissions['device'])) {
  283.                 return true;
  284.             }
  285.         }
  286.         return false;
  287.     }
  288. }