src/Security/Voter/SatisfactionSurveys/SatisfactionSurveyQuestionSecurityVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\SatisfactionSurveys;
  3. use App\Entity\SatisfactionSurveys\SatisfactionSurveyQuestion;
  4. use App\Entity\User;
  5. use App\Enum\MenuRolesAssociatedEnum;
  6. use App\Enum\MenuRolesManagerEnum;
  7. use App\Enum\VotersEnum;
  8. use LogicException;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  11. use Symfony\Component\Security\Core\Security;
  12. final class SatisfactionSurveyQuestionSecurityVoter extends Voter
  13. {
  14.     private Security $security;
  15.     private array $voters;
  16.     public function __construct(Security $security)
  17.     {
  18.         $this->security $security;
  19.         $this->voters = [
  20.             VotersEnum::LIST_SATISFACTION_SURVEY_QUESTION,
  21.             VotersEnum::LIST_SATISFACTION_SURVEY_QUESTION_ASSOCIATED,
  22.             VotersEnum::CREATE_SATISFACTION_SURVEY_QUESTION,
  23.             VotersEnum::READ,
  24.             VotersEnum::UPDATE,
  25.         ];
  26.     }
  27.     protected function supports(string $attribute$subject): bool
  28.     {
  29.         // first check the $subject and last if the $attribute is supported,
  30.         // because there are attributes (with subject) used as well by other voters (like UPDATE, ...)
  31.         if ($subject && !$subject instanceof SatisfactionSurveyQuestion) {
  32.             // only vote on these objects
  33.             return false;
  34.         }
  35.         if (in_array($attribute$this->voters)) {
  36.             // if the attribute is one we support
  37.             return true;
  38.         }
  39.         return false;
  40.     }
  41.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  42.     {
  43.         $user $token->getUser();
  44.         if (!$user instanceof User) {
  45.             // the user must be logged in; if not, deny access
  46.             return false;
  47.         }
  48.         switch ($attribute) {
  49.             case VotersEnum::LIST_SATISFACTION_SURVEY_QUESTION:
  50.                 return $this->canList();
  51.             case VotersEnum::LIST_SATISFACTION_SURVEY_QUESTION_ASSOCIATED:
  52.                 return $this->canListAssociated();
  53.             case VotersEnum::CREATE_SATISFACTION_SURVEY_QUESTION:
  54.                 return $this->canCreate();
  55.             case VotersEnum::READ:
  56.                 return $this->canRead();
  57.             case VotersEnum::UPDATE:
  58.                 return $this->canUpdate();
  59.         }
  60.         throw new LogicException('This code should not be reached!');
  61.     }
  62.     private function canList(): bool
  63.     {
  64.         return $this->isAdminUser();
  65.     }
  66.     private function canListAssociated(): bool
  67.     {
  68.         return $this->isAssociatedUser();
  69.     }
  70.     private function canCreate(): bool
  71.     {
  72.         return $this->isAdminUser();
  73.     }
  74.     private function canRead(): bool
  75.     {
  76.         return $this->isAdminUser() || $this->isAssociatedUser();
  77.     }
  78.     private function canUpdate(): bool
  79.     {
  80.         return $this->isAdminUser();
  81.     }
  82.     private function isAssociatedUser(): bool
  83.     {
  84.         return $this->security->isGranted(MenuRolesAssociatedEnum::ROLE_MENU_SATISFACTION_SURVEYS_ASOCIATED);
  85.     }
  86.     private function isAdminUser(): bool
  87.     {
  88.         return $this->security->isGranted(MenuRolesManagerEnum::ROLE_MENU_SATISFACTION_SURVEYS);
  89.     }
  90. }