<?php
namespace App\Security\Voter;
use App\Entity\Notification;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class NotificationVoter extends Voter
{
public const DELETE = 'delete';
public const EDIT = 'edit';
protected function supports(string $attribute, $subject): bool
{
return in_array($attribute, [self::DELETE, self::EDIT])
&& $subject instanceof Notification;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof UserInterface) {
return false;
}
return match ($attribute) {
self::DELETE, self::EDIT => $subject->getProfile() === $user->getProfile(),
default => false,
};
}
}