<?php
declare(strict_types=1);
namespace App\Security\Voter;
use App\Entity\StaticPages\RealizationsPage;
use App\Entity\User\AdminUser;
use App\Repository\RealizationsPageRepository;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class RealizationsPageVoter extends Voter
{
const CAN_VIEW_REALIZATION_PAGE = 'CAN_VIEW_REALIZATION_PAGE';
private $realizationsPageRepository;
public function __construct(RealizationsPageRepository $realizationsPageRepository)
{
$this->realizationsPageRepository = $realizationsPageRepository;
}
protected function supports(string $attribute, $subject)
{
return $attribute == self::CAN_VIEW_REALIZATION_PAGE;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
$realizationPageState = $this->realizationsPageRepository->findSingleInstance()->getState();
if (!$user instanceof AdminUser) {
return $realizationPageState !== RealizationsPage::STATE_DRAFT;
}
}
}