Advertisement
Guest User

Untitled

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