<?php
namespace App\Voter\__Accounting;
use App\Entity\__Marketing\Departure\Departure;
use App\Voter\VoterTrait;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class AccountingDepartureVoter extends Voter
{
use VoterTrait;
const LIST = "ACCOUNTING_DEPARTURE_LIST";
const DETAILS = "ACCOUNTING_DEPARTURE_DETAILS";
protected function supports($attribute, $subject): bool
{
return in_array($attribute, [
self::LIST,
self::DETAILS,
]) && ($subject === null || $subject instanceof Departure);
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$loggedUser = $token->getUser();
if (!$loggedUser instanceof UserInterface) {
return false;
}
switch ($attribute) {
case self::LIST:
return $this->voteOnList($loggedUser);
case self::DETAILS:
return $this->voteOnDetails($subject, $loggedUser);
}
return false;
}
private function voteOnList(UserInterface $loggedUser): bool
{
return in_array(self::LIST, $loggedUser->getRoles());
}
private function voteOnDetails(?Departure $departure, UserInterface $loggedUser): bool
{
if (null === $departure) {
return false;
}
return in_array(self::DETAILS, $loggedUser->getRoles());
}
}