vendor/crea/documentation-bundle/src/Voter/DocumentationVoter.php line 14

Open in your IDE?
  1. <?php
  2. namespace Crea\DocumentationBundle\Voter;
  3. use Crea\DocumentationBundle\Entity\Article;
  4. use Crea\DocumentationBundle\Entity\Chapter;
  5. use Crea\DocumentationBundle\Provider\DocumentationRightCodeProvider;
  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 DocumentationVoter extends Voter
  11. {
  12.     protected DocumentationRightCodeProvider $documentationRightCodeProvider;
  13.     public function __construct(DocumentationRightCodeProvider $documentationRightCodeProvider)
  14.     {
  15.         $this->documentationRightCodeProvider $documentationRightCodeProvider;
  16.     }
  17.     /**
  18.      * @inheritDoc
  19.      * @throws Exception
  20.      */
  21.     protected function supports($attribute$subject): bool
  22.     {
  23.         $chapterAttributes = [$this->documentationRightCodeProvider->getMainChapterCreateRightCode()];
  24.         if ($subject instanceof Chapter) {
  25.             $chapterAttributes[] = $this->documentationRightCodeProvider->getChapterViewRightCode($subject);
  26.             $chapterAttributes[] = $this->documentationRightCodeProvider->getChapterCreateRightCode($subject);
  27.             $chapterAttributes[] = $this->documentationRightCodeProvider->getChapterUpdateRightCode($subject);
  28.             $chapterAttributes[] = $this->documentationRightCodeProvider->getChapterRemoveRightCode($subject);
  29.             $chapterAttributes[] = $this->documentationRightCodeProvider->getArticleCreateRightCode($subject);
  30.         }
  31.         if ($subject instanceof Article) {
  32.             $chapterAttributes[] = $this->documentationRightCodeProvider->getArticleUpdateRightCode($subject);
  33.             $chapterAttributes[] = $this->documentationRightCodeProvider->getArticleRemoveRightCode($subject);
  34.         }
  35.         return in_array($attribute$chapterAttributes);
  36.     }
  37.     /**
  38.      * @inheritDoc
  39.      * @throws Exception
  40.      */
  41.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  42.     {
  43.         /** @var UserInterface $loggedUser */
  44.         $loggedUser $token->getUser();
  45.         if (!$loggedUser instanceof UserInterface) {
  46.             return false;
  47.         }
  48.         if ($attribute === $this->documentationRightCodeProvider->getMainChapterCreateRightCode()) {
  49.             return $this->voteOnMainChapterCreate($loggedUser);
  50.         }
  51.         if ($subject instanceof Chapter) {
  52.             switch ($attribute) {
  53.                 case $this->documentationRightCodeProvider->getChapterViewRightCode($subject):
  54.                     return $this->voteOnChapterView($subject$loggedUser);
  55.                 case $this->documentationRightCodeProvider->getChapterCreateRightCode($subject):
  56.                     return $this->voteOnChapterCreate($subject$loggedUser);
  57.                 case $this->documentationRightCodeProvider->getChapterUpdateRightCode($subject):
  58.                     return $this->voteOnChapterUpdate($subject$loggedUser);
  59.                 case $this->documentationRightCodeProvider->getChapterRemoveRightCode($subject):
  60.                     return $this->voteOnChapterRemove($subject$loggedUser);
  61.                 case $this->documentationRightCodeProvider->getArticleCreateRightCode($subject):
  62.                     return $this->voteOnArticleCreate($subject$loggedUser);
  63.             }
  64.         }
  65.         if ($subject instanceof Article) {
  66.             switch ($attribute) {
  67.                 case $this->documentationRightCodeProvider->getArticleUpdateRightCode($subject):
  68.                     return $this->voteOnArticleUpdate($subject$loggedUser);
  69.                 case $this->documentationRightCodeProvider->getArticleRemoveRightCode($subject):
  70.                     return $this->voteOnArticleRemove($subject$loggedUser);
  71.             }
  72.         }
  73.         return false;
  74.     }
  75.     private function voteOnMainChapterCreate(UserInterface $loggedUser): bool
  76.     {
  77.         return in_array($this->documentationRightCodeProvider->getMainChapterCreateRightCode(), $loggedUser->getRoles());
  78.     }
  79.     /**
  80.      * @throws Exception
  81.      */
  82.     private function voteOnChapterView(Chapter $chapterUserInterface $loggedUser): bool
  83.     {
  84.         return in_array($this->documentationRightCodeProvider->getChapterViewRightCode($chapter), $loggedUser->getRoles());
  85.     }
  86.     /**
  87.      * @throws Exception
  88.      */
  89.     private function voteOnChapterCreate(Chapter $chapterUserInterface $loggedUser): bool
  90.     {
  91.         return in_array($this->documentationRightCodeProvider->getChapterCreateRightCode($chapter), $loggedUser->getRoles());
  92.     }
  93.     /**
  94.      * @throws Exception
  95.      */
  96.     private function voteOnChapterUpdate(Chapter $chapterUserInterface $loggedUser): bool
  97.     {
  98.         return in_array($this->documentationRightCodeProvider->getChapterUpdateRightCode($chapter), $loggedUser->getRoles());
  99.     }
  100.     /**
  101.      * @throws Exception
  102.      */
  103.     private function voteOnChapterRemove(Chapter $chapterUserInterface $loggedUser): bool
  104.     {
  105.         return in_array($this->documentationRightCodeProvider->getChapterRemoveRightCode($chapter), $loggedUser->getRoles());
  106.     }
  107.     /**
  108.      * @throws Exception
  109.      */
  110.     private function voteOnArticleCreate(Chapter $chapterUserInterface $loggedUser): bool
  111.     {
  112.         return in_array($this->documentationRightCodeProvider->getArticleCreateRightCode($chapter), $loggedUser->getRoles());
  113.     }
  114.     /**
  115.      * @throws Exception
  116.      */
  117.     private function voteOnArticleUpdate(Article $articleUserInterface $loggedUser): bool
  118.     {
  119.         return in_array($this->documentationRightCodeProvider->getArticleUpdateRightCode($article), $loggedUser->getRoles());
  120.     }
  121.     /**
  122.      * @throws Exception
  123.      */
  124.     private function voteOnArticleRemove(Article $articleUserInterface $loggedUser): bool
  125.     {
  126.         return in_array($this->documentationRightCodeProvider->getArticleRemoveRightCode($article), $loggedUser->getRoles());
  127.     }
  128. }