<?php
namespace App\Security;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class AgencyVoter extends Voter
{
const ACCESS = 'access';
/**
* @var AccessDecisionManagerInterface
*/
private $decisionManager;
public function __construct(AccessDecisionManagerInterface $decisionManager)
{
$this->decisionManager = $decisionManager;
}
protected function supports(string $attribute, mixed $subject): bool
{
if (!in_array($attribute, array(self::ACCESS))) {
return false;
}
return true;
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
$user = $token->getUser();
$actualAgency = $subject;
switch ($attribute) {
case self::ACCESS:
if ($user->canEditAgency($actualAgency)){
return true;
}
break;
}
return false;
}
}