<?php
declare(strict_types=1);
namespace App\Bundles\DiseaseCaseBundle\Security;
use App\Bundles\DiseaseCaseBundle\Entity\DiseaseCase;
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 DiseaseCaseVoter extends Voter
{
public function __construct(
private readonly OrganizationService $organizationService,
) {
}
protected function supports(string $attribute, $subject): bool
{
return in_array(
$attribute,
[
SystemPermissionEnum::SPECIFIC_DISEASE_CASE_VIEW->value,
SystemPermissionEnum::SPECIFIC_DISEASE_CASE_EDIT->value,
],
);
}
/** @param DiseaseCase $subject */
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
if (
!($this->organizationService->hasPermissionByOrganization($subject->getCreatingOrganization()) ||
$this->organizationService->hasPermissionByOrganization($subject->getInvestigatingInstitution()) ||
$this->organizationService->hasPermissionByPatientAddress($subject->getPatientLivingAddress()))
) {
return false;
}
return true;
}
}