for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace App\Security\Voter;
use App\Entity\Article;
use App\Entity\Comment;
use App\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
final class AuthorVoter extends Voter
{
/**
* {@inheritdoc}
*/
protected function supports(string $attribute, $subject): bool
return $attribute === 'AUTHOR' && ($subject instanceof Article || $subject instanceof Comment);
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
/** @var Article|Comment $subject */
$user = $token->getUser();
if (!$user instanceof User) {
return false;
return $subject->getAuthor()->getId() === $user->getId();