src/Security/Authorization/Voter/EnvironmentVoter.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 EnvironmentVoter implements VoterInterface
  8. {
  9.     const VIEW 'VIEW';
  10.     const EDIT 'EDIT';
  11.     const DELETE 'DELETE';
  12.     const CREATE 'CREATE';
  13.     const CLONE = 'CLONE';
  14.     const LIST = 'LIST';
  15.     const DEPLOY 'DEPLOY';
  16.     const STOP 'STOP';
  17.     const BUILD 'BUILD';
  18.     const DBIMPORT 'DBIMPORT';
  19.     const DBLANGIMPORT 'DBLANGIMPORT';
  20.     const DBEXPORT 'DBEXPORT';
  21.     const IDPFY 'IDPFY';
  22.     const PERMAUSER 'PERMAUSER';
  23.     const VIBERTOKEN 'VIBERTOKEN';
  24.     const INITDB 'INITDB';
  25.     const CREATEUSER 'CREATEUSER';
  26.     const DEBUG 'DEBUG';
  27.     const LOGLEVEL 'LOGLEVEL';
  28.     const CREATERESOURCEPROXYUSER 'CREATERESOURCEPROXYUSER';
  29.  
  30.     public function supportsAttribute($attribute)
  31.     {
  32.  
  33.         return in_array($attribute, array(
  34.             self::VIEW,
  35.             self::EDIT,
  36.             self::DELETE,
  37.             self::CREATE,
  38.             self::CLONE,
  39.             self::LIST,
  40.             self::DEPLOY,
  41.             self::STOP,
  42.             self::BUILD,
  43.             self::DBIMPORT,
  44.             self::DBLANGIMPORT,
  45.             self::DBEXPORT,
  46.             self::IDPFY,
  47.             self::PERMAUSER,
  48.             self::VIBERTOKEN,
  49.             self::INITDB,
  50.             self::CREATEUSER,
  51.             self::DEBUG,
  52.             self::LOGLEVEL,
  53.             self::CREATERESOURCEPROXYUSER,
  54.         ));
  55.     }
  56.  
  57.     public function supportsClass($class)
  58.     {
  59.  
  60.         $supportedClass 'App\Entity\Environment';
  61.  
  62.         if (is_string($class)) {
  63.             if ($class === $supportedClass) {
  64.                 return true;
  65.             } else {
  66.                 return false;
  67.             }
  68.         }
  69.         if(is_null($class)) {
  70.             return true;
  71.         }
  72.         return $supportedClass === get_class($class) || is_subclass_of(get_class($class), $supportedClass);
  73.     }
  74.  
  75.  
  76.     public function vote(TokenInterface $token$entity, array $attributes)
  77.     {
  78.         $user $token->getUser();
  79.         if (!is_object($user)) {
  80.             return VoterInterface::ACCESS_DENIED;
  81.         }
  82.  
  83.         if (!$this->supportsClass($entity)) {
  84.             return VoterInterface::ACCESS_ABSTAIN;
  85.         }
  86.         
  87.         $attribute $attributes[0];
  88.         if(is_array($attribute)) {
  89.             $attribute $attribute[0];
  90.         };
  91.         if($user->hasRole("SuperUsers")) {
  92.             return VoterInterface::ACCESS_GRANTED;
  93.         }
  94.         switch($attribute) {
  95.             case self::LIST:
  96.                 return VoterInterface::ACCESS_GRANTED;
  97.                 break;
  98.             case self::VIEW:
  99.                 return VoterInterface::ACCESS_GRANTED;
  100.                 break;
  101.             case self::CREATE:
  102.                 if($user->hasRole("SuperUsers") || $user->hasRole("Deliveries")) {
  103.                     return VoterInterface::ACCESS_GRANTED;
  104.                 } else {
  105.                     return VoterInterface::ACCESS_DENIED;
  106.                 }
  107.                 break;
  108.             case self::CLONE:
  109.                 if($user->hasRole("SuperUsers") || $user->hasRole("Deliveries")) {
  110.                     return VoterInterface::ACCESS_GRANTED;
  111.                 } else {
  112.                     return VoterInterface::ACCESS_DENIED;
  113.                 }
  114.                 break;
  115.             case self::EDIT:
  116.                 if($user->hasRole("SuperUsers") || $user->hasRole("Deliveries") || $user->hasRole("Developers")) {
  117.                     return VoterInterface::ACCESS_GRANTED;
  118.                 } else {
  119.                     return VoterInterface::ACCESS_DENIED;
  120.                 }
  121.                 break;
  122.             case self::DELETE:
  123.                 if($user->hasRole("SuperUsers")) {
  124.                     return VoterInterface::ACCESS_GRANTED;
  125.                 } else {
  126.                     return VoterInterface::ACCESS_DENIED;
  127.                 }
  128.                 break;
  129.             case self::DEPLOY:
  130.                 if($entity->getType()->getName() != "prod") {
  131.                     return VoterInterface::ACCESS_GRANTED;
  132.                 } elseif($user->hasRole("SuperUsers") || $user->hasRole("Deliveries") || $user->hasRole("CSMs")) {
  133.                     return VoterInterface::ACCESS_GRANTED;
  134.                 } elseif($entity->getType()->getName() == "prod" && $user->hasRole("Developers") && $entity->getDebugMode() == true) {
  135.                     return VoterInterface::ACCESS_GRANTED;
  136.                 } else {
  137.                     return VoterInterface::ACCESS_DENIED;
  138.                 }
  139.                 break;
  140.             case self::STOP:
  141.                 if($entity->getType()->getName() != "prod") {
  142.                     return VoterInterface::ACCESS_GRANTED;
  143.                 } elseif($user->hasRole("SuperUsers") || $user->hasRole("Deliveries") || $user->hasRole("CSMs")) {
  144.                     return VoterInterface::ACCESS_GRANTED;
  145.                 } elseif($entity->getType()->getName() == "prod" && $user->hasRole("Developers") && $entity->getDebugMode() == true) {
  146.                     return VoterInterface::ACCESS_GRANTED;
  147.                 } else {
  148.                     return VoterInterface::ACCESS_DENIED;
  149.                 }
  150.                 break;
  151.             case self::BUILD:
  152.                 if($user->hasRole("SuperUsers") || $user->hasRole("Deliveries") || $user->hasRole("Developers") || $user->hasRole("CSMs")) {
  153.                     return VoterInterface::ACCESS_GRANTED;
  154.                 } else {
  155.                     return VoterInterface::ACCESS_DENIED;
  156.                 }
  157.                 break;
  158.             case self::DBIMPORT:
  159.                 if($user->hasRole("SuperUsers") || $user->hasRole("Deliveries") || $user->hasRole("Developers")) {
  160.                     return VoterInterface::ACCESS_GRANTED;
  161.                 } else {
  162.                     return VoterInterface::ACCESS_DENIED;
  163.                 }
  164.                 break;
  165.             case self::DBEXPORT:
  166.                 return VoterInterface::ACCESS_GRANTED;
  167.                 break;
  168.             case self::IDPFY:
  169.                 if($user->hasRole("SuperUsers") || $user->hasRole("Deliveries") || $user->hasRole("Developers") || $user->hasRole("CSMs")) {
  170.                     return VoterInterface::ACCESS_GRANTED;
  171.                 } else {
  172.                     return VoterInterface::ACCESS_DENIED;
  173.                 }
  174.                 break;
  175.             case self::DBLANGIMPORT:
  176.                 if($user->hasRole("SuperUsers") || $user->hasRole("CHEQPermaUsers")) {
  177.                     return VoterInterface::ACCESS_GRANTED;
  178.                 } else {
  179.                     return VoterInterface::ACCESS_DENIED;
  180.                 }
  181.                 break;
  182.             case self::PERMAUSER:
  183.                 if($user->hasRole("SuperUsers") || $user->hasRole("CHEQPermaUsers")) {
  184.                     return VoterInterface::ACCESS_GRANTED;
  185.                 } else {
  186.                     return VoterInterface::ACCESS_DENIED;
  187.                 }
  188.                 break;
  189.             
  190.             case self::VIBERTOKEN:
  191.                 if($user->hasRole("SuperUsers") || $user->hasRole("CHEQPermaUsers")) {
  192.                     return VoterInterface::ACCESS_GRANTED;
  193.                 } else {
  194.                     return VoterInterface::ACCESS_DENIED;
  195.                 }
  196.                 break;
  197.           case self::INITDB:
  198.                 if($user->hasRole("SuperUsers") || $user->hasRole("Deliveries")) {
  199.                     return VoterInterface::ACCESS_GRANTED;
  200.                 } else {
  201.                     return VoterInterface::ACCESS_DENIED;
  202.                 }
  203.                 break;
  204.             case self::CREATEUSER:
  205.                 if($user->hasRole("SuperUsers") || $user->hasRole("Deliveries") || $user->hasRole("Developers") || $user->hasRole("CSMs")) {
  206.                     return VoterInterface::ACCESS_GRANTED;
  207.                 } else {
  208.                     return VoterInterface::ACCESS_DENIED;
  209.                 }
  210.                 break;
  211.             case self::DEBUG:
  212.                 if($user->hasRole("SuperUsers") || $user->hasRole("Deliveries")) {
  213.                     return VoterInterface::ACCESS_GRANTED;
  214.                 } else {
  215.                     return VoterInterface::ACCESS_DENIED;
  216.                 }
  217.                 break;
  218.             case self::LOGLEVEL:
  219.                 if($entity->getType()->getName() != "prod") {
  220.                     return VoterInterface::ACCESS_GRANTED;
  221.                 } elseif($user->hasRole("SuperUsers") || $user->hasRole("Deliveries")) {
  222.                     return VoterInterface::ACCESS_GRANTED;
  223.                 } elseif($entity->getType()->getName() == "prod" && $user->hasRole("Developers") && $entity->getDebugMode() == true) {
  224.                     return VoterInterface::ACCESS_GRANTED;
  225.                 } else {
  226.                     return VoterInterface::ACCESS_DENIED;
  227.                 }
  228.             case self::CREATERESOURCEPROXYUSER:
  229.                 if($entity->getType()->getName() != "prod") {
  230.                     return VoterInterface::ACCESS_GRANTED;
  231.                 } elseif($user->hasRole("SuperUsers") || $user->hasRole("Deliveries")) {
  232.                     return VoterInterface::ACCESS_GRANTED;
  233.                 } elseif($entity->getType()->getName() == "prod" && $user->hasRole("Developers") && $entity->getDebugMode() == true) {
  234.                     return VoterInterface::ACCESS_GRANTED;
  235.                 } else {
  236.                     return VoterInterface::ACCESS_DENIED;
  237.                 }
  238.                 break;
  239.         }
  240.  
  241.         return VoterInterface::ACCESS_DENIED;
  242.     }
  243. }