<?php
namespace App\Security\Voter;
use App\Entity\Episode;
use App\Entity\Program;
use App\Entity\Radio;
use App\Entity\User;
use EasyCorp\Bundle\EasyAdminBundle\Dto\ActionDto;
use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
use EasyCorp\Bundle\EasyAdminBundle\Security\Permission;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* This class vote on PUBLISH_REVIEWED attribute.
* The entity can be published only if it has been reviewed.
*/
class PublishReviewedVoter extends Voter
{
private $security;
private $adminContextProvider;
private $entities;
public function __construct(Security $security, AdminContextProvider $adminContextProvider)
{
$this->security = $security;
$this->adminContextProvider = $adminContextProvider;
}
protected function supports($attribute, $subject)
{
$context = $this->adminContextProvider->getContext();
//$entity = $context?->getEntity()?->getInstance();
// dump([
// "supports" => $attribute,
// 'subject' => $subject,
// 'entity' => $entity,
// ]);
// Cache entities, we need them later when asked to vote on PUBLISH_REVIEWED
if ($attribute == 'EA_ACCESS_ENTITY' && $entity = $subject->getInstance()) {
$this->entities[] = $entity;
}
$supports = $attribute == 'PUBLISH_REVIEWED';
return $supports;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
static $entityPos = 0;
$entity = $this->entities[$entityPos] ?? null;
$entityPos++;
// dump([
// "voteOnAttribute" => $attribute,
// 'subject' => $subject,
// 'token' => $token,
// 'entity' => $entity,
// ]);
return $entity?->getReviewed();
}
}