src/Security/BaseCrudVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use AppBundle\Entity\CompanyAssetInterface;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  7. use App\Entity\User;
  8. class BaseCrudVoter extends Voter
  9. {
  10.     const ROLE_SUPER 'ROLE_SUPER_ADMIN';
  11.     const ROLE_ALL 'ROLE_DEFAULT_ALL';
  12.     const ROLE_READ 'ROLE_DEFAULT_READ';
  13.     const ROLE_WRITE 'ROLE_DEFAULT_WRITE';
  14.     const ROLE_DELETE 'ROLE_DEFAULT_DELETE';
  15.     const GRANT_READ 'read';
  16.     const GRANT_WRITE 'write';
  17.     const GRANT_DELETE 'delete';
  18.     private $decisionManager;
  19.     public function __construct(AccessDecisionManagerInterface $decisionManager)
  20.     {
  21.         $this->decisionManager $decisionManager;
  22.     }
  23.     protected function supports($attribute$subject)
  24.     {
  25.         // if the attribute isn't one we support, return false
  26.         if (!in_array($attribute, array(self::GRANT_READself::GRANT_WRITEself::GRANT_DELETE))) {
  27.             return false// abstain
  28.         }
  29.         return true// supports
  30.     }
  31.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  32.     {
  33.         // ROLE_SUPER_ADMIN can do anything! The power!
  34.         if ($this->decisionManager->decide($token, array(static::ROLE_SUPER))) {
  35.             return true;
  36.         }
  37.         $user $token->getUser();
  38.         if (!$user instanceof User) {
  39.             // the user must be logged in; if not, deny access
  40.             return false;
  41.         }
  42.         switch ($attribute) {
  43.             case self::GRANT_READ:
  44.                 return $this->canRead($subject$user);
  45.             case self::GRANT_WRITE:
  46.                 return $this->canWrite($subject$user);
  47.             case self::GRANT_DELETE:
  48.                 return $this->canDelete($subject$user);
  49.         }
  50.         throw new \LogicException('This code should not be reached!');
  51.     }
  52.     private function canDoAll($subjectUser $user)
  53.     {
  54.         if ($user->hasRole(static::ROLE_ALL)) {
  55.             return true;
  56.         }
  57.         return false;
  58.     }
  59.     private function canRead($subjectUser $user)
  60.     {
  61.         // if they can do all, they can show
  62.         if ($this->canDoAll($subject$user)) {
  63.             return true;
  64.         }
  65.         if ($user->hasRole(static::ROLE_READ)) {
  66.             return true;
  67.         }
  68.         return false;
  69.     }
  70.     private function canWrite($subjectUser $user)
  71.     {
  72.         // if they can do all, they can edit
  73.         if ($this->canDoAll($subject$user)) {
  74.             return true;
  75.         }
  76.         if ($user->hasRole(static::ROLE_WRITE)) {
  77.             return true;
  78.         }
  79.         return false;
  80.     }
  81.     private function canDelete($subjectUser $user)
  82.     {
  83.         // if they can do all, they can delete
  84.         if ($this->canDoAll($subject$user)) {
  85.             return true;
  86.         }
  87.         if ($user->hasRole(static::ROLE_DELETE)) {
  88.             return true;
  89.         }
  90.         return false;
  91.     }
  92. }