Advertisement
vansanblch

Untitled

May 23rd, 2013
615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.74 KB | None | 0 0
  1. namespace App;
  2.  
  3. use Symfony\Component\HttpFoundation\Response;
  4. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  5. use Symfony\Component\Security\Http\Firewall\ListenerInterface;
  6. use Symfony\Component\Security\Http\HttpUtils;
  7. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  8. use Symfony\Component\Security\Core\SecurityContextInterface;
  9. use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
  10.  
  11. class LdapAuthenticationListener implements ListenerInterface
  12. {
  13.     private $securityContext;
  14.     private $authenticationManager;
  15.     private $httpUtils;
  16.  
  17.     public function __construct(SecurityContextInterface $securityContext, AuthenticationManagerInterface $authenticationManager, HttpUtils $httpUtils)
  18.     {
  19.         $this->securityContext = $securityContext;
  20.         $this->authenticationManager = $authenticationManager;
  21.         $this->httpUtils = $httpUtils;
  22.     }
  23.  
  24.     public function handle(GetResponseEvent $ev)
  25.     {
  26.         $request = $ev->getRequest();
  27.  
  28.         $username = $request->get('_username');
  29.         $password = $request->get('_password');
  30.  
  31.         if (!$username && !$password) {
  32.             return;
  33.         }
  34.  
  35.         $token = new LdapUserToken($username, $password);
  36.  
  37.         try {
  38.             $authToken = $this->authenticationManager->authenticate($token);
  39.             $this->securityContext->setToken($authToken);
  40.  
  41.             return;
  42.         } catch (AuthenticationException $ex) {
  43.             $request->getSession()->set(SecurityContextInterface::AUTHENTICATION_ERROR, $ex);
  44.  
  45.             $test = $this->httpUtils->createRedirectResponse(
  46.                 $request,
  47.                 '/login'
  48.             );
  49.             return $test;
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement