src/Security/Voter/PageThumbVoter.php line 13

  1. <?php
  2. namespace App\Security\Voter;
  3. use Symfony\Component\Security\Core\Authorization\Voter\Voter,
  4.     Symfony\Component\Security\Core\Authentication\Token\TokenInterface,
  5.     Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  6. use App\Entity\Page,
  7.     App\Entity\PageThumb,
  8.     App\Lib\Actions,
  9.     App\Lib\Roles;
  10. final class PageThumbVoter extends Voter
  11. {
  12.     private AuthorizationCheckerInterface $authorizationChecker;
  13.     public function __construct(AuthorizationCheckerInterface $authorizationChecker)
  14.     {
  15.         $this->authorizationChecker $authorizationChecker;
  16.     }
  17.     protected function supports($attribute$subject): bool
  18.     {
  19.         if (! $subject instanceof PageThumb) {
  20.             return false;
  21.         }
  22.         return true;
  23.     }
  24.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  25.     {
  26.         if ($this->authorizationChecker->isGranted(Roles::ROLE_OPENFORM)) {
  27.             return true;
  28.         }
  29.         /** @var Page */
  30.         $page $subject->getParent();
  31.         return match($attribute) {
  32.             Actions::EDIT => $page->getIsEditable(),
  33.             default => false,
  34.         };
  35.     }
  36. }