<?php
namespace App\Security\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
use App\Manager\Dossier\DossierListManager;
use Doctrine\Persistence\ManagerRegistry;
class AstreinteViriaVoter extends Voter
{
const CAN_SEE_ASTREINTE_VIRIA = "can_see_astreinte_viria";
const ACTIONS = [
self::CAN_SEE_ASTREINTE_VIRIA
];
const ACCESSIBLE_DOSSIER_IDS = [333];
private ManagerRegistry $doctrine;
public function __construct(ManagerRegistry $doctrine)
{
$this->doctrine = $doctrine; // Stockage de la dépendance dans une propriété privée
}
protected function supports(string $attribute, $subject): bool
{
return in_array($attribute, self::ACTIONS);
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
switch ($attribute) {
case self::CAN_SEE_ASTREINTE_VIRIA:
return $this->canSeeAstreinteViria($user, $subject);
break;
default:
throw new \LogicException('This code should not be reached!');
break;
}
return false;
}
// Vérifie si l'utilisateur peut accéder à l'astreinte Viria
private function canSeeAstreinteViria(UserInterface $user, $subject): bool
{
$em = $this->doctrine->getManager();
$dossiers = DossierListManager::getDossierByCompteClientDQL($em, $user->getId());
if (empty($dossiers)) {
return false;
}
foreach ($dossiers as $dossier) {
if (in_array($dossier->getIdDossier(), self::ACCESSIBLE_DOSSIER_IDS)) {
return true;
}
}
return false;
}
}