src/Voter/Fiche/FicheBatimentVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Voter\Fiche;
  3. use App\Entity\Fiche\FicheBatiment;
  4. use App\Entity\User\User;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use Symfony\Component\Security\Core\Security;
  10. class FicheBatimentVoter extends Voter
  11. {
  12.     const ANALYSE 'fiche_batiment_analyse';
  13.     const DELETE 'fiche_batiment_delete';
  14.     /**
  15.      * @var Security
  16.      */
  17.     private $theSecurity;
  18.     /**
  19.      * @var Request
  20.      */
  21.     private $theRequest;
  22.     public function __construct(Security $theSecurityRequestStack $theRequestStack)
  23.     {
  24.         $this->theSecurity $theSecurity;
  25.         $this->theRequest $theRequestStack->getCurrentRequest();
  26.     }
  27.     protected function supports(string $attribute$subject)
  28.     {
  29.         if (!in_array($attribute, [self::ANALYSEself::DELETE])) {
  30.             return false;
  31.         }
  32.         if (
  33.             !$subject instanceof FicheBatiment
  34.         ) {
  35.             return false;
  36.         }
  37.         return true;
  38.     }
  39.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token)
  40.     {
  41.         /** @var FicheBatiment $theFicheBatiment */
  42.         $theFicheBatiment $subject;
  43.         switch ($attribute) {
  44.             case self::ANALYSE:
  45.                 return $this->accessAnalyse($theFicheBatiment);
  46.             case self::DELETE:
  47.                 return $this->accessDelete($theFicheBatiment);
  48.             default:
  49.                 return false;
  50.         }
  51.     }
  52.     private function accessAnalyse(FicheBatiment $theFicheBatiment) : bool
  53.     {
  54.         return $this->hasRight($theFicheBatiment);
  55.     }
  56.     private function accessDelete(FicheBatiment $theFicheBatiment) : bool
  57.     {
  58.         return $this->hasRight($theFicheBatiment);
  59.     }
  60.     private function hasRight(FicheBatiment $theFicheBatiment) : bool
  61.     {
  62.         if ($theFicheBatiment->getTheFicheBatimentExemple() != null) {
  63.             return true;
  64.         } elseif ($this->hasRightAnonymous($theFicheBatiment)) {
  65.             return true;
  66.         } elseif ($this->hasRightUser($theFicheBatiment)) {
  67.             return true;
  68.         } else {
  69.             return false;
  70.         }
  71.     }
  72.     /**
  73.      *
  74.      * Accès à toutes les fiches pour les Admin, uniquement pour les Fiche de l'Utilisateur dans le cas contraire.
  75.      *
  76.      * @param FicheBatiment $theFicheBatiment
  77.      * @return bool
  78.      */
  79.     private function hasRightUser(FicheBatiment $theFicheBatiment) : bool
  80.     {
  81.         return $this->theSecurity->isGranted('IS_AUTHENTICATED_REMEMBERED') &&
  82.             ($this->theSecurity->isGranted('ROLE_ADMIN') ||
  83.             $theFicheBatiment->getTheUser() == $this->theSecurity->getUser());
  84.     }
  85.     /**
  86.      *
  87.      * Si pas connecté, accès uniquement aux fiches qu'il a stocké en Session.
  88.      *
  89.      * @param FicheBatiment $theFicheBatiment
  90.      * @return bool
  91.      */
  92.     private function hasRightAnonymous(FicheBatiment $theFicheBatiment) : bool
  93.     {
  94.         return $this->theSecurity->isGranted('IS_AUTHENTICATED_ANONYMOUSLY') &&
  95.             $this->theRequest->getSession()->has('idFicheBatiments') &&
  96.             $this->theRequest->getSession()->get('idFicheBatiments') != null &&
  97.             in_array($theFicheBatiment->getId(), $this->theRequest->getSession()->get('idFicheBatiments'));
  98.     }
  99. }