/** * Authentication logic to allow Ldap user * * @param \IMAG\LdapBundle\User\LdapUserInterface $user * @param TokenInterface $token * * @return \Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken $token */ private function ldapAuthenticate(LdapUserInterface $user, TokenInterface $token) { // provide credential to LdapUserEvent $userEvent = new LdapUserEvent($user, $token->getCredentials()); if (null !== $this->dispatcher) { try { $this->dispatcher->dispatch(LdapEvents::PRE_BIND, $userEvent); } catch (AuthenticationException $expt) { if ($this->hideUserNotFoundExceptions) { throw new BadCredentialsException('Bad credentials', 0, $expt); } throw $expt; } } $this->bind($user, $token); if (null === $user->getDn()) { $user = $this->reloadUser($user); } if (null !== $this->dispatcher) { // provide credential to LdapUserEvent $userEvent = new LdapUserEvent($user, $token->getCredentials()); try { $this->dispatcher->dispatch(LdapEvents::POST_BIND, $userEvent); } catch (AuthenticationException $authenticationException) { if ($this->hideUserNotFoundExceptions) { throw new BadCredentialsException('Bad credentials', 0, $authenticationException); } throw $authenticationException; } } $token = new UsernamePasswordToken($userEvent->getUser(), null, $this->providerKey, $userEvent->getUser()->getRoles()); $token->setAttributes($token->getAttributes()); return $token; }