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