src/Security/Voter/PublishReviewedVoter.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Episode;
  4. use App\Entity\Program;
  5. use App\Entity\Radio;
  6. use App\Entity\User;
  7. use EasyCorp\Bundle\EasyAdminBundle\Dto\ActionDto;
  8. use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
  9. use EasyCorp\Bundle\EasyAdminBundle\Security\Permission;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  12. use Symfony\Component\Security\Core\Security;
  13. use Symfony\Component\Security\Core\User\UserInterface;
  14. /**
  15.  * This class vote on PUBLISH_REVIEWED attribute.
  16.  * The entity can be published only if it has been reviewed. 
  17.  */
  18. class PublishReviewedVoter extends Voter
  19. {
  20.     private $security;
  21.     private $adminContextProvider;
  22.     private $entities;
  23.     public function __construct(Security $securityAdminContextProvider $adminContextProvider)
  24.     {
  25.         $this->security $security;
  26.         $this->adminContextProvider $adminContextProvider;
  27.     }
  28.     protected function supports($attribute$subject)
  29.     {
  30.         $context $this->adminContextProvider->getContext();
  31.         //$entity = $context?->getEntity()?->getInstance();
  32.         // dump([
  33.         //     "supports" => $attribute,
  34.         //     'subject' => $subject,
  35.         //     'entity' => $entity,
  36.         // ]);
  37.         // Cache entities, we need them later when asked to vote on PUBLISH_REVIEWED
  38.         if ($attribute == 'EA_ACCESS_ENTITY' && $entity $subject->getInstance()) {
  39.             $this->entities[] = $entity;
  40.         }
  41.         $supports $attribute == 'PUBLISH_REVIEWED';
  42.         return $supports;
  43.     }
  44.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  45.     {
  46.         $user $token->getUser();
  47.         // if the user is anonymous, do not grant access
  48.         if (!$user instanceof UserInterface) {
  49.             return false;
  50.         }
  51.         static $entityPos 0;
  52.         $entity $this->entities[$entityPos] ?? null;
  53.         $entityPos++;
  54.         // dump([
  55.         //     "voteOnAttribute" => $attribute,
  56.         //     'subject' => $subject,
  57.         //     'token' => $token,
  58.         //     'entity' => $entity,
  59.         // ]);
  60.         return $entity?->getReviewed();
  61.     }
  62. }