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-27 13:04:47 +02:00
|
|
|
use Icinga\Application\Config as IcingaConfig;
|
2013-06-10 16:03:51 +02:00
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
|
|
|
* User authentication backend (@see Icinga\Authentication\UserBackend) for
|
|
|
|
* authentication of users via LDAP. The attributes and location of the
|
|
|
|
* user is configurable via the application.ini
|
|
|
|
*
|
|
|
|
* See the UserBackend class (@see Icinga\Authentication\UserBackend) for
|
|
|
|
* usage information
|
|
|
|
**/
|
2013-06-10 16:03:51 +02:00
|
|
|
class LdapUserBackend implements UserBackend
|
|
|
|
{
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
|
|
|
* @var Ldap\Connection
|
|
|
|
**/
|
2013-06-10 16:03:51 +02:00
|
|
|
protected $connection;
|
2013-06-27 15:18:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new Authentication backend using the
|
|
|
|
* connection information provided in $config
|
|
|
|
*
|
|
|
|
* @param object $config The ldap connection information
|
|
|
|
**/
|
2013-06-10 16:03:51 +02:00
|
|
|
public function __construct($config)
|
|
|
|
{
|
|
|
|
$this->connection = new Ldap\Connection($config);
|
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
|
|
|
* @see Icinga\Authentication\UserBackend::hasUsername
|
|
|
|
**/
|
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
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
|
|
|
* Removes the '*' characted from $string
|
|
|
|
*
|
|
|
|
* @param String $string
|
|
|
|
*
|
|
|
|
* @return String
|
|
|
|
**/
|
2013-06-10 16:03:51 +02:00
|
|
|
protected function stripAsterisks($string)
|
|
|
|
{
|
|
|
|
return str_replace('*', '', $string);
|
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
|
|
|
* Tries to fetch the username given in $username from
|
|
|
|
* the ldap connection, using the configuration parameters
|
|
|
|
* given in the Authentication configuration
|
|
|
|
*
|
|
|
|
* @param String $username The username to select
|
|
|
|
*
|
|
|
|
* @return object $result
|
|
|
|
**/
|
2013-06-10 16:03:51 +02:00
|
|
|
protected function selectUsername($username)
|
|
|
|
{
|
|
|
|
return $this->connection->select()
|
2013-06-27 13:04:47 +02:00
|
|
|
->from(IcingaConfig::getInstance()->authentication->users->user_class,
|
|
|
|
array(IcingaConfig::getInstance()->authentication->users->user_name_attribute))
|
|
|
|
->where(IcingaConfig::getInstance()->authentication->users->user_name_attribute,
|
|
|
|
$this->stripAsterisks($username));
|
2013-06-10 16:03:51 +02:00
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
|
|
|
* @see Icinga\Authentication\UserBackend::authenticate
|
|
|
|
**/
|
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;
|
|
|
|
}
|
|
|
|
}
|