src/Controller/Login/SecurityController.php line 118

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Login;
  3. use App\Entity\Core\Agencies;
  4. use App\Entity\Core\AgenciesHasSubscriptions;
  5. use App\Entity\Core\AgenciesHasTrials;
  6. use App\Entity\Core\AgenciesHasUsers;
  7. use App\Entity\Core\Users;
  8. use App\Form\Core\RegisterCompanyForm;
  9. use App\Form\Core\RegisterRecruiterForm;
  10. use App\Services\Core\Core;
  11. use App\Services\Core\Translations;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
  13. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. use Symfony\Component\HttpFoundation\Cookie;
  21. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  22. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  23. use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface;
  24. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  25. use Symfony\Component\Routing\RouterInterface;
  26. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  27. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  28. use Symfony\Contracts\HttpClient\HttpClientInterface;
  29. use Doctrine\ORM\EntityManagerInterface;
  30. use App\Security\LoginFormAuthenticator;
  31. use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
  32. class SecurityController extends AbstractController
  33. {
  34. public static function getSubscribedServices(): array
  35. {
  36. $coreService = new Core();
  37. return array_merge(parent::getSubscribedServices(),$coreService->getSubscribedServices());
  38. }
  39. private $params;
  40. private $passwordEncoder;
  41. private $tokenGenerator;
  42. private $httpClient;
  43. private $em;
  44. private $us;
  45. private $ts;
  46. private $authenticator;
  47. public function __construct(EntityManagerInterface $em,
  48. UserPasswordEncoderInterface $passwordEncoder,
  49. TokenGeneratorInterface $tokenGenerator,
  50. ParameterBagInterface $params,
  51. HttpClientInterface $httpClient,
  52. \App\Services\Core\Users $us,
  53. Translations $translationService,
  54. LoginFormAuthenticator $authenticator,
  55. GuardAuthenticatorHandler $guardHandler
  56. ){
  57. $this->passwordEncoder = $passwordEncoder;
  58. $this->tokenGenerator = $tokenGenerator;
  59. $this->params = $params;
  60. $this->httpClient = $httpClient;
  61. $this->em = $em;
  62. $this->us = $us;
  63. $this->ts = $translationService;
  64. $this->authenticator = $authenticator;
  65. $this->guardHandler = $guardHandler;
  66. }
  67. public function afterLogin(Request $request)
  68. {
  69. $lang = $request->getLocale();
  70. $user = $this->getUser();
  71. if($user == null) {
  72. if($lang == "en") {
  73. return $this->redirectToRoute('app_login');
  74. }
  75. return $this->redirectToRoute('app_login', ['_locale' => $lang]);
  76. }
  77. $role = $this->us->hasRoles($user->getRoles(), "ROLE_SUPER_ADMIN");
  78. if ($role == true) {
  79. return $this->redirectToRoute('backoffice_dashboard');
  80. }
  81. $agency = $user->getCurrentAgency();
  82. if($agency == null) {
  83. return $this->redirectToRoute('customer_register',['_locale' => $lang]);
  84. }
  85. $ahu = $this->em->getRepository(AgenciesHasUsers::class)->findOneBy(['agency' => $agency, 'user' => $user]);
  86. if($ahu == null) {
  87. Throw new \Exception("No user linked");
  88. }
  89. if($agency->getType() == "recruiters") {
  90. return $this->redirectToRoute('gestion_recruiters_dashboard');
  91. } elseif($agency->getType() == "companies") {
  92. return $this->redirectToRoute('gestion_companies_dashboard');
  93. }
  94. return $this->redirectToRoute('homepage');
  95. }
  96. /**
  97. * Page de connexion
  98. * @param Request $request
  99. * @param AuthenticationUtils $authenticationUtils
  100. * @return Response
  101. */
  102. public function login(Request $request, AuthenticationUtils $authenticationUtils): Response
  103. {
  104. $session = $request->getSession();
  105. $user = $this->getUser();
  106. $lang = $request->getLocale();
  107. if(empty($lang)) {
  108. $lang = "en";
  109. }
  110. if($user !== null) {
  111. $actualPath = "homepage";
  112. if($lang == "fr") {
  113. $actualPath = "locale_fr_homepage";
  114. } elseif($lang == "nl") {
  115. $actualPath = "locale_nl_homepage";
  116. } elseif($lang == "es") {
  117. $actualPath = "locale_es_homepage";
  118. } elseif($lang == "de") {
  119. $actualPath = "locale_de_homepage";
  120. }
  121. return $this->redirectToRoute($actualPath);
  122. }
  123. if($request->getLocale() != null) {
  124. $lang = $request->getLocale();
  125. $session->set("lang",$lang);
  126. }
  127. $error = $authenticationUtils->getLastAuthenticationError();
  128. $lastUsername = $authenticationUtils->getLastUsername();
  129. return $this->render('login/login.html.twig',[
  130. 'last_username' => $lastUsername,
  131. 'error' => $error
  132. ]);
  133. }
  134. /**
  135. * Le compte n'est pas activé
  136. * @param Request $request
  137. * @param AuthenticationUtils $authenticationUtils
  138. * @return Response
  139. */
  140. public function enabled(Request $request): Response
  141. {
  142. $lang = $request->getLocale();
  143. if(empty($lang)) {
  144. $lang = "en";
  145. }
  146. $user = $this->getUser();
  147. if($user === null) {
  148. if($lang == "fr") {
  149. return $this->redirectToRoute('locale_fr_homepage');
  150. } elseif($lang == "nl") {
  151. return $this->redirectToRoute('locale_nl_homepage');
  152. } elseif($lang == "es") {
  153. return $this->redirectToRoute('locale_es_homepage');
  154. } elseif($lang == "de") {
  155. return $this->redirectToRoute('locale_de_homepage');
  156. }
  157. return $this->redirectToRoute('homepage');
  158. }
  159. return $this->render('login/enabled.html.twig');
  160. }
  161. /**
  162. * Déconnexion
  163. * @return mixed
  164. * @throws \Exception
  165. */
  166. public function logout()
  167. {
  168. throw new \Exception('This method can be blank - it will be intercepted by the logout key on your firewall');
  169. }
  170. }