<?phpnamespace App\Controller\Login;use App\Entity\Core\Agencies;use App\Entity\Core\AgenciesHasSubscriptions;use App\Entity\Core\AgenciesHasTrials;use App\Entity\Core\AgenciesHasUsers;use App\Entity\Core\Users;use App\Form\Core\RegisterCompanyForm;use App\Form\Core\RegisterRecruiterForm;use App\Services\Core\Core;use App\Services\Core\Translations;use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Symfony\Component\EventDispatcher\EventDispatcherInterface;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\Annotation\Route;use Symfony\Component\HttpFoundation\Cookie;use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface;use Symfony\Component\Routing\Generator\UrlGeneratorInterface;use Symfony\Component\Routing\RouterInterface;use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;use Symfony\Component\HttpFoundation\Session\SessionInterface;use Symfony\Contracts\HttpClient\HttpClientInterface;use Doctrine\ORM\EntityManagerInterface;use App\Security\LoginFormAuthenticator;use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;class SecurityController extends AbstractController{ public static function getSubscribedServices(): array { $coreService = new Core(); return array_merge(parent::getSubscribedServices(),$coreService->getSubscribedServices()); } private $params; private $passwordEncoder; private $tokenGenerator; private $httpClient; private $em; private $us; private $ts; private $authenticator; public function __construct(EntityManagerInterface $em, UserPasswordEncoderInterface $passwordEncoder, TokenGeneratorInterface $tokenGenerator, ParameterBagInterface $params, HttpClientInterface $httpClient, \App\Services\Core\Users $us, Translations $translationService, LoginFormAuthenticator $authenticator, GuardAuthenticatorHandler $guardHandler ){ $this->passwordEncoder = $passwordEncoder; $this->tokenGenerator = $tokenGenerator; $this->params = $params; $this->httpClient = $httpClient; $this->em = $em; $this->us = $us; $this->ts = $translationService; $this->authenticator = $authenticator; $this->guardHandler = $guardHandler; } public function afterLogin(Request $request) { $lang = $request->getLocale(); $user = $this->getUser(); if($user == null) { if($lang == "en") { return $this->redirectToRoute('app_login'); } return $this->redirectToRoute('app_login', ['_locale' => $lang]); } $role = $this->us->hasRoles($user->getRoles(), "ROLE_SUPER_ADMIN"); if ($role == true) { return $this->redirectToRoute('backoffice_dashboard'); } $agency = $user->getCurrentAgency(); if($agency == null) { return $this->redirectToRoute('customer_register',['_locale' => $lang]); } $ahu = $this->em->getRepository(AgenciesHasUsers::class)->findOneBy(['agency' => $agency, 'user' => $user]); if($ahu == null) { Throw new \Exception("No user linked"); } if($agency->getType() == "recruiters") { return $this->redirectToRoute('gestion_recruiters_dashboard'); } elseif($agency->getType() == "companies") { return $this->redirectToRoute('gestion_companies_dashboard'); } return $this->redirectToRoute('homepage'); } /** * Page de connexion * @param Request $request * @param AuthenticationUtils $authenticationUtils * @return Response */ public function login(Request $request, AuthenticationUtils $authenticationUtils): Response { $session = $request->getSession(); $user = $this->getUser(); $lang = $request->getLocale(); if(empty($lang)) { $lang = "en"; } if($user !== null) { $actualPath = "homepage"; if($lang == "fr") { $actualPath = "locale_fr_homepage"; } elseif($lang == "nl") { $actualPath = "locale_nl_homepage"; } elseif($lang == "es") { $actualPath = "locale_es_homepage"; } elseif($lang == "de") { $actualPath = "locale_de_homepage"; } return $this->redirectToRoute($actualPath); } if($request->getLocale() != null) { $lang = $request->getLocale(); $session->set("lang",$lang); } $error = $authenticationUtils->getLastAuthenticationError(); $lastUsername = $authenticationUtils->getLastUsername(); return $this->render('login/login.html.twig',[ 'last_username' => $lastUsername, 'error' => $error ]); } /** * Le compte n'est pas activé * @param Request $request * @param AuthenticationUtils $authenticationUtils * @return Response */ public function enabled(Request $request): Response { $lang = $request->getLocale(); if(empty($lang)) { $lang = "en"; } $user = $this->getUser(); if($user === null) { if($lang == "fr") { return $this->redirectToRoute('locale_fr_homepage'); } elseif($lang == "nl") { return $this->redirectToRoute('locale_nl_homepage'); } elseif($lang == "es") { return $this->redirectToRoute('locale_es_homepage'); } elseif($lang == "de") { return $this->redirectToRoute('locale_de_homepage'); } return $this->redirectToRoute('homepage'); } return $this->render('login/enabled.html.twig'); } /** * Déconnexion * @return mixed * @throws \Exception */ public function logout() { throw new \Exception('This method can be blank - it will be intercepted by the logout key on your firewall'); }}