<?php
namespace App\Security;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
use App\Repository\UserRepository;
class AuthenticationEntryPoint implements AuthenticationEntryPointInterface
{
public function __construct(
private UrlGeneratorInterface $urlGenerator,
private UserRepository $userRepo
) {
}
public function start(Request $request, AuthenticationException $authException = null): RedirectResponse
{
if (empty(!$this->userRepo->findAll())) {
// add a custom flash message and redirect to the login page
$request->getSession()->getFlashBag()->add('danger', 'Vous devez vous connecter pour accéder à cette page.');
return new RedirectResponse($this->urlGenerator->generate('app_login'));
} else {
// add a custom flash message and redirect to the login page
$request->getSession()->getFlashBag()->add(
'success',
'Bien venue Cher Admin !!!'
);
return new RedirectResponse($this->urlGenerator->generate('app_admin_register'));
}
}
}