src/Controller/SecurityController.php line 126

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace App\Controller;
  11. use App\Entity\LoginLog;
  12. use App\Repository\UserRepository;
  13. use App\Services\OktaApiService;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  21. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  22. /**
  23. * Controller used to manage the application security.
  24. * See https://symfony.com/doc/current/cookbook/security/form_login_setup.html.
  25. *
  26. * @author Ryan Weaver <weaverryan@gmail.com>
  27. * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  28. */
  29. class SecurityController extends AbstractController
  30. {
  31. private SessionInterface $session;
  32. private OktaApiService $okta;
  33. private UserRepository $userRepository;
  34. private EntityManagerInterface $em;
  35. private TokenStorageInterface $tokenStorage;
  36. public function __construct(
  37. SessionInterface $session,
  38. OktaApiService $okta,
  39. UserRepository $UserRepository,
  40. EntityManagerInterface $em,
  41. TokenStorageInterface $tokenStorage
  42. ) {
  43. $this->session = $session;
  44. $this->okta = $okta;
  45. $this->userRepository = $UserRepository;
  46. $this->em = $em;
  47. $this->tokenStorage = $tokenStorage;
  48. }
  49. /**
  50. * @Route("/errorlogin", name="security_errorlogin")
  51. */
  52. public function errorlogin(AuthenticationUtils $helper): Response
  53. {
  54. return $this->render('security/login.html.twig', [
  55. // last username entered by the user (if any)
  56. 'last_username' => '',
  57. 'logoutMessage' => '',
  58. // last authentication error (if any)
  59. 'error' => $helper->getLastAuthenticationError(),
  60. 'errorMessage' => 'You do not have access to this application. Please request access to Baker Hughes team',
  61. 'legacy' => 'N', 'myidurl' => $this->okta->buildAuthorizeUrl(),
  62. ]);
  63. }
  64. private function LoginLog($user, $username, $action, $attuale, $request) {
  65. $ip = $request->getClientIp();
  66. $loginLog = new LoginLog();
  67. // $location = []; //unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $ip)); //$_SERVER['REMOTE_ADDR']);
  68. $_tmp = file_get_contents('https://ipinfo.io/' . $ip . '/json');
  69. if ((bool) $_tmp) {
  70. $location = json_decode($_tmp, true);
  71. if (json_last_error() !== JSON_ERROR_NONE || !is_array($location)) {
  72. $location = [];
  73. }
  74. } else {
  75. $location = [];
  76. }
  77. $loginLog->setUser($user);
  78. $loginLog->setUsername($username);
  79. $loginLog->setDataLogin(new \DateTime());
  80. $loginLog->setAttuale($attuale);
  81. $loginLog->setAAction($action);
  82. $loginLog->setAIp($ip);
  83. $loginLog->setAAgent($request->headers->get('User-Agent'));
  84. $loginLog->setGeoLoc($location);
  85. $this->em->persist($loginLog);
  86. $this->em->flush();
  87. }
  88. /**
  89. * @Route("/logoutpage", name="security_logoutpage")
  90. */
  91. public function logoutpage(Request $request, AuthenticationUtils $helper): Response
  92. {
  93. $user = $this->getUser();
  94. if ($user != null) {
  95. $this->LoginLog($user, $user->getUsername(), 9003, 0, $request);
  96. }
  97. $this->session->set('userislocked', 0);
  98. $this->tokenStorage->setToken(null);
  99. $this->session->set('_security_main', null);
  100. return $this->render('security/login.html.twig', [
  101. // last username entered by the user (if any)
  102. 'last_username' => '',
  103. // last authentication error (if any)
  104. 'error' => $helper->getLastAuthenticationError(),
  105. 'logoutMessage' => 'Logout successful',
  106. 'errorMessage' => '',
  107. 'legacy' => 'N', 'myidurl' => $this->okta->buildAuthorizeUrl(),
  108. ]);
  109. }
  110. /**
  111. * @Route("/login", name="security_login")
  112. */
  113. public function login(AuthenticationUtils $helper): Response
  114. {
  115. if ($this->getParameter('app.legacy_login') === 'Y') {
  116. return $this->render('security/login.html.twig', [
  117. // last username entered by the user (if any)
  118. 'last_username' => $helper->getLastUsername(),
  119. // last authentication error (if any)
  120. 'logoutMessage' => '',
  121. 'error' => $helper->getLastAuthenticationError(),
  122. 'errorMessage' => '',
  123. 'legacy' => 'Y', 'myidurl' => ''
  124. ]);
  125. }
  126. else {
  127. return $this->redirect($this->okta->buildAuthorizeUrl());
  128. }
  129. /* $token = $this->okta->authorizeUser();
  130. if (!$token) {
  131. // return $this->redirectToRoute('home');
  132. return $this->render('security/login.html.twig', [
  133. // last username entered by the user (if any)
  134. 'last_username' => $helper->getLastUsername(),
  135. // last authentication error (if any)
  136. 'error' => $helper->getLastAuthenticationError(),
  137. 'legacy' => 'N',
  138. 'myidurl' => $this->getParameter('app.okta_app_url')
  139. ]);
  140. }
  141. $email = $token->email;
  142. $username = $token->username;
  143. $user = $this->userRepository->loadUserByUsername($username);
  144. if (! $user) {
  145. return $this->render('security/login.html.twig', [
  146. // last username entered by the user (if any)
  147. 'last_username' => $helper->getLastUsername(),
  148. // last authentication error (if any)
  149. 'error' => 'Unable to login',
  150. 'legacy' => 'N', 'myidurl' => $this->getParameter('app.okta_app_url')]);
  151. }
  152. // Manually authenticate the user
  153. $token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
  154. $this->get('security.token_storage')->setToken($token);
  155. $this->get('session')->set('_security_main', serialize($token));
  156. return $this->redirectToRoute('welcome_page');
  157. */
  158. /*
  159. return $this->render('security/login.html.twig', [
  160. // last username entered by the user (if any)
  161. 'last_username' => $helper->getLastUsername(),
  162. // last authentication error (if any)
  163. 'error' => $helper->getLastAuthenticationError(),
  164. ]);
  165. * */
  166. }
  167. /**
  168. * This is the route the user can use to logout.
  169. *
  170. * But, this will never be executed. Symfony will intercept this first
  171. * and handle the logout automatically. See logout in app/config/security.yml
  172. *
  173. * @Route("/logout", name="security_logout")
  174. */
  175. public function logout(): void
  176. {
  177. throw new \Exception('This should never be reached!');
  178. }
  179. }