You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
herbarium/src/Security/Voter/CommentVoter.php

37 lines
1.0 KiB

<?php
namespace App\Security\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class CommentVoter extends Voter
{
public const EDIT = 'COMMENT_EDIT';
protected function supports(string $attribute, mixed $subject): bool
{
// replace with your own logic
// https://symfony.com/doc/current/security/voters.html
return $attribute === self::EDIT
&& $subject instanceof \App\Entity\Comment;
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
if ($subject->getAuthor() === $user) {
return true;
}
return in_array('ROLE_ADMIN', $user->getRoles(), true);
}
}