Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace GameBundle\Controller\Security;
- use Doctrine\ORM\EntityManager;
- use GameBundle\Entity\Player;
- use GameBundle\Form\LoginForm;
- use Symfony\Component\Form\FormFactoryInterface;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\Routing\RouterInterface;
- use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder;
- use Symfony\Component\Security\Core\Security;
- use Symfony\Component\Security\Core\User\UserInterface;
- use Symfony\Component\Security\Core\User\UserProviderInterface;
- use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
- class LoginFormAuthenticator extends AbstractFormLoginAuthenticator
- {
- private $formFactory;
- private $em;
- private $router;
- private $passwordEncoder;
- public function __construct(FormFactoryInterface $formFactory, EntityManager $em, RouterInterface $router,UserPasswordEncoder $passwordEncoder)
- {
- $this->formFactory = $formFactory;
- $this->em = $em;
- $this->router = $router;
- $this->passwordEncoder = $passwordEncoder;
- }
- public function getCredentials(Request $request)
- {
- $isLoginSubmit = $request->getPathInfo() == '/login' && $request->isMethod('POST');
- if(!$isLoginSubmit){
- return null;
- }
- $form = $this->formFactory->create(LoginForm::class);
- $form->handleRequest($request);
- $data = $form->getData();
- $request->getSession()->set(
- Security::LAST_USERNAME,
- $data['_username']
- );
- return $data;
- }
- public function getUser($credentials, UserProviderInterface $userProvider)
- {
- $username = $credentials['_username'];
- return $this->em->getRepository('GameBundle:Player')
- ->findOneBy(['nickname' => $username]);
- }
- public function checkCredentials($credentials, UserInterface $player)
- {
- $password = $credentials['_password'];
- if ($this->passwordEncoder->isPasswordValid($player, $password)) {
- return true;
- }
- return false;;
- }
- protected function getLoginUrl()
- {
- return $this->router->generate('security_login');
- }
- protected function getDefaultSuccessRedirectUrl()
- {
- return $this->router->generate('homepage');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement