<?php
namespace DW\GingerBundle\Security;
use DW\GingerBundle\Entity\Integration;
use DW\GingerBundle\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
class IntergartionVoter extends Voter
{
const VIEW = 'view';
const EDIT = 'edit';
const CREATE = 'create';
const DELETE = 'delete';
private AccessDecisionManagerInterface $decisionManager;
public function __construct(AccessDecisionManagerInterface $decisionManager)
{
$this->decisionManager = $decisionManager;
}
protected function supports($attribute, $subject): bool
{
// if the attribute isn't one we support, return false
if (!in_array($attribute, array(self::VIEW, self::EDIT, self::CREATE, self::DELETE))) {
return false;
}
// only vote on integration objects inside this voter
if (!$subject instanceof Integration) {
return false;
}
return true;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof User) {
// the user must be logged in; if not, deny access
return false;
}
return $this->decisionManager->decide($token, ['ROLE_GINGER_INTEGRATION_ADMIN']);
}
}