src/Security/Voter/PageBlockVoter.php line 12

  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\PageBlock,
  7.     App\Lib\Actions,
  8.     App\Lib\Roles;
  9. final class PageBlockVoter extends Voter
  10. {
  11.     private AuthorizationCheckerInterface $authorizationChecker;
  12.     public function __construct(AuthorizationCheckerInterface $authorizationChecker)
  13.     {
  14.         $this->authorizationChecker $authorizationChecker;
  15.     }
  16.     protected function supports($attribute$subject): bool
  17.     {
  18.         if (! $subject instanceof PageBlock) {
  19.             return false;
  20.         }
  21.         return true;
  22.     }
  23.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  24.     {
  25.         if ($this->authorizationChecker->isGranted(Roles::ROLE_OPENFORM)) {
  26.             return true;
  27.         }
  28.         /** @var Page */
  29.         $page $subject->getParent();
  30.         return match($attribute) {
  31.             Actions::CREATEActions::EDITActions::DELETE => $page->getIsEditable(),
  32.             default => false,
  33.         };
  34.     }
  35. }