Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace App;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\HttpKernel\Event\GetResponseEvent;
- use Symfony\Component\Security\Http\Firewall\ListenerInterface;
- use Symfony\Component\Security\Http\HttpUtils;
- use Symfony\Component\Security\Core\Exception\AuthenticationException;
- use Symfony\Component\Security\Core\SecurityContextInterface;
- use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
- class LdapAuthenticationListener implements ListenerInterface
- {
- private $securityContext;
- private $authenticationManager;
- private $httpUtils;
- public function __construct(SecurityContextInterface $securityContext, AuthenticationManagerInterface $authenticationManager, HttpUtils $httpUtils)
- {
- $this->securityContext = $securityContext;
- $this->authenticationManager = $authenticationManager;
- $this->httpUtils = $httpUtils;
- }
- public function handle(GetResponseEvent $ev)
- {
- $request = $ev->getRequest();
- $username = $request->get('_username');
- $password = $request->get('_password');
- if (!$username && !$password) {
- return;
- }
- $token = new LdapUserToken($username, $password);
- try {
- $authToken = $this->authenticationManager->authenticate($token);
- $this->securityContext->setToken($authToken);
- return;
- } catch (AuthenticationException $ex) {
- $request->getSession()->set(SecurityContextInterface::AUTHENTICATION_ERROR, $ex);
- $test = $this->httpUtils->createRedirectResponse(
- $request,
- '/login'
- );
- return $test;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement