src/Security/Voter/OffreEmploiVoter.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\OffreEmploi;
  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 OffreEmploiVoter extends Voter
  16. {
  17.     public const EDIT 'edit';
  18.     public const DELETE 'delete';
  19.     private $security;
  20.     public function __construct(Security $security)
  21.     {
  22.         $this->security $security;
  23.     }
  24.     protected function supports($attribute$subject): bool
  25.     {
  26.         return $subject instanceof OffreEmploi && \in_array($attribute, [
  27.             self::EDIT,
  28.             self::DELETE,
  29.         ], 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.         switch ($attribute) {
  38.             case self::EDIT:
  39.             case self::DELETE:
  40.                 return $this->canManipulate($subject$token);
  41.         }
  42.         return false;
  43.     }
  44.     private function canManipulate(OffreEmploi $offreTokenInterface $token): bool
  45.     {
  46.         // Actualités distantes ne peuvent pas être modifié dans le portail
  47.         if ($offre->getIdDistant()) {
  48.             return false;
  49.         }
  50.         if (!$this->security->isGranted('ROLE_RESPONSABLE_ETABLISSEMENT')) {
  51.             return false;
  52.         }
  53.         $etab $offre->getEtablissement();
  54.         if (!$etab) {
  55.             return false;
  56.         }
  57.         return $token->getUser()->getEtablissementsResponsable()->contains($etab);
  58.     }
  59. }