<?php
namespace App\Security\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
class SyncVoter extends Voter
{
public const EDIT = 'POST_EDIT';
public const VIEW = 'POST_VIEW';
public const ADD = 'POST_ADD';
public const DELETE = 'POST_DELETE';
public function __construct( private readonly Security $security ) {}
protected function supports(string $attribute, $subject): bool
{
return in_array($attribute, [self::VIEW, self::DELETE])
&& $subject instanceof \App\Entity\Sync;
}
/* attribute = accion, y subject es el objeto */
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
if ($this->security->isGranted('ROLE_ADMIN'))
return true;
if ($this->security->isGranted('ROLE_MANAGER'))
return true;
/* respuesta segura al ANON */
return false;
}
}