<?php
declare(strict_types=1);
namespace Serglobin\ChatGptBundle\Security;
use Serglobin\ChatGptBundle\Contracts\ChatGptUserInfoInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class ChatGptVoter extends Voter
{
public const VOTER_ATTRIBUTE = 'CHAT_GPT';
public function __construct(
private readonly Security $security,
) {
}
protected function supports(string $attribute, mixed $subject): bool
{
return self::VOTER_ATTRIBUTE === $attribute;
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof ChatGptUserInfoInterface) {
return false;
}
if (!$this->security->isGranted('IS_AUTHENTICATED_FULLY')) {
return false;
}
if (!$user->isVirtualAssistantActive()) {
return false;
}
return true;
}
}