Advertisement
n0rthway

knp-formauth

May 30th, 2017
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.01 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\RedirectResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\Routing\RouterInterface;
  11. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. use Symfony\Component\Security\Core\Security;
  13. use Symfony\Component\Security\Core\User\UserInterface;
  14. use Symfony\Component\Security\Core\User\UserProviderInterface;
  15. use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
  16. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  17.  
  18. class LoginFormAuthenticator extends AbstractFormLoginAuthenticator
  19. {
  20.     use TargetPathTrait;
  21.  
  22.     /**
  23.      * @var FormFactoryInterface
  24.      */
  25.     private $formFactory;
  26.  
  27.     /**
  28.      * @var EntityManager
  29.      */
  30.     private $em;
  31.  
  32.     /**
  33.      * @var RouterInterface
  34.      */
  35.     private $router;
  36.  
  37.     public function __construct(FormFactoryInterface $formFactory, EntityManager $em, RouterInterface $router)
  38.     {
  39.         $this->formFactory = $formFactory;
  40.         $this->em = $em;
  41.         $this->router = $router;
  42.     }
  43.  
  44.     public function getCredentials(Request $request)
  45.     {
  46.         $isLoginSubmit = $request->getPathInfo() == '/login' && $request->isMethod('POST');
  47.  
  48.         if ($isLoginSubmit) {
  49.  
  50.             $req = $request->request->get('login_form');
  51.  
  52.             return [
  53.                 '_username' => $req['_username'],
  54.                 '_password' => $req['_password'],
  55.             ];
  56.         }
  57.  
  58.         $form = $this->formFactory->create(LoginForm::class);
  59.         $form->handleRequest($request);
  60.  
  61.         dump($form);
  62.  
  63.         $data = $form->getData();
  64.  
  65.         $request->getSession()->set(
  66.             Security::LAST_USERNAME,
  67.             $data['_username']
  68.         );
  69.  
  70.         return $data;
  71.     }
  72.  
  73.     public function getUser($credentials, UserProviderInterface $userProvider)
  74.     {
  75.         $username = $credentials['_username'];
  76.  
  77.         return $this->em->getRepository('AppBundle:User')
  78.             ->findOneBy(['email' => $username]);
  79.     }
  80.  
  81.     public function checkCredentials($credentials, UserInterface $user)
  82.     {
  83.         $password = $credentials['_password'];
  84.  
  85.         if ($password == 'klm123') {
  86.             return true;
  87.         }
  88.  
  89.         return false;
  90.     }
  91.  
  92.     protected function getLoginUrl()
  93.     {
  94.         return $this->router->generate('security_login');
  95.     }
  96.  
  97.     public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
  98.     {
  99.         // if the user hits a secure page and start() was called, this was
  100.         // the URL they were on, and probably where you want to redirect to
  101.         $targetPath = $this->getTargetPath($request->getSession(), $providerKey);
  102.  
  103.         if (!$targetPath) {
  104.             $targetPath = $this->router->generate('homepage');
  105.         }
  106.  
  107.         return new RedirectResponse($targetPath);
  108.     }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement