src/Security/Authorization/Voter/ProjectVoter.php line 8

Open in your IDE?
  1. <?php
  2. namespace App\Security\Authorization\Voter;
  3.  
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  6.  
  7. class ProjectVoter implements VoterInterface
  8. {
  9.     const VIEW 'VIEW';
  10.     const EDIT 'EDIT';
  11.     const DELETE 'DELETE';
  12.     const CREATE 'CREATE';
  13.     const LIST = 'LIST';
  14.     const CREATE_ATLASSIAN 'CREATE_ATLASSIAN';
  15.     const INIT_CHEQ 'INIT_CHEQ';
  16.  
  17.     public function supportsAttribute($attribute)
  18.     {
  19.  
  20.         return in_array($attribute, array(
  21.             self::VIEW,
  22.             self::EDIT,
  23.             self::DELETE,
  24.             self::CREATE,
  25.             self::LIST,
  26.             self::CREATE_ATLASSIAN,
  27.             self::INIT_CHEQ,
  28.         ));
  29.     }
  30.  
  31.     public function supportsClass($class)
  32.     {
  33.  
  34.         $supportedClass 'App\Entity\Project';
  35.  
  36.         if (is_string($class)) {
  37.             if ($class === $supportedClass) {
  38.                 return true;
  39.             } else {
  40.                 return false;
  41.             }
  42.         }
  43.         if(is_null($class)) {
  44.             return true;
  45.         }
  46.         return $supportedClass === get_class($class) || is_subclass_of(get_class($class), $supportedClass);
  47.     }
  48.  
  49.  
  50.     public function vote(TokenInterface $token$entity, array $attributes)
  51.     {
  52.         $user $token->getUser();
  53.         if (!is_object($user)) {
  54.             return VoterInterface::ACCESS_DENIED;
  55.         }
  56.  
  57.         if (!$this->supportsClass($entity)) {
  58.             return VoterInterface::ACCESS_ABSTAIN;
  59.         }
  60.         
  61.         $attribute $attributes[0];
  62.         if(is_array($attribute)) {
  63.             $attribute $attribute[0];
  64.         };
  65.         if($user->hasRole("SuperUsers")) {
  66.             return VoterInterface::ACCESS_GRANTED;
  67.         }
  68.         switch($attribute) {
  69.             case self::LIST:
  70.                 return VoterInterface::ACCESS_GRANTED;
  71.                 break;
  72.             case self::VIEW:
  73.                 return VoterInterface::ACCESS_GRANTED;
  74.                 break;
  75.             case self::CREATE:
  76.                 if($user->hasRole("SuperUsers") || $user->hasRole("Deliveries")) {
  77.                     return VoterInterface::ACCESS_GRANTED;
  78.                 } else {
  79.                     return VoterInterface::ACCESS_DENIED;
  80.                 }
  81.                 break;
  82.             case self::EDIT:
  83.                 if($user->hasRole("SuperUsers") || $user->hasRole("Deliveries") || $user->hasRole("Developers")) {
  84.                     return VoterInterface::ACCESS_GRANTED;
  85.                 } else {
  86.                     return VoterInterface::ACCESS_DENIED;
  87.                 }
  88.                 break;
  89.             case self::DELETE:
  90.                 if($user->hasRole("SuperUsers")) {
  91.                     return VoterInterface::ACCESS_GRANTED;
  92.                 } else {
  93.                     return VoterInterface::ACCESS_DENIED;
  94.                 }
  95.                 break;
  96.             case self::CREATE_ATLASSIAN:
  97.                 if($user->hasRole("SuperUsers") || $user->hasRole("Deliveries")) {
  98.                     return VoterInterface::ACCESS_GRANTED;
  99.                 } else {
  100.                     return VoterInterface::ACCESS_DENIED;
  101.                 }
  102.                 break;
  103.             case self::INIT_CHEQ:
  104.                 if($user->hasRole("SuperUsers") || $user->hasRole("Deliveries")) {
  105.                     return VoterInterface::ACCESS_GRANTED;
  106.                 } else {
  107.                     return VoterInterface::ACCESS_DENIED;
  108.                 }
  109.                 break;
  110.         }
  111.  
  112.         return VoterInterface::ACCESS_DENIED;
  113.     }
  114. }