<?php
declare(strict_types=1);
namespace App\Bundles\EpidemiologicalSurveillanceBundle\Security\Voter;
use App\Bundles\EpidemiologicalSurveillanceBundle\Entity\SurveillanceCase;
use App\Bundles\OrganizationBundle\Service\Organization\OrganizationService;
use App\Bundles\UserBundle\Enum\SystemPermissionEnum;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class SurveillanceCaseVoter extends Voter
{
public function __construct(
private readonly OrganizationService $organizationService,
) {
}
/**
* @param string $attribute
* @param SurveillanceCase $subject
*
* @return bool
*/
protected function supports(string $attribute, $subject): bool
{
return $attribute === SystemPermissionEnum::EPIDEMIOLOGICAL_SURVEILLANCE_CASE_VIEW->value;
}
/**
* @param string $attribute
* @param SurveillanceCase $subject
* @param TokenInterface $token
*
* @return bool
*/
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
return $this->organizationService->hasPermissionByLocation($subject->getLocation());
}
}