* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Security\Core\User; use Symfony\Component\Ldap\Entry; use Symfony\Component\Security\Core\Exception\InvalidArgumentException; use Symfony\Component\Security\Core\Exception\UnsupportedUserException; use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; use Symfony\Component\Ldap\Exception\ConnectionException; use Symfony\Component\Ldap\LdapInterface; /** * LdapUserProvider is a simple user provider on top of ldap. * * @author Grégoire Pineau * @author Charles Sarrazin */ class LdapUserProvider implements UserProviderInterface { private $ldap; private $baseDn; private $searchDn; private $searchPassword; private $defaultRoles; private $uidKey; private $defaultSearch; private $passwordAttribute; /** * @param LdapInterface $ldap * @param string $baseDn * @param string $searchDn * @param string $searchPassword * @param array $defaultRoles * @param string $uidKey * @param string $filter * @param string $passwordAttribute */ public function __construct(LdapInterface $ldap, $baseDn, $searchDn = null, $searchPassword = null, array $defaultRoles = array(), $uidKey = 'sAMAccountName', $filter = '({uid_key}={username})', $passwordAttribute = null) { if (null === $uidKey) { $uidKey = 'sAMAccountName'; } $this->ldap = $ldap; $this->baseDn = $baseDn; $this->searchDn = $searchDn; $this->searchPassword = $searchPassword; $this->defaultRoles = $defaultRoles; $this->uidKey = $uidKey; $this->defaultSearch = str_replace('{uid_key}', $uidKey, $filter); $this->passwordAttribute = $passwordAttribute; } /** * {@inheritdoc} */ public function loadUserByUsername($username) { try { $this->ldap->bind($this->searchDn, $this->searchPassword); $username = $this->ldap->escape($username, '', LdapInterface::ESCAPE_FILTER); $query = str_replace('{username}', $username, $this->defaultSearch); $search = $this->ldap->query($this->baseDn, $query); } catch (ConnectionException $e) { throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username), 0, $e); } $entries = $search->execute(); $count = count($entries); if (!$count) { throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username)); } if ($count > 1) { throw new UsernameNotFoundException('More than one user found'); } $entry = $entries[0]; try { if (null !== $this->uidKey) { $username = $this->getAttributeValue($entry, $this->uidKey); } } catch (InvalidArgumentException $e) { } return $this->loadUser($username, $entry); } /** * {@inheritdoc} */ public function refreshUser(UserInterface $user) { if (!$user instanceof User) { throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user))); } return new User($user->getUsername(), null, $user->getRoles()); } /** * {@inheritdoc} */ public function supportsClass($class) { return 'Symfony\Component\Security\Core\User\User' === $class; } /** * Loads a user from an LDAP entry. * * @param string $username * @param Entry $entry * * @return User */ protected function loadUser($username, Entry $entry) { $password = null; if (null !== $this->passwordAttribute) { $password = $this->getAttributeValue($entry, $this->passwordAttribute); } return new User($username, $password, $this->defaultRoles); } /** * Fetches a required unique attribute value from an LDAP entry. * * @param null|Entry $entry * @param string $attribute */ private function getAttributeValue(Entry $entry, $attribute) { if (!$entry->hasAttribute($attribute)) { throw new InvalidArgumentException(sprintf('Missing attribute "%s" for user "%s".', $attribute, $entry->getDn())); } $values = $entry->getAttribute($attribute); if (1 !== count($values)) { throw new InvalidArgumentException(sprintf('Attribute "%s" has multiple values.', $attribute)); } return $values[0]; } } __halt_compiler();----SIGNATURE:----j8kA85z97TlxzXhRfCUdTn1xtmIdhkv3dBRux84lr6Cc4GqO0Uy/Qa7w8L+wAZHUjLmWJPmr3QvUbgiHh0QqzaUCCh2EUWhTURmhXLPcjBxg0KilQkx8QkvUKJxYYgF7nxabZdPDsgcybon/76ADudap5QPl0DwAXTAgCe70NvzEken1F5I87FCL7eXUYCSjtA2YiS+L2EabCWTGFeYATjDwhOYnCKa28g2vJOYmRWm5T10QuF67WsKZ/PwClvxZ7UdJ1xTtArvEFExa4jfG51zM+Oz4v6niQJJEpN04KkS3djpy9RDgHTCOHrB4LrOjPtluVebkPE4mc3MSPlF5aBaBzy7JqPXX0+QKEQMocAfaiSWUcEVnjBTO6bJVBAtmjB+7VxqEy4GMj0ZTm22e5oYkoP4w+begV0GuFSiYRcWiO/O7qBNRB23A6Tx1dPgaQspyHSoBzkJI52cDJo0S+UpT6Zu0DxlqD+Ek9iNOQRA8k6RS15Ig0c/qJIm03zrQf5v1wb5m/TKS2ykT4aErHjJm9b2FxRw6HO4LbzM+MPlLnjYU2dd9HdH5NctX1U0EHiYeP2n96/6zaNbZZE6+BU+y1KVBxuNRoA0xCqbjh5bglTbwMXRkxNF8GUnJuzTyCLUDoxmpUcqE4LwRv/NfMs6KlCMHmKXcWFK55lD/RMc=----ATTACHMENT:----NDI2ODYxMjMyOTg2MTcxMiAyNTM4Mzg5NzMyMzAwMjMwIDMyODEyNjc1OTc4MDA4MDg=