<?php
namespace App\Security\Voter;
use App\Entity\ForumPost;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class ForumPostVoter extends Voter
{
public const EDIT = 'EDIT';
public const DELETE = 'DELETE';
protected function supports(string $attribute, $subject): bool
{
// replace with your own logic
// https://symfony.com/doc/current/security/voters.html
return in_array($attribute, [self::EDIT, self::DELETE])
&& $subject instanceof ForumPost;
}
protected function voteOnAttribute(string $attribute, $forumPost, TokenInterface $token): bool
{
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
if (null == $forumPost->getUser()) {
return false;
}
// ... (check conditions and return true to grant permission) ...
switch ($attribute) {
case self::EDIT:
if ($user == $forumPost->getUser()) {
return true;
}
break;
case self::DELETE:
if ($user == $forumPost->getUser()) {
return true;
}
break;
}
return false;
}
}