* @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 Icinga\User; use Icinga\Authentication\UserBackend; use Icinga\Protocol\Ldap\Connection; class LdapUserBackend extends UserBackend { /** * Connection to the LDAP server * * @var Connection **/ protected $conn; protected $userClass; protected $userNameAttribute; public function __construct(Connection $conn, $userClass, $userNameAttribute) { $this->conn = $conn; $this->userClass = $userClass; $this->userNameAttribute = $userNameAttribute; } /** * Create query * * @param string $username * * @return \Icinga\Protocol\Ldap\Query **/ protected function createQuery($username) { return $this->conn->select() ->from( $this->userClass, array($this->userNameAttribute) ) ->where( $this->userNameAttribute, str_replace('*', '', $username) ); } /** * Test whether the given user exists * * @param User $user * * @return bool */ public function hasUser(User $user) { $username = $user->getUsername(); return $this->conn->fetchOne($this->createQuery($username)) === $username; } /** * Authenticate * * @param User $user * @param string $password * * @return bool */ public function authenticate(User $user, $password) { if ($this->conn->testCredentials( $this->conn->fetchDN($this->createQuery($user->getUsername())), $password ) ) { return true; } return false; } /** * Get the number of users available * * @return int */ public function count() { return $this->conn->count( $this->conn->select()->from( $this->userClass, array( $this->userNameAttribute ) ) ); } }