src/Controller/SecurityController.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\AuthLog;
  4. use App\Entity\Guardian;
  5. use App\Repository\ChildRepository;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  14. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  15. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  16. use Symfony\Component\Security\Core\Security as SecurityCore;
  17. class SecurityController extends AbstractController
  18. {
  19. /**
  20. * @Route("/login", name="app_login")
  21. */
  22. public function login(SecurityCore $security, AuthenticationUtils $authenticationUtils): Response
  23. {
  24. if ($security->isGranted('ROLE_USER')) {
  25. return $this->redirectToRoute('dashboard');
  26. }
  27. return $this->render('security/login.html.twig', [
  28. // last username entered by the user (if any)
  29. 'last_username' => $authenticationUtils->getLastUsername(),
  30. // last authentication error (if any)
  31. 'error' => $authenticationUtils->getLastAuthenticationError(),
  32. ]);
  33. }
  34. /**
  35. * @Route("/logout", name="app_logout")
  36. */
  37. public function logout()
  38. {
  39. throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  40. }
  41. /**
  42. * @Route("/auth_log", name="app_auth_log_index")
  43. * @Security("is_granted('ROLE_ADMIN') and is_granted('IS_AUTHENTICATED_FULLY')")
  44. */
  45. public function authLog()
  46. {
  47. return $this->render('security/authLog.html.twig', [
  48. ]);
  49. }
  50. /**
  51. * @Route("/tokenlogin", name="app_token_login")
  52. */
  53. public function tokenLogin(Request $request, ChildRepository $childRepository, RequestStack $requestStack, EntityManagerInterface $entityManager): Response
  54. {
  55. $user = $this->getUser();
  56. if ($user) {
  57. return $this->redirectToRoute('dashboard');
  58. }
  59. $token = $request->request->get('token');
  60. if($token)
  61. {
  62. $child = $childRepository->findOneBy(['loginToken' => $token]);
  63. if($child) {
  64. if($child->getGuardian() && $child->getGuardian()->getStatus() == Guardian::STATUS_LEFT['text'])
  65. {
  66. return $this->render('security/token.html.twig', []);
  67. }
  68. $token = new UsernamePasswordToken($child, 'main', $child->getRoles());
  69. $this->get('security.token_storage')->setToken($token);
  70. $requestStack->getSession()->set('_security_main', serialize($token));
  71. $requestStack->getSession()->set('tokenlogin', ['token' => $request->request->get('token'), 'date' => (new \DateTime())->format('Y-m-d H:i:s')]);
  72. $authLog = (new AuthLog())
  73. ->setStatus('success token')
  74. ->setEmail($child->getEmail());
  75. $authLog->setUserAgent(
  76. json_encode([
  77. 'child_id' => $child->getId(),
  78. 'HTTP_USER_AGENT' => isset($_SERVER['HTTP_USER_AGENT'])?$_SERVER['HTTP_USER_AGENT']:'',
  79. 'HTTP_SEC_CH_UA_PLATFORM' => isset($_SERVER['HTTP_SEC_CH_UA_PLATFORM'])?$_SERVER['HTTP_SEC_CH_UA_PLATFORM']:'',
  80. 'HTTP_SEC_CH_UA_MOBILE' => isset($_SERVER['HTTP_SEC_CH_UA_MOBILE'])?$_SERVER['HTTP_SEC_CH_UA_MOBILE']:'',
  81. 'HTTP_SEC_CH_UA' => isset($_SERVER['HTTP_SEC_CH_UA'])?$_SERVER['HTTP_SEC_CH_UA']:'',
  82. 'REMOTE_ADDR' => isset($_SERVER['REMOTE_ADDR'])?$_SERVER['REMOTE_ADDR']:'',
  83. ])
  84. );
  85. $entityManager->persist($authLog);
  86. $entityManager->flush();
  87. return $this->redirectToRoute('me_child');
  88. }
  89. }
  90. return $this->render('security/token.html.twig', [
  91. ]);
  92. }
  93. }