<?php
declare(strict_types=1);
namespace App\Bundles\AntibioticResistanceBundle\Security;
use App\Bundles\AntibioticResistanceBundle\Entity\InfectionControl\InfectionControl;
use App\Bundles\AntibioticResistanceBundle\Repository\InfectionControlRepository;
use App\Bundles\AntibioticResistanceBundle\Service\DepartmentOrganisationProvider;
use App\Bundles\OrganizationBundle\Exception\UserOrganizationNotFoundException;
use App\Bundles\OrganizationBundle\Service\UserOrganization\UserOrganizationProvider;
use App\Bundles\UserBundle\Enum\SystemPermissionEnum;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class InfectionControlVoter extends Voter
{
public function __construct(
private readonly UserOrganizationProvider $userOrganizationProvider,
private readonly InfectionControlRepository $infectionControlRepository,
private readonly DepartmentOrganisationProvider $departmentOrganisationProvider,
) {
}
/**
* @param InfectionControl $subject
*/
protected function supports(string $attribute, $subject): bool
{
return $attribute === SystemPermissionEnum::SINGLE_INFECTION_CONTROL_ACCESS->value;
}
/**
* @param InfectionControl $subject
*
* @throws UserOrganizationNotFoundException
*/
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$userOrganization = $this->userOrganizationProvider->provideFromSession();
$organization = $userOrganization->getOrganization();
return $this->infectionControlRepository->isExistsInOrganizations(
$subject,
$this->departmentOrganisationProvider->provideAvailable($organization)
);
}
}