Advertisement
Guest User

Untitled

a guest
Sep 10th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. <?php
  2.  
  3. namespace AppBundle\Security;
  4.  
  5. use AppBundle\Form\LoginForm;
  6. use Doctrine\ORM\EntityManager;
  7. use Symfony\Component\Form\FormFactoryInterface;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\Routing\RouterInterface;
  10. use Symfony\Component\Security\Core\Security;
  11. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder;
  12. use Symfony\Component\Security\Core\User\UserInterface;
  13. use Symfony\Component\Security\Core\User\UserProviderInterface;
  14. use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
  15.  
  16. class LoginFormAuthenticator extends AbstractFormLoginAuthenticator
  17. {
  18. private $formFactory;
  19. private $em;
  20. private $router;
  21. private $passwordEncoder;
  22.  
  23.  
  24.  
  25. public function __construct(FormFactoryInterface $formFactory, EntityManager $em, RouterInterface $router, UserPasswordEncoder $passwordEncoder)
  26. {
  27. $this->formFactory = $formFactory;
  28. $this->em = $em;
  29. $this->router = $router;
  30. $this->passwordEncoder = $passwordEncoder;
  31. }
  32.  
  33. public function getCredentials(Request $request)
  34. {
  35. $isLoginSubmit = $request->getPathInfo() == '/login' && $request->isMethod('POST');
  36. if (!$isLoginSubmit) {
  37. // skip authentication
  38. return;
  39. }
  40.  
  41. $form = $this->formFactory->create(LoginForm::class);
  42. $form->handleRequest($request);
  43.  
  44. $data = $form->getData();
  45. $request->getSession()->set(
  46. Security::LAST_USERNAME,
  47. $data['_username']
  48. );
  49.  
  50. return $data;
  51. }
  52.  
  53. public function getUser($credentials, UserProviderInterface $userProvider)
  54. {
  55. $username = $credentials['_username'];
  56.  
  57. return $this->em->getRepository('AppBundle:User')
  58. ->findOneBy(['email' => $username]);
  59. }
  60.  
  61. public function checkCredentials($credentials, UserInterface $user)
  62. {
  63. $password = $credentials['_password'];
  64.  
  65. if ($this->passwordEncoder->isPasswordValid($user, $password)) {
  66. return true;
  67. }
  68.  
  69. return false;
  70. }
  71.  
  72. protected function getLoginUrl()
  73. {
  74. return $this->router->generate('login');
  75. }
  76.  
  77. protected function getDefaultSuccessRedirectUrl()
  78. {
  79.  
  80. return $this->router->generate('homepage');
  81.  
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement