vendor/serglobin/chat-gpt-bundle/src/EventSubscriber/OpenAIApiSubscriber.php line 30

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Serglobin\ChatGptBundle\EventSubscriber;
  4. use Serglobin\ChatGptBundle\Security\ChatGptVoter;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpKernel\Event\RequestEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  9. use Symfony\Component\Security\Core\Security;
  10. class OpenAIApiSubscriber implements EventSubscriberInterface
  11. {
  12.     public function __construct(private readonly Security $security)
  13.     {
  14.     }
  15.     public static function getSubscribedEvents(): array
  16.     {
  17.         return [
  18.             KernelEvents::REQUEST => 'onKernelRequest',
  19.         ];
  20.     }
  21.     public function onKernelRequest(RequestEvent $requestEvent): void
  22.     {
  23.         $request $requestEvent->getRequest();
  24.         $path $request->getPathInfo();
  25.         if (\str_starts_with($path'/api/openai') && !$this->security->isGranted(ChatGptVoter::VOTER_ATTRIBUTE)) {
  26.             $accessDeniedException = new AccessDeniedException();
  27.              $accessDeniedException->setAttributes(ChatGptVoter::VOTER_ATTRIBUTE);
  28.              throw $accessDeniedException;
  29.         }
  30.     }
  31. }