Advertisement
vansanblch

Untitled

May 23rd, 2013
505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.62 KB | None | 0 0
  1. namespace App;
  2.  
  3. use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
  4. use Symfony\Component\Security\Core\User\UserProviderInterface;
  5. use Symfony\Component\Security\Core\Exception\AuthenticationServiceException;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7.  
  8. class LdapAuthenticationProvider implements AuthenticationProviderInterface
  9. {
  10.     private $userProvider;
  11.     private $providerKey;
  12.  
  13.     public function __construct(UserProviderInterface $userProvider, $providerKey)
  14.     {
  15.         $this->userProvider = $userProvider;
  16.         $this->providerKey = $providerKey;
  17.     }
  18.  
  19.     /**
  20.      * {@inheritdoc}
  21.      */
  22.     protected function retrieveUser($username, LdapUserToken $token)
  23.     {
  24.         $user = $token->getUser();
  25.         if ($user instanceof UserInterface) {
  26.             return $user;
  27.         }
  28.  
  29.         try {
  30.             $user = $this->userProvider->loadUserByUsernameAndPassword(
  31.                 $user,
  32.                 $token->getCredentials()
  33.             );
  34.  
  35.             if (!$user instanceof UserInterface) {
  36.                 throw new AuthenticationServiceException('The user provider must return a UserInterface object.');
  37.             }
  38.  
  39.             return $user;
  40.         } catch (UsernameNotFoundException $notFound) {
  41.             throw $notFound;
  42.         }
  43.     }
  44.  
  45.     public function supports(TokenInterface $token)
  46.     {
  47.         return $token instanceof LdapUserToken;
  48.     }
  49.  
  50.     public function authenticate(TokenInterface $token)
  51.     {
  52.  
  53.         if (!$this->supports($token)) {
  54.             return null;
  55.         }
  56.  
  57.         $username = $token->getUsername();
  58.         if (empty($username)) {
  59.             $username = 'NONE_PROVIDED';
  60.         }
  61.  
  62.         try {
  63.             $user = $this->retrieveUser($username, $token);
  64.         } catch (UsernameNotFoundException $notFound) {
  65.             throw new BadCredentialsException('Bad credentials', 0, $notFound);
  66.         }
  67.  
  68.         if (!$user instanceof UserInterface) {
  69.             throw new AuthenticationServiceException('retrieveUser() must return a UserInterface.');
  70.         }
  71.  
  72.         try {
  73.             $this->checkAuthentication($user, $token);
  74.         } catch (BadCredentialsException $e) {
  75.             if ($this->hideUserNotFoundExceptions) {
  76.                 throw new BadCredentialsException('Bad credentials', 0, $e);
  77.             }
  78.  
  79.             throw $e;
  80.         }
  81.  
  82.         $authenticatedToken = new LdapUserToken(
  83.             $user,
  84.             $token->getCredentials(),
  85.             $user->getRoles()
  86.         );
  87.  
  88.         return $authenticatedToken;
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement