<?php
declare(strict_types=1);
namespace App\Security\Voter\User;
use App\ReadModel\Procedure\Lot\DetailView;
use App\Security\UserIdentity;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\Security;
class NotificationVoter extends Voter
{
public const NOTIFICATION_SHOW = 'notification_show';
private Security $security;
public function __construct(
Security $security
) {
$this->security = $security;
}
protected function supports(string $attribute, $subject): bool
{
return in_array($attribute, [
self::NOTIFICATION_SHOW
], true);
}
/**
* @param string $attribute
* @param DetailView $subject
* @param TokenInterface $token
* @return bool
*/
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
// Moderators have full access
if ($this->security->isGranted('ROLE_MODERATOR')) {
return true;
}
$user = $token->getUser();
if (!$user instanceof UserIdentity) {
return false;
}
if ($user->getId() !== $subject->id) {
throw new AccessDeniedException('Доступ запрещен. У вас недостаточно прав для совершения этого действия.');
}
return true;
}
}