<?php
namespace App\Security\Voter;
use App\Service\VoterSecurityService;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class StationVoter extends Voter
{
public const EDIT = 'EDIT';
public const VIEW = 'VIEW';
public const VIEW_PREMIUM = 'VIEW_PREMIUM';
public function __construct(private readonly VoterSecurityService $voterSecurityService)
{
}
protected function supports(string $attribute, $subject): bool
{
// replace with your own logic
// https://symfony.com/doc/current/security/voters.html
return in_array($attribute, [self::EDIT, self::VIEW, self::VIEW_PREMIUM])
&& $subject instanceof \App\Entity\Station;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$this->voterSecurityService->isUserAllowed($user)) {
return false;
}
if (!$this->voterSecurityService->isUserInElement($user, $subject)) {
return false;
}
return match ($attribute) {
self::VIEW => true,
self::VIEW_PREMIUM => $user->isPremium(),
self::EDIT => $this->voterSecurityService->allowedEditElement($subject),
default => false,
};
}
}