<?php
namespace App\Security\Voter\Front;
use Stimactiv\CmsBundle\Entity\Page;
use Stimactiv\CoreBundle\Entity\BaseUser;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class PageVoter extends Voter
{
public const IS_READABLE = 'front.page.is_readable';
protected function supports(string $attribute, $subject)
{
return in_array($attribute, [
self::IS_READABLE,
]);
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
if (!$subject instanceof Page) {
return false;
}
switch ($attribute) {
case self::IS_READABLE:
if ($token->getUser() instanceof BaseUser) {
return true;
}
return $subject->getVisibility() === Page::__P_VISIBILITY_1_; // Publique
}
return false;
}
}