<?php
namespace App\Security\Authorization\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
class ProjectVoter implements VoterInterface
{
const VIEW = 'VIEW';
const EDIT = 'EDIT';
const DELETE = 'DELETE';
const CREATE = 'CREATE';
const LIST = 'LIST';
const CREATE_ATLASSIAN = 'CREATE_ATLASSIAN';
const INIT_CHEQ = 'INIT_CHEQ';
public function supportsAttribute($attribute)
{
return in_array($attribute, array(
self::VIEW,
self::EDIT,
self::DELETE,
self::CREATE,
self::LIST,
self::CREATE_ATLASSIAN,
self::INIT_CHEQ,
));
}
public function supportsClass($class)
{
$supportedClass = 'App\Entity\Project';
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::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::CREATE_ATLASSIAN:
if($user->hasRole("SuperUsers") || $user->hasRole("Deliveries")) {
return VoterInterface::ACCESS_GRANTED;
} else {
return VoterInterface::ACCESS_DENIED;
}
break;
case self::INIT_CHEQ:
if($user->hasRole("SuperUsers") || $user->hasRole("Deliveries")) {
return VoterInterface::ACCESS_GRANTED;
} else {
return VoterInterface::ACCESS_DENIED;
}
break;
}
return VoterInterface::ACCESS_DENIED;
}
}