src/Security/Voter/WishlistVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\User;
  4. use App\Utils\Commons;
  5. use LogicException;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\Security;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. class WishlistVoter extends Voter
  11. {
  12.     const WISHLIST_VIEW 'WISHLIST_VIEW';
  13.     const WISHLIST_CREATE 'WISHLIST_CREATE';
  14.     const WISHLIST_LIKE 'WISHLIST_LIKE';
  15.     const WISHLIST_DISLIKE 'WISHLIST_DISLIKE';
  16.     /**
  17.      * @var Security
  18.      */
  19.     private $security;
  20.     /**
  21.      * @var Commons
  22.      */
  23.     private $commons;
  24.     /**
  25.      * @var array
  26.      */
  27.     private $permissions;
  28.     /**
  29.      * SupportVoter constructor.
  30.      * @param Security $security
  31.      * @param Commons $commons
  32.      */
  33.     public function __construct(Security $securityCommons $commons)
  34.     {
  35.         $this->security $security;
  36.         $this->commons $commons;
  37.         $this->permissions = [];
  38.     }
  39.     /**
  40.      * @param string $attribute
  41.      * @param mixed $subject
  42.      * @return bool
  43.      */
  44.     protected function supports($attribute$subject): bool
  45.     {
  46.         // if the attribute isn't one we support, return false
  47.         if (!in_array($attribute,
  48.             [self::WISHLIST_VIEWself::WISHLIST_LIKEself::WISHLIST_CREATEself::WISHLIST_DISLIKE]
  49.         )) {
  50.             return false;
  51.         }
  52.         return true;
  53.     }
  54.     /**
  55.      * @param string $attribute
  56.      * @param mixed $subject
  57.      * @param TokenInterface $token
  58.      * @return bool
  59.      */
  60.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  61.     {
  62.         /** @var User $user */
  63.         $user $token->getUser();
  64.         // if the user is anonymous, do not grant access
  65.         if (!$user instanceof UserInterface) {
  66.             return false;
  67.         }
  68.         $this->permissions $this->commons->getUserPermissions($user);
  69.         switch ($attribute) {
  70.             case self::WISHLIST_VIEW:
  71.                 return $this->canView();
  72.             case self::WISHLIST_CREATE:
  73.                 return $this->canCreate();
  74.             case self::WISHLIST_LIKE:
  75.                 return $this->canLike();
  76.             case self::WISHLIST_DISLIKE:
  77.                 return $this->canDislike();
  78.         }
  79.         throw new LogicException('Invalid attribute: ' $attribute);
  80.     }
  81.     /**
  82.      * Return True if have View Permission else return false
  83.      * @return bool
  84.      */
  85.     private function canView(): bool
  86.     {
  87.         if ($this->security->isGranted('ROLE_ADMIN')) {
  88.             return true;
  89.         }
  90.         if (array_key_exists('wishList'$this->permissions)) {
  91.             if (in_array('View'$this->permissions['wishList'])) {
  92.                 return true;
  93.             }
  94.         }
  95.         return false;
  96.     }
  97.     /**
  98.      * Return True if have Like Permission else return false
  99.      * @return bool
  100.      */
  101.     private function canLike(): bool
  102.     {
  103.         if ($this->security->isGranted('ROLE_ADMIN')) {
  104.             return true;
  105.         }
  106.         if (array_key_exists('wishList'$this->permissions)) {
  107.             if (in_array('Like'$this->permissions['wishList'])) {
  108.                 return true;
  109.             }
  110.         }
  111.         return false;
  112.     }
  113.     /**
  114.      * Return True if have Create Permission else return false
  115.      * @return bool
  116.      */
  117.     private function canCreate(): bool
  118.     {
  119.         if ($this->security->isGranted('ROLE_ADMIN')) {
  120.             return true;
  121.         }
  122.         if (array_key_exists('wishList'$this->permissions)) {
  123.             if (in_array('Create'$this->permissions['wishList'])) {
  124.                 return true;
  125.             }
  126.         }
  127.         return false;
  128.     }
  129.     /**
  130.      * Return True if have DisLike Permission else return false
  131.      * @return bool
  132.      */
  133.     private function canDislike(): bool
  134.     {
  135.         if ($this->security->isGranted('ROLE_ADMIN')) {
  136.             return true;
  137.         }
  138.         if (array_key_exists('wishList'$this->permissions)) {
  139.             if (in_array('Dislike'$this->permissions['wishList'])) {
  140.                 return true;
  141.             }
  142.         }
  143.         return false;
  144.     }
  145. }