vendor/crea/parameter-bundle/src/Voter/ParameterVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace Crea\ParameterBundle\Voter;
  3. use Crea\ParameterBundle\Entity\Parameter;
  4. use Crea\ParameterBundle\Entity\ParameterType;
  5. use Crea\ParameterBundle\Provider\ParameterRightCodeProvider;
  6. use Exception;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. class ParameterVoter extends Voter
  11. {
  12.     protected ParameterRightCodeProvider $parameterRightCodeProvider;
  13.     public function __construct(ParameterRightCodeProvider $parameterRightCodeProvider)
  14.     {
  15.         $this->parameterRightCodeProvider $parameterRightCodeProvider;
  16.     }
  17.     /**
  18.      * @inheritDoc
  19.      * @throws Exception
  20.      */
  21.     protected function supports($attribute$subject): bool
  22.     {
  23.         $chapterAttributes = [];
  24.         if ($subject instanceof ParameterType) {
  25.             $chapterAttributes[] = $this->parameterRightCodeProvider->getParameterViewRightCode($subject);
  26.             $chapterAttributes[] = $this->parameterRightCodeProvider->getParameterCreateRightCode($subject);
  27.         }
  28.         if ($subject instanceof Parameter) {
  29.             $chapterAttributes[] = $this->parameterRightCodeProvider->getParameterUpdateRightCode($subject);
  30.             $chapterAttributes[] = $this->parameterRightCodeProvider->getParameterRemoveRightCode($subject);
  31.         }
  32.         return in_array($attribute$chapterAttributes);
  33.     }
  34.     /**
  35.      * @inheritDoc
  36.      * @throws Exception
  37.      */
  38.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  39.     {
  40.         /** @var UserInterface $loggedUser */
  41.         $loggedUser $token->getUser();
  42.         if (!$loggedUser instanceof UserInterface) {
  43.             return false;
  44.         }
  45.         if ($subject instanceof ParameterType) {
  46.             switch ($attribute) {
  47.                 case $this->parameterRightCodeProvider->getParameterViewRightCode($subject):
  48.                     return $this->voteOnParameterView($subject$loggedUser);
  49.                 case $this->parameterRightCodeProvider->getParameterCreateRightCode($subject):
  50.                     return $this->voteOnParameterCreate($subject$loggedUser);
  51.             }
  52.         }
  53.         if ($subject instanceof Parameter) {
  54.             switch ($attribute) {
  55.                 case $this->parameterRightCodeProvider->getParameterUpdateRightCode($subject):
  56.                     return $this->voteOnParameterUpdate($subject$loggedUser);
  57.                 case $this->parameterRightCodeProvider->getParameterRemoveRightCode($subject):
  58.                     return $this->voteOnParameterRemove($subject$loggedUser);
  59.             }
  60.         }
  61.         return false;
  62.     }
  63.     private function voteOnParameterView(ParameterType $parameterTypeUserInterface $loggedUser): bool
  64.     {
  65.         return in_array($this->parameterRightCodeProvider->getParameterViewRightCode($parameterType), $loggedUser->getRoles());
  66.     }
  67.     private function voteOnParameterCreate(ParameterType $parameterTypeUserInterface $loggedUser): bool
  68.     {
  69.         return in_array($this->parameterRightCodeProvider->getParameterCreateRightCode($parameterType), $loggedUser->getRoles());
  70.     }
  71.     /**
  72.      * @throws Exception
  73.      */
  74.     private function voteOnParameterUpdate(Parameter $parameterUserInterface $loggedUser): bool
  75.     {
  76.         return in_array($this->parameterRightCodeProvider->getParameterUpdateRightCode($parameter), $loggedUser->getRoles());
  77.     }
  78.     /**
  79.      * @throws Exception
  80.      */
  81.     private function voteOnParameterRemove(Parameter $parameterUserInterface $loggedUser): bool
  82.     {
  83.         return in_array($this->parameterRightCodeProvider->getParameterRemoveRightCode($parameter), $loggedUser->getRoles());
  84.     }
  85. }