<?php
namespace App\Security\Voter;
use App\Entity\Notification;
use App\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class NotificationVoter extends Voter
{
public const VIEW = 'NOTIFICATION_VIEW';
public const EDIT = 'NOTIFICATION_EDIT';
protected function supports(string $attribute, mixed $subject): bool
{
if (!$subject instanceof Notification) {
return false;
}
return in_array($attribute, [self::VIEW, self::EDIT], true);
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
/** @var Notification $notification */
$notification = $subject;
$owner = $notification->getUser();
return null !== $owner && $owner->getId() === $user->getId();
}
}