src/Security/Voter/TexteReferencementVoter.php line 20

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * Copyright (C) Office National de Publication et de Communication - All Rights Reserved
  5.  *
  6.  * Unauthorized copying of this file, via any medium is strictly prohibited by law
  7.  * This file is proprietary and confidential
  8.  */
  9. namespace App\Security\Voter;
  10. use App\Entity\TexteReferencement;
  11. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  13. use Symfony\Component\Security\Core\Security;
  14. use Symfony\Component\Security\Core\User\UserInterface;
  15. final class TexteReferencementVoter extends Voter
  16. {
  17.     public const EDIT 'edit';
  18.     private $security;
  19.     public function __construct(Security $security)
  20.     {
  21.         $this->security $security;
  22.     }
  23.     protected function supports($attribute$subject): bool
  24.     {
  25.         return $subject instanceof TexteReferencement && \in_array($attribute, [
  26.             self::EDIT,
  27.         ], true);
  28.     }
  29.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  30.     {
  31.         // Utilisateur doit être authentifié
  32.         if (!$token->getUser() instanceof UserInterface) {
  33.             return false;
  34.         }
  35.         if ($this->security->isGranted('ROLE_ADMIN')) {
  36.             return true;
  37.         }
  38.         switch ($attribute) {
  39.             case self::EDIT:
  40.                 return $this->security->isGranted('ROLE_REDACTEUR');
  41.         }
  42.         return false;
  43.     }
  44. }