* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 * @author Icinga Development Team */ // {{{ICINGA_LICENSE_HEADER}}} namespace Icinga\Authentication\Backend; use \stdClass; use \Zend_Config; use \Icinga\User; use \Icinga\Authentication\UserBackend; use \Icinga\Authentication\Credential; use \Icinga\Protocol\Ldap; use \Icinga\Protocol\Ldap\Connection; use \Icinga\Application\Config as IcingaConfig; /** * User authentication backend */ class LdapUserBackend implements UserBackend { /** * Ldap resource * * @var Connection **/ protected $connection; /** * The ldap connection information * * @var Zend_Config */ private $config; /** * Name of the backend * * @var string */ private $name; /** * Create new Ldap User backend * * @param Zend_Config $config Configuration to create instance */ public function __construct(Zend_Config $config) { $this->connection = new Ldap\Connection($config); $this->config = $config; $this->name = $config->name; } /** * Name of the backend * * @return string */ public function getName() { return $this->name; } /** * Test if the username exists * * @param Credential $credential Credential to find user in database * * @return bool */ public function hasUsername(Credential $credential) { return $this->connection->fetchOne( $this->selectUsername($credential->getUsername()) ) === $credential->getUsername(); } /** * Removes the '*' character from $string * * @param string $string Input string * * @return string **/ protected function stripAsterisks($string) { return str_replace('*', '', $string); } /** * Tries to fetch the username * * @param string $username The username to select * * @return stdClass $result **/ protected function selectUsername($username) { return $this->connection->select() ->from( $this->config->user_class, array( $this->config->user_name_attribute ) ) ->where( $this->config->user_name_attribute, $this->stripAsterisks($username) ); } /** * Authenticate * * @param Credential $credentials Credential to authenticate * * @return User */ public function authenticate(Credential $credentials) { if (!$this->connection->testCredentials( $this->connection->fetchDN($this->selectUsername($credentials->getUsername())), $credentials->getPassword() )) { return false; } $user = new User($credentials->getUsername()); return $user; } public function getUserCount() { return $this->connection->count( $this->connection->select()->from( $this->config->user_class, array( $this->config->user_name_attribute ) ) ); } }