Advertisement
Guest User

LoginFormAuthenticator

a guest
Jun 15th, 2017
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. <?php
  2. /**
  3. * LoginFormAuthenticator.php
  4. * User: draiz
  5. * Date: 14/06/2017
  6. * Time: 14:09
  7. */
  8.  
  9. namespace AdminBundle\Security;
  10.  
  11. use AdminBundle\Entity\AdminUser;
  12. use AdminBundle\Form\LoginForm;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Symfony\Component\Form\FormFactoryInterface;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\Routing\RouterInterface;
  17. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  18. use Symfony\Component\Security\Core\Security;
  19. use Symfony\Component\Security\Core\User\UserInterface;
  20. use Symfony\Component\Security\Core\User\UserProviderInterface;
  21. use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
  22. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  23. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  24. use Symfony\Component\HttpFoundation\RedirectResponse;
  25.  
  26.  
  27. class LoginFormAuthenticator extends AbstractFormLoginAuthenticator
  28. {
  29. use TargetPathTrait;
  30.  
  31. private $formFactory;
  32. private $em;
  33. private $router;
  34. private $passwordEncoder;
  35.  
  36. public function __construct(FormFactoryInterface $formFactory, EntityManagerInterface $em, RouterInterface $router, UserPasswordEncoderInterface $passwordEncoder)
  37. {
  38. $this->formFactory = $formFactory;
  39. $this->em = $em;
  40. $this->router = $router;
  41. $this->passwordEncoder = $passwordEncoder;
  42. }
  43.  
  44. public function getCredentials(Request $request)
  45. {
  46. $isLoginSubmit = $request->getPathInfo() == '/login' && $request->isMethod('POST');
  47.  
  48. if ($isLoginSubmit) {
  49.  
  50. $req = $request->request->get('login_form');
  51.  
  52. return [
  53. '_username' => $req['_username'],
  54. '_password' => $req['_password'],
  55. ];
  56. }
  57.  
  58. $form = $this->formFactory->create(LoginForm::class);
  59. $form->handleRequest($request);
  60.  
  61. $data = $form->getData();
  62.  
  63. return $data;
  64. }
  65.  
  66. public function getUser($credentials, UserProviderInterface $userProvider)
  67. {
  68. $username = $credentials['_username'];
  69.  
  70. return $this->em->getRepository('AdminBundle:AdminUser')
  71. ->findOneBy(['email' => $username]);
  72. }
  73.  
  74. public function checkCredentials($credentials, UserInterface $user)
  75. {
  76. $password = $credentials['_password'];
  77.  
  78. if ($this->passwordEncoder->isPasswordValid($user, $password)) {
  79. return true;
  80. }
  81.  
  82. return false;
  83. }
  84.  
  85. protected function getLoginUrl()
  86. {
  87. return $this->router->generate('admin_security_login');
  88. }
  89.  
  90.  
  91. public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
  92. {
  93. $session = $request->getSession();
  94. $user = $token->getUser();
  95.  
  96. if ( $user instanceof AdminUser )
  97. $session->set( 'last_connection', $user->getDateLastConnection() );
  98.  
  99. // Modification de la date de dernière connexion
  100. $user->setDateLastConnection( new \Datetime() );
  101. $this->em->flush();
  102.  
  103. // if the user hits a secure page and start() was called, this was
  104. // the URL they were on, and probably where you want to redirect to
  105. $targetPath = $this->getTargetPath($request->getSession(), $providerKey);
  106.  
  107. if (!$targetPath) {
  108. $targetPath = $this->router->generate('admin_index');
  109. }
  110.  
  111. return new RedirectResponse($targetPath);
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement