2013-06-10 16:03:51 +02:00
|
|
|
<?php
|
2013-06-10 17:03:01 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-06-10 16:03:51 +02:00
|
|
|
|
|
|
|
namespace Icinga\Authentication\Backend;
|
|
|
|
|
|
|
|
use Icinga\Authentication\User as User;
|
2013-06-25 12:24:52 +02:00
|
|
|
use Icinga\Authentication\UserBackend;
|
|
|
|
use Icinga\Authentication\Credentials;
|
2013-06-10 16:03:51 +02:00
|
|
|
use Icinga\Protocol\Ldap;
|
2013-06-26 16:48:07 +02:00
|
|
|
use Icinga\Application\Config;
|
2013-06-10 16:03:51 +02:00
|
|
|
|
|
|
|
class LdapUserBackend implements UserBackend
|
|
|
|
{
|
|
|
|
protected $connection;
|
|
|
|
|
|
|
|
public function __construct($config)
|
|
|
|
{
|
|
|
|
$this->connection = new Ldap\Connection($config);
|
|
|
|
}
|
|
|
|
|
2013-06-25 12:24:52 +02:00
|
|
|
public function hasUsername(Credentials $credential)
|
2013-06-10 16:03:51 +02:00
|
|
|
{
|
|
|
|
return $this->connection->fetchOne(
|
2013-06-25 12:24:52 +02:00
|
|
|
$this->selectUsername($credential->getUsername())
|
|
|
|
) === $credential->getUsername();
|
2013-06-10 16:03:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function stripAsterisks($string)
|
|
|
|
{
|
|
|
|
return str_replace('*', '', $string);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function selectUsername($username)
|
|
|
|
{
|
|
|
|
return $this->connection->select()
|
2013-06-26 16:48:07 +02:00
|
|
|
->from(
|
|
|
|
Config::getInstance()->authentication->users->user_class,
|
|
|
|
array(
|
|
|
|
Config::getInstance()->authentication->users->user_name_attribute
|
|
|
|
)
|
|
|
|
)
|
|
|
|
->where(
|
|
|
|
Config::getInstance()->authentication->users->user_name_attribute,
|
|
|
|
$this->stripAsterisks($username)
|
|
|
|
);
|
2013-06-10 16:03:51 +02:00
|
|
|
}
|
|
|
|
|
2013-06-25 12:24:52 +02:00
|
|
|
public function authenticate(Credentials $credentials)
|
2013-06-10 16:03:51 +02:00
|
|
|
{
|
2013-06-11 13:53:42 +02:00
|
|
|
if (!$this->connection->testCredentials(
|
2013-06-25 12:24:52 +02:00
|
|
|
$this->connection->fetchDN($this->selectUsername($credentials->getUsername())),
|
|
|
|
$credentials->getPassword()
|
2013-06-26 16:48:07 +02:00
|
|
|
)
|
|
|
|
) {
|
2013-06-10 16:03:51 +02:00
|
|
|
return false;
|
|
|
|
}
|
2013-06-25 12:24:52 +02:00
|
|
|
$user = new User($credentials->getUsername());
|
2013-06-11 13:53:42 +02:00
|
|
|
|
2013-06-10 16:03:51 +02:00
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
}
|