Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * LoginFormAuthenticator.php
- * User: draiz
- * Date: 14/06/2017
- * Time: 14:09
- */
- namespace AdminBundle\Security;
- use AdminBundle\Entity\AdminUser;
- use AdminBundle\Form\LoginForm;
- use Doctrine\ORM\EntityManagerInterface;
- use Symfony\Component\Form\FormFactoryInterface;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\Routing\RouterInterface;
- use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
- 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;
- use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
- use Symfony\Component\Security\Http\Util\TargetPathTrait;
- use Symfony\Component\HttpFoundation\RedirectResponse;
- class LoginFormAuthenticator extends AbstractFormLoginAuthenticator
- {
- use TargetPathTrait;
- private $formFactory;
- private $em;
- private $router;
- private $passwordEncoder;
- public function __construct(FormFactoryInterface $formFactory, EntityManagerInterface $em, RouterInterface $router, UserPasswordEncoderInterface $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) {
- $req = $request->request->get('login_form');
- return [
- '_username' => $req['_username'],
- '_password' => $req['_password'],
- ];
- }
- $form = $this->formFactory->create(LoginForm::class);
- $form->handleRequest($request);
- $data = $form->getData();
- return $data;
- }
- public function getUser($credentials, UserProviderInterface $userProvider)
- {
- $username = $credentials['_username'];
- return $this->em->getRepository('AdminBundle:AdminUser')
- ->findOneBy(['email' => $username]);
- }
- public function checkCredentials($credentials, UserInterface $user)
- {
- $password = $credentials['_password'];
- if ($this->passwordEncoder->isPasswordValid($user, $password)) {
- return true;
- }
- return false;
- }
- protected function getLoginUrl()
- {
- return $this->router->generate('admin_security_login');
- }
- public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
- {
- $session = $request->getSession();
- $user = $token->getUser();
- if ( $user instanceof AdminUser )
- $session->set( 'last_connection', $user->getDateLastConnection() );
- // Modification de la date de dernière connexion
- $user->setDateLastConnection( new \Datetime() );
- $this->em->flush();
- // if the user hits a secure page and start() was called, this was
- // the URL they were on, and probably where you want to redirect to
- $targetPath = $this->getTargetPath($request->getSession(), $providerKey);
- if (!$targetPath) {
- $targetPath = $this->router->generate('admin_index');
- }
- return new RedirectResponse($targetPath);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement