<?php
// src/EventListener/RequestListener.php
namespace App\EventListener;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Security;
use App\Service\ConfigService;
use App\Entity\KycVerificationStatus;
class KycListener
{
private $urlGenerator;
private $authorizationChecker;
private $security;
public function __construct(UrlGeneratorInterface $urlGenerator, AuthorizationCheckerInterface $authorizationChecker, Security $security, private ConfigService $configService)
{
$this->urlGenerator = $urlGenerator;
$this->authorizationChecker = $authorizationChecker;
$this->security = $security;
}
public function onKernelRequest(RequestEvent $event)
{
$request = $event->getRequest();
return;
// Check if the current path is not in the list of paths to exclude
$excludePaths = ['/prestataire-back-office','/auth/connexion', '/auth/inscription', '/reset-password', '/auth/formulaire_contact', '/auth/print_condition_utilisation','/auth/condition-utilisation', '/auth/check-existing-user', '/otp', '/informations', $this->urlGenerator->generate('app_data_protection_infos'), $this->urlGenerator->generate('app_new_kyc_sumsub_webhook')];
foreach($excludePaths as $path){
if(strncmp($request->getPathInfo(), $path, strlen($path)) == 0){
return;
}
}
$user = $this->security->getUser();
if($user && !$user->isDataProtectionChecked() && !$this->authorizationChecker->isGranted('ROLE_ADMIN') && !$this->authorizationChecker->isGranted('ROLE_PLAYER')){
$event->setResponse(new RedirectResponse($this->urlGenerator->generate('app_data_protection_infos')));
return;
}
$kycVerificationStatus = $this->configService->getKycVerificationStatusVerified();
if ($user?->getKycVerificationStatus()?->getId() == $kycVerificationStatus->getId() && !$this->authorizationChecker->isGranted('ROLE_PLAYER') && !$user?->hasSubmittedKycDocument() && !$this->authorizationChecker->isGranted('ROLE_ADMIN')) {
$documentRoutesArray = [
'/verification/'
];
$isDocumentRoutes = false;
foreach($documentRoutesArray as $path){
if(strncmp($request->getPathInfo(), $path, strlen($path)) == 0){
$isDocumentRoutes = true;;
}
}
$urlKycDocument = $this->urlGenerator->generate('app_kyc_info');
if(!$isDocumentRoutes){
$event->setResponse(new RedirectResponse($urlKycDocument));
}
}
if ($this->authorizationChecker->isGranted('KYC_INFO_SUBMITTED') || $this->authorizationChecker->isGranted('ROLE_PLAYER')) {
return;
}
// Redirect to a specific page
$url = $this->urlGenerator->generate('app_kyc');
if(strncmp($request->getPathInfo(), $url, strlen($url)) != 0){
$event->setResponse(new RedirectResponse($url));
}
}
}