src/Security/Voter/PartenaireVoter.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\Partenaire;
  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 PartenaireVoter extends Voter
  16. {
  17.     public const ADD 'add';
  18.     public const DELETE 'delete';
  19.     public const EDIT 'edit';
  20.     private $security;
  21.     public function __construct(Security $security)
  22.     {
  23.         $this->security $security;
  24.     }
  25.     protected function supports($attribute$subject): bool
  26.     {
  27.         return
  28.             ($subject instanceof Partenaire || 'partenaire' === $subject) &&
  29.             \in_array($attribute, [self::ADDself::DELETEself::EDIT], true);
  30.     }
  31.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  32.     {
  33.         // Utilisateur doit être authentifié
  34.         if (!$token->getUser() instanceof UserInterface) {
  35.             return false;
  36.         }
  37.         if ($this->security->isGranted('ROLE_ADMIN')) {
  38.             return true;
  39.         }
  40.         switch ($attribute) {
  41.             case self::ADD:
  42.             case self::DELETE:
  43.             case self::EDIT:
  44.                 return $this->security->isGranted('ROLE_REDACTEUR');
  45.         }
  46.         return false;
  47.     }
  48. }