Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace App;
- use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
- use Symfony\Component\Security\Core\User\UserProviderInterface;
- use Symfony\Component\Security\Core\Exception\AuthenticationServiceException;
- use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
- class LdapAuthenticationProvider implements AuthenticationProviderInterface
- {
- private $userProvider;
- private $providerKey;
- public function __construct(UserProviderInterface $userProvider, $providerKey)
- {
- $this->userProvider = $userProvider;
- $this->providerKey = $providerKey;
- }
- /**
- * {@inheritdoc}
- */
- protected function retrieveUser($username, LdapUserToken $token)
- {
- $user = $token->getUser();
- if ($user instanceof UserInterface) {
- return $user;
- }
- try {
- $user = $this->userProvider->loadUserByUsernameAndPassword(
- $user,
- $token->getCredentials()
- );
- if (!$user instanceof UserInterface) {
- throw new AuthenticationServiceException('The user provider must return a UserInterface object.');
- }
- return $user;
- } catch (UsernameNotFoundException $notFound) {
- throw $notFound;
- }
- }
- public function supports(TokenInterface $token)
- {
- return $token instanceof LdapUserToken;
- }
- public function authenticate(TokenInterface $token)
- {
- if (!$this->supports($token)) {
- return null;
- }
- $username = $token->getUsername();
- if (empty($username)) {
- $username = 'NONE_PROVIDED';
- }
- try {
- $user = $this->retrieveUser($username, $token);
- } catch (UsernameNotFoundException $notFound) {
- throw new BadCredentialsException('Bad credentials', 0, $notFound);
- }
- if (!$user instanceof UserInterface) {
- throw new AuthenticationServiceException('retrieveUser() must return a UserInterface.');
- }
- try {
- $this->checkAuthentication($user, $token);
- } catch (BadCredentialsException $e) {
- if ($this->hideUserNotFoundExceptions) {
- throw new BadCredentialsException('Bad credentials', 0, $e);
- }
- throw $e;
- }
- $authenticatedToken = new LdapUserToken(
- $user,
- $token->getCredentials(),
- $user->getRoles()
- );
- return $authenticatedToken;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement