src/Security/Voter/PageVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Config;
  4. use App\Entity\BackUser;
  5. use App\Entity\Page;
  6. use App\Utils\VoterHelper;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. class PageVoter extends Voter
  10. {
  11.     private const SEE_FRONT     'see-front';
  12.     private const SEE           'see';
  13.     private const EDIT          'edit';
  14.     private const PAGE_COMPOSER 'page-composer';
  15.     private const TRASH         'trash';
  16.     private const DELETE        'delete';
  17.     private const DRAFT         'draft';
  18.     private const SUBMIT        'submit';
  19.     private const PUBLISH       'publish';
  20.     private const UN_PUBLISH    'un-publish';
  21.     private const ARCHIVE       'archive';
  22.     private const PREVIEW       'preview';
  23.     /**
  24.      * PageVoter constructor.
  25.      */
  26.     public function __construct()
  27.     {
  28.     }
  29.     protected function supports(string $attributemixed $subject): bool
  30.     {
  31.         return $subject instanceof Page && in_array($attribute, [self::SEE_FRONTself::SEEself::EDITself::PAGE_COMPOSERself::TRASHself::DELETEself::DRAFTself::SUBMITself::PUBLISHself::UN_PUBLISHself::ARCHIVEself::PREVIEW], true);
  32.     }
  33.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  34.     {
  35.         $user $token->getUser();
  36.         // the user must be logged in; if not, deny permission
  37.         if (!$user instanceof BackUser && self::SEE_FRONT != $attribute) {
  38.             return false;
  39.         }
  40.         switch ($attribute) {
  41.             case self::SEE_FRONT:
  42.                 if (!$subject->isVisible()) {
  43.                     return false;
  44.                 }
  45.                 break;
  46.             case self::PAGE_COMPOSER:
  47.                 if (Config::isTemplateLocked($subject->getTemplate())) {
  48.                     return false;
  49.                 }
  50.                 break;
  51.             case self::TRASH:
  52.                 if (!$subject->isMulti()) {
  53.                     return false;
  54.                 }
  55.                 break;
  56.             case self::DELETE:
  57.                 if (!$subject->isMulti() || Page::STATUS_TRASH != $subject->getStatus()) {
  58.                     return false;
  59.                 }
  60.                 break;
  61.         }
  62.         return VoterHelper::voteOnAttribute($attribute$subject$token'page');
  63.     }
  64. }