src/Security/Voter/PageVoter.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\Page,
  7.     App\Lib\Actions,
  8.     App\Lib\Roles;
  9. final class PageVoter extends Voter
  10. {
  11.     const CREATE_MESSAGE 'Page cannot be created.';
  12.     const EDIT_MESSAGE 'Page cannot be edited.';
  13.     const DELETE_MESSAGE 'Page cannot be deleted.';
  14.     private AuthorizationCheckerInterface $authorizationChecker;
  15.     public function __construct(AuthorizationCheckerInterface $authorizationChecker)
  16.     {
  17.         $this->authorizationChecker $authorizationChecker;
  18.     }
  19.     protected function supports($attribute$subject): bool
  20.     {
  21.         if (! $subject instanceof Page) {
  22.             return false;
  23.         }
  24.         return true;
  25.     }
  26.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  27.     {
  28.         if ($this->authorizationChecker->isGranted(Roles::ROLE_OPENFORM)) {
  29.             return true;
  30.         }
  31.         /** @var Page $page */
  32.         $page $subject;
  33.         return match($attribute) {
  34.             Actions::CREATE => $page->getParent()?->getIsSubPageable() ?? true,
  35.             Actions::EDITActions::CLONE => $page->getIsEditable(),
  36.             Actions::DELETE  => $page->getIsDeletable(),
  37.             default => false,
  38.         };
  39.     }
  40. }