src/Security/Voter/PurchaseItemVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  4. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  5. use Symfony\Component\Security\Core\User\UserInterface;
  6. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  7. use App\Entity\PurchaseItem;
  8. class PurchaseItemVoter extends Voter
  9. {
  10.     const ROLE_SUPER 'ROLE_ADMIN';
  11.     const ROLE_ALL 'ROLE_PURCHASE';
  12.     const ROLE_CREATE 'ROLE_PURCHASE_CREATE';
  13.     const ROLE_SHOW 'ROLE_USER';
  14.     const ROLE_EDIT 'ROLE_PURCHASE_EDIT';
  15.     const ROLE_DELETE 'ROLE_PURCHASE_DELETE';
  16.     const GRANT_CREATE 'create';
  17.     const GRANT_SHOW 'show';
  18.     const GRANT_EDIT 'edit';
  19.     const GRANT_DELETE 'delete';
  20.     private $decisionManager;
  21.     public function __construct(AccessDecisionManagerInterface $decisionManager)
  22.     {
  23.         $this->decisionManager $decisionManager;
  24.     }
  25.     protected function supports($attribute$subject)
  26.     {
  27.         return $subject instanceof PurchaseItem && in_array($attribute, [self::GRANT_SHOWself::GRANT_CREATEself::GRANT_EDITself::GRANT_DELETE], true);
  28.     }
  29.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  30.     {
  31.         // ROLE_SUPER_ADMIN can do anything! The power!
  32.         if ($this->decisionManager->decide($token, array(static::ROLE_SUPER))) {
  33.             return true;
  34.         }
  35.         $user $token->getUser();
  36.         switch ($attribute) {
  37.             case self::GRANT_CREATE:
  38.                 return $this->canCreate($user$subject);
  39.             case self::GRANT_SHOW:
  40.                 return $this->canShow($user$subject);
  41.             case self::GRANT_EDIT:
  42.                 return $this->canEdit($user$subject);
  43.             case self::GRANT_DELETE:
  44.                 return $this->canDelete($user$subject);
  45.         }
  46.         throw new \LogicException('This code should not be reached!');
  47.     }
  48.     private function canDoAll($user$subject)
  49.     {
  50.         if ($user->hasRole(static::ROLE_ALL)) {
  51.             return true;
  52.         }
  53.         return false;
  54.     }
  55.     private function canCreate($user$subject)
  56.     {
  57.         // if they can do all, they can edit
  58.         if ($this->canDoAll($user$subject)) {
  59.             return true;
  60.         }
  61.         if ($user->hasRole(static::ROLE_CREATE)) {
  62.             return true;
  63.         }
  64.         return false;
  65.     }
  66.     private function canShow($user$subject)
  67.     {
  68.         // if they can do all, they can show
  69.         if ($this->canDoAll($user$subject)) {
  70.             return true;
  71.         }
  72.         if ($user->hasRole(static::ROLE_SHOW)) {
  73.             return true;
  74.         }
  75.         return false;
  76.     }
  77.     private function canEdit($user$subject)
  78.     {
  79.         // if they can do all, they can edit
  80.         if ($this->canDoAll($user$subject)) {
  81.             return true;
  82.         }
  83.         if ($user->hasRole(static::ROLE_EDIT)) {
  84.             return true;
  85.         }
  86.         return false;
  87.     }
  88.     private function canDelete($user$subject)
  89.     {
  90.         // if they can do all, they can delete
  91.         if ($this->canDoAll($user$subject)) {
  92.             return true;
  93.         }
  94.         if ($user->hasRole(static::ROLE_DELETE)) {
  95.             return true;
  96.         }
  97.         return false;
  98.     }
  99. }