<?php
declare(strict_types=1);
namespace App\Bundles\AntibioticResistanceBundle\Security;
use App\Bundles\AntibioticResistanceBundle\Entity\Bacteriological\BacteriologicalExperiment;
use App\Bundles\AntibioticResistanceBundle\Repository\BacteriologicalExperimentRepository;
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 BacteriologicalExperimentVoter extends Voter
{
public function __construct(
private readonly UserOrganizationProvider $userOrganizationProvider,
private readonly BacteriologicalExperimentRepository $bacteriologicalExperimentRepository,
private readonly DepartmentOrganisationProvider $departmentOrganisationProvider,
) {
}
/**
* @param BacteriologicalExperiment $subject
*/
protected function supports(string $attribute, $subject): bool
{
return $attribute === SystemPermissionEnum::SINGLE_BACTERIOLOGICAL_EXPERIMENT_ACCESS->value;
}
/**
* @param BacteriologicalExperiment $subject
*
* @throws UserOrganizationNotFoundException
*/
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$userOrganization = $this->userOrganizationProvider->provideFromSession();
$organization = $userOrganization->getOrganization();
return $this->bacteriologicalExperimentRepository->isExistsInOrganizations(
$subject,
$this->departmentOrganisationProvider->provideAvailable($organization)
);
}
}