<?php
namespace App\EventListener;
use App\Entity\User;
use App\Repository\GroupRepository;
use App\Services\Group\ValidateDomainGroupUserService;
use HWI\Bundle\OAuthBundle\Security\Core\Authentication\Token\OAuthToken;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
/**
* Class ValidateDomainGroupUserListener
* @package App\EventListener
*/
class ValidateDomainGroupUserListener
{
/**
* @var TokenStorageInterface
*/
private $tokenStorage;
/**
* ValidateDomainGroupUserListener constructor.
* @param TokenStorageInterface $tokenStorage
*/
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}
/**
* @param RequestEvent $event
*/
public function onKernelRequest(RequestEvent $event): void
{
if (!$this->tokenStorage->getToken() instanceof UsernamePasswordToken) {
return;
}
$user = $this->tokenStorage->getToken()->getUser();
$validateDomainGroupUserService = new ValidateDomainGroupUserService();
if (!$validateDomainGroupUserService->__invoke($event->getRequest()->getHost(), $user)) {
$event->setResponse(new Response('', 403));
}
}
}