<?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 VideoVoter extends Voter
{
public const EDIT = 'edit';
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
protected function supports($attribute, $subject): bool
{
return $subject instanceof Etablissement && \in_array($attribute, [
self::EDIT,
], true);
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
// Utilisateur doit être authentifié
if (!$token->getUser() instanceof UserInterface) {
return false;
}
if ($this->security->isGranted('ROLE_ADMIN')) {
return true;
}
switch ($attribute) {
case self::EDIT:
return $this->security->isGranted('ROLE_REDACTEUR');
}
return false;
}
}