<?php
declare(strict_types=1);
/*
* Copyright (C) Office National de Publication et de Communication - All Rights Reserved
*
* Unauthorized copying of this file, via any medium is strictly prohibited by law
* This file is proprietary and confidential
*/
namespace App\Security\Voter;
use App\Entity\Etablissement;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
final class EtablissementVoter extends Voter
{
public const PLACE_DISPONIBLE = 'place-disponible';
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
protected function supports($attribute, $subject): bool
{
return $subject instanceof Etablissement && \in_array($attribute, [
self::PLACE_DISPONIBLE,
], true);
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
// Utilisateur doit être authentifié
if (!$token->getUser() instanceof UserInterface) {
return false;
}
switch ($attribute) {
case self::PLACE_DISPONIBLE:
return $this->canPlaceDisponible($subject, $token);
}
return false;
}
private function canPlaceDisponible(Etablissement $etablissement, TokenInterface $token): bool
{
if (!$this->security->isGranted('ROLE_RESPONSABLE_ETABLISSEMENT')) {
return false;
}
return $token->getUser()->getEtablissementsResponsable()->contains($etablissement);
}
}