src/Security/Voter/Documents/DocumentVoter.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\Documents;
  3. use App\Entity\Documents\Document;
  4. use App\Entity\User;
  5. use App\Enum\MenuRolesAssociatedEnum;
  6. use App\Enum\MenuRolesManagerEnum;
  7. use App\Enum\VotersEnum;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. use Symfony\Component\Security\Core\Security;
  11. class DocumentVoter extends Voter
  12. {
  13.     private Security $security;
  14.     private array $voters;
  15.     public function __construct(Security $security)
  16.     {
  17.         $this->security $security;
  18.         $this->voters = [
  19.             VotersEnum::LIST_DOCUMENT,
  20.             VotersEnum::CREATE_DOCUMENT,
  21.             VotersEnum::READ,
  22.             VotersEnum::UPDATE,
  23.             VotersEnum::DELETE,
  24.             VotersEnum::LIST_DOCUMENT_ASSOCIATED,
  25.             VotersEnum::VIEW,
  26.             VotersEnum::DOWNLOAD,
  27.         ];
  28.     }
  29.     protected function supports(string $attribute$subject): bool
  30.     {
  31.         // first check the $subject and last if the $attribute is supported,
  32.         // because there are attributes (with subject) used as well by other voters (like UPDATE, ...)
  33.         if ($subject && !$subject instanceof Document) {
  34.             // only vote on these objects
  35.             return false;
  36.         }
  37.         if (in_array($attribute$this->voters)) {
  38.             // if the attribute is one we support
  39.             return true;
  40.         }
  41.         return false;
  42.     }
  43.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  44.     {
  45.         $user $token->getUser();
  46.         if (!$user instanceof User) {
  47.             // the user must be logged in; if not, deny access
  48.             return false;
  49.         }
  50.         switch ($attribute) {
  51.             case VotersEnum::LIST_DOCUMENT:
  52.                 return $this->canlist();
  53.             case VotersEnum::CREATE_DOCUMENT:
  54.                 return $this->canCreate();
  55.             case VotersEnum::READ:
  56.                 return $this->canRead();
  57.             case VotersEnum::UPDATE:
  58.                 return $this->canUpdate();
  59.             case VotersEnum::DELETE:
  60.                 return $this->canDelete();
  61.             case VotersEnum::LIST_DOCUMENT_ASSOCIATED:
  62.                 return $this->canlistAssociated();
  63.             case VotersEnum::VIEW:
  64.                 return $this->canView($subject$user);
  65.             case VotersEnum::DOWNLOAD:
  66.                 return $this->canDownload($subject$user);
  67.         }
  68.         throw new \LogicException('This code should not be reached!');
  69.     }
  70.     private function canList(): bool
  71.     {
  72.         return $this->isAdminUser();
  73.     }
  74.     private function canCreate(): bool
  75.     {
  76.         return $this->isAdminUser();
  77.     }
  78.     private function canRead(): bool
  79.     {
  80.         return $this->isAdminUser();
  81.     }
  82.     private function canUpdate(): bool
  83.     {
  84.         return $this->isAdminUser();
  85.     }
  86.     private function canDelete(): bool
  87.     {
  88.         return $this->isAdminUser();
  89.     }
  90.     private function canListAssociated(): bool
  91.     {
  92.         return $this->isAdminUser()
  93.             || $this->isAssociatedUser()
  94.             ;
  95.     }
  96.     private function canView(Document $documentUser $user): bool
  97.     {
  98.         return $this->isAdminUser()
  99.             || ($this->isAssociatedUser() && !$document->isExcludedFor($user))
  100.             || ($this->isAssociatedUser() && !$document->isExcludedFor($user))
  101.             ;
  102.     }
  103.     private function canDownload(Document $documentUser $user): bool
  104.     {
  105.         return $this->isAdminUser()
  106.             || ($this->isAssociatedUser() && !$document->isExcludedFor($user))
  107.             ;
  108.     }
  109.     private function isAdminUser(): bool
  110.     {
  111.         return $this->security->isGranted(MenuRolesManagerEnum::ROLE_MENU_DOCUMENTS_MANAGEMENT);
  112.     }
  113.     private function isAssociatedUser(): bool
  114.     {
  115.         return $this->security->isGranted(MenuRolesAssociatedEnum::ROLE_MENU_DOCUMENTS_MANAGEMENT_ASSOCIATED);
  116.     }
  117. }