src/Security/Voter/JournalPageVoter.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\JournalPage,
  7.     App\Lib\Actions,
  8.     App\Lib\Roles;
  9. final class JournalPageVoter extends Voter
  10. {
  11.     const MESSAGE 'Predefined page cannot be deleted.';
  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 JournalPage || Actions::DELETE !== $attribute) {
  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.         if ($subject->getIdName()) {
  30.             return false;
  31.         }
  32.         return true;
  33.     }
  34. }