src/Security/Voter/AllowCommentsVoter.php line 11
<?php
namespace Cms\Security\Voter;
use Cms\Entity\Post;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class AllowCommentsVoter extends Voter
{
public const VOTE = 'comments_allowed';
public function __construct(
#[Autowire('%cms.allow_comments%')] private bool $allowComments
) {}
protected function supports(string $attribute, mixed $subject): bool
{
return $subject instanceof Post && self::VOTE === $attribute;
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
/** @var Post $subject */
$user = $token->getUser();
if (!$user instanceof UserInterface) {
return false;
}
if (null !== $subject->isCommentsAllowed()) {
return $subject->isCommentsAllowed();
}
if (null !== $subject->getCategory()->isCommentsAllowed()) {
return $subject->getCategory()->isCommentsAllowed();
}
return $this->allowComments;
}
}