Created
August 10, 2016 19:36
-
-
Save ravenberg/71f5020a30af882390a40c18d0c43bf6 to your computer and use it in GitHub Desktop.
LoginFormAuthenticator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Security; | |
use AppBundle\Form\LoginForm; | |
use Doctrine\ORM\EntityManager; | |
use Symfony\Component\Form\FormFactoryInterface; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\RequestStack; | |
use Symfony\Component\Routing\RouterInterface; | |
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; | |
/** | |
* This services intercepts login form submission and handles it. | |
*/ | |
class LoginFormAuthenticator extends AbstractFormLoginAuthenticator | |
{ | |
/** | |
* @var FormFactoryInterface | |
*/ | |
private $form_factory; | |
/** | |
* @var EntityManager | |
*/ | |
private $em; | |
/** | |
* @var RouterInterface | |
*/ | |
private $router; | |
/** | |
* @var RequestStack | |
*/ | |
private $request_stack; | |
/** | |
* @param FormFactoryInterface $form_factory | |
* @param EntityManager $em | |
* @param RouterInterface $router | |
* @param RequestStack $request_stack | |
*/ | |
public function __construct( | |
FormFactoryInterface $form_factory, | |
EntityManager $em, | |
RouterInterface $router, | |
RequestStack $request_stack | |
) | |
{ | |
$this->form_factory = $form_factory; | |
$this->em = $em; | |
$this->router = $router; | |
$this->request_stack = $request_stack; | |
} | |
/** | |
* This method gets the credentials that have put into the login form. | |
* | |
* @inheritDoc | |
*/ | |
public function getCredentials(Request $request) | |
{ | |
if (!$request->getPathInfo() == '/login' && !$request->isMethod('POST')) { | |
return; | |
} | |
$form = $this->form_factory->create(LoginForm::class); | |
$form->handleRequest($request); | |
$data = $form->getData(); | |
return $data; | |
} | |
/** | |
* Symfony calls getUser() and has the value from getCredentials() passed in as value for | |
* $credentials. | |
* | |
* @inheritDoc | |
*/ | |
public function getUser($credentials, UserProviderInterface $userProvider) | |
{ | |
$username = $credentials['_username']; | |
$request = $this->request_stack->getCurrentRequest(); | |
$request->getSession()->set( | |
Security::LAST_USERNAME, | |
$username | |
); | |
return $this->em->getRepository('AppBundle:User')->findOneBy(['email' => $username]); | |
} | |
/** | |
* Symfony Guard calls checkCredentials() as soon as getUser() returns something | |
* | |
* @inheritDoc | |
*/ | |
public function checkCredentials($credentials, UserInterface $user) | |
{ | |
return 'Welkom01!' == $credentials['_password']; | |
} | |
/** | |
* @inheritDoc | |
*/ | |
protected function getLoginUrl() | |
{ | |
return $this->router->generate('security_login'); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function getDefaultSuccessRedirectUrl() | |
{ | |
return $this->router->generate('homepage'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment