<?php
namespace App\Security\Authorization\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
class EnvironmentVoter implements VoterInterface
{
const VIEW = 'VIEW';
const EDIT = 'EDIT';
const DELETE = 'DELETE';
const CREATE = 'CREATE';
const CLONE = 'CLONE';
const LIST = 'LIST';
const DEPLOY = 'DEPLOY';
const STOP = 'STOP';
const BUILD = 'BUILD';
const DBIMPORT = 'DBIMPORT';
const DBLANGIMPORT = 'DBLANGIMPORT';
const DBEXPORT = 'DBEXPORT';
const IDPFY = 'IDPFY';
const PERMAUSER = 'PERMAUSER';
const VIBERTOKEN = 'VIBERTOKEN';
const INITDB = 'INITDB';
const CREATEUSER = 'CREATEUSER';
const DEBUG = 'DEBUG';
const LOGLEVEL = 'LOGLEVEL';
const CREATERESOURCEPROXYUSER = 'CREATERESOURCEPROXYUSER';
public function supportsAttribute($attribute)
{
return in_array($attribute, array(
self::VIEW,
self::EDIT,
self::DELETE,
self::CREATE,
self::CLONE,
self::LIST,
self::DEPLOY,
self::STOP,
self::BUILD,
self::DBIMPORT,
self::DBLANGIMPORT,
self::DBEXPORT,
self::IDPFY,
self::PERMAUSER,
self::VIBERTOKEN,
self::INITDB,
self::CREATEUSER,
self::DEBUG,
self::LOGLEVEL,
self::CREATERESOURCEPROXYUSER,
));
}
public function supportsClass($class)
{
$supportedClass = 'App\Entity\Environment';
if (is_string($class)) {
if ($class === $supportedClass) {
return true;
} else {
return false;
}
}
if(is_null($class)) {
return true;
}
return $supportedClass === get_class($class) || is_subclass_of(get_class($class), $supportedClass);
}
public function vote(TokenInterface $token, $entity, array $attributes)
{
$user = $token->getUser();
if (!is_object($user)) {
return VoterInterface::ACCESS_DENIED;
}
if (!$this->supportsClass($entity)) {
return VoterInterface::ACCESS_ABSTAIN;
}
$attribute = $attributes[0];
if(is_array($attribute)) {
$attribute = $attribute[0];
};
if($user->hasRole("SuperUsers")) {
return VoterInterface::ACCESS_GRANTED;
}
switch($attribute) {
case self::LIST:
return VoterInterface::ACCESS_GRANTED;
break;
case self::VIEW:
return VoterInterface::ACCESS_GRANTED;
break;
case self::CREATE:
if($user->hasRole("SuperUsers") || $user->hasRole("Deliveries")) {
return VoterInterface::ACCESS_GRANTED;
} else {
return VoterInterface::ACCESS_DENIED;
}
break;
case self::CLONE:
if($user->hasRole("SuperUsers") || $user->hasRole("Deliveries")) {
return VoterInterface::ACCESS_GRANTED;
} else {
return VoterInterface::ACCESS_DENIED;
}
break;
case self::EDIT:
if($user->hasRole("SuperUsers") || $user->hasRole("Deliveries") || $user->hasRole("Developers")) {
return VoterInterface::ACCESS_GRANTED;
} else {
return VoterInterface::ACCESS_DENIED;
}
break;
case self::DELETE:
if($user->hasRole("SuperUsers")) {
return VoterInterface::ACCESS_GRANTED;
} else {
return VoterInterface::ACCESS_DENIED;
}
break;
case self::DEPLOY:
if($entity->getType()->getName() != "prod") {
return VoterInterface::ACCESS_GRANTED;
} elseif($user->hasRole("SuperUsers") || $user->hasRole("Deliveries") || $user->hasRole("CSMs")) {
return VoterInterface::ACCESS_GRANTED;
} elseif($entity->getType()->getName() == "prod" && $user->hasRole("Developers") && $entity->getDebugMode() == true) {
return VoterInterface::ACCESS_GRANTED;
} else {
return VoterInterface::ACCESS_DENIED;
}
break;
case self::STOP:
if($entity->getType()->getName() != "prod") {
return VoterInterface::ACCESS_GRANTED;
} elseif($user->hasRole("SuperUsers") || $user->hasRole("Deliveries") || $user->hasRole("CSMs")) {
return VoterInterface::ACCESS_GRANTED;
} elseif($entity->getType()->getName() == "prod" && $user->hasRole("Developers") && $entity->getDebugMode() == true) {
return VoterInterface::ACCESS_GRANTED;
} else {
return VoterInterface::ACCESS_DENIED;
}
break;
case self::BUILD:
if($user->hasRole("SuperUsers") || $user->hasRole("Deliveries") || $user->hasRole("Developers") || $user->hasRole("CSMs")) {
return VoterInterface::ACCESS_GRANTED;
} else {
return VoterInterface::ACCESS_DENIED;
}
break;
case self::DBIMPORT:
if($user->hasRole("SuperUsers") || $user->hasRole("Deliveries") || $user->hasRole("Developers")) {
return VoterInterface::ACCESS_GRANTED;
} else {
return VoterInterface::ACCESS_DENIED;
}
break;
case self::DBEXPORT:
return VoterInterface::ACCESS_GRANTED;
break;
case self::IDPFY:
if($user->hasRole("SuperUsers") || $user->hasRole("Deliveries") || $user->hasRole("Developers") || $user->hasRole("CSMs")) {
return VoterInterface::ACCESS_GRANTED;
} else {
return VoterInterface::ACCESS_DENIED;
}
break;
case self::DBLANGIMPORT:
if($user->hasRole("SuperUsers") || $user->hasRole("CHEQPermaUsers")) {
return VoterInterface::ACCESS_GRANTED;
} else {
return VoterInterface::ACCESS_DENIED;
}
break;
case self::PERMAUSER:
if($user->hasRole("SuperUsers") || $user->hasRole("CHEQPermaUsers")) {
return VoterInterface::ACCESS_GRANTED;
} else {
return VoterInterface::ACCESS_DENIED;
}
break;
case self::VIBERTOKEN:
if($user->hasRole("SuperUsers") || $user->hasRole("CHEQPermaUsers")) {
return VoterInterface::ACCESS_GRANTED;
} else {
return VoterInterface::ACCESS_DENIED;
}
break;
case self::INITDB:
if($user->hasRole("SuperUsers") || $user->hasRole("Deliveries")) {
return VoterInterface::ACCESS_GRANTED;
} else {
return VoterInterface::ACCESS_DENIED;
}
break;
case self::CREATEUSER:
if($user->hasRole("SuperUsers") || $user->hasRole("Deliveries") || $user->hasRole("Developers") || $user->hasRole("CSMs")) {
return VoterInterface::ACCESS_GRANTED;
} else {
return VoterInterface::ACCESS_DENIED;
}
break;
case self::DEBUG:
if($user->hasRole("SuperUsers") || $user->hasRole("Deliveries")) {
return VoterInterface::ACCESS_GRANTED;
} else {
return VoterInterface::ACCESS_DENIED;
}
break;
case self::LOGLEVEL:
if($entity->getType()->getName() != "prod") {
return VoterInterface::ACCESS_GRANTED;
} elseif($user->hasRole("SuperUsers") || $user->hasRole("Deliveries")) {
return VoterInterface::ACCESS_GRANTED;
} elseif($entity->getType()->getName() == "prod" && $user->hasRole("Developers") && $entity->getDebugMode() == true) {
return VoterInterface::ACCESS_GRANTED;
} else {
return VoterInterface::ACCESS_DENIED;
}
case self::CREATERESOURCEPROXYUSER:
if($entity->getType()->getName() != "prod") {
return VoterInterface::ACCESS_GRANTED;
} elseif($user->hasRole("SuperUsers") || $user->hasRole("Deliveries")) {
return VoterInterface::ACCESS_GRANTED;
} elseif($entity->getType()->getName() == "prod" && $user->hasRole("Developers") && $entity->getDebugMode() == true) {
return VoterInterface::ACCESS_GRANTED;
} else {
return VoterInterface::ACCESS_DENIED;
}
break;
}
return VoterInterface::ACCESS_DENIED;
}
}