summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Bruederli <bruederli@kolabsys.com>2013-01-16 16:53:46 (GMT)
committerThomas Bruederli <bruederli@kolabsys.com>2013-01-16 16:53:46 (GMT)
commit9fbb32d9b7f5c289878b8876400debfb4f9ac350 (patch)
tree4c0f9566125d95aed1e52beccb6c0014ec712077
parent212bb3330b9a6b034771b2449b0d4ed2dcceaa8a (diff)
downloadkolab-freebusy-9fbb32d9b7f5c289878b8876400debfb4f9ac350.tar.gz
Implement LDAP lookup routine; improve ldap logging
-rw-r--r--lib/Kolab/FreeBusy/DirectoryLDAP.php75
1 files changed, 67 insertions, 8 deletions
diff --git a/lib/Kolab/FreeBusy/DirectoryLDAP.php b/lib/Kolab/FreeBusy/DirectoryLDAP.php
index ae80bfc..1c675f9 100644
--- a/lib/Kolab/FreeBusy/DirectoryLDAP.php
+++ b/lib/Kolab/FreeBusy/DirectoryLDAP.php
@@ -4,6 +4,7 @@ namespace Kolab\FreeBusy;
// PEAR modules operate in global namespace
use \Net_LDAP3;
+use \Monolog\Logger as Monolog;
/**
*
@@ -12,30 +13,64 @@ class DirectoryLDAP extends Directory
{
private $ldap;
private $logger;
+ private $ready = false;
/**
* Default constructor loading directory configuration
*/
public function __construct($config)
{
+ $this->config = $config;
+
+ $host = parse_url($config['host']);
$ldap_config = array(
-
- );
+ 'hosts' => array($host['host']),
+ 'port' => $host['port'] ?: 389,
+ 'use_tls' => $host['scheme'] == 'tls' || $host['scheme'] == 'ldaps',
+ 'root_dn' => $config['base_dn'],
+ 'return_attributes' => (array)$config['attributes'],
+ 'sizelimit' => 0,
+ 'timelimit' => 0,
+ ) + $config;
- $this->logger = Logger::get('ldap');
+ // instantiate Net_LDAP3 and connect with logger
+ $this->logger = Logger::get('ldap', $config['loglevel']);
$this->ldap = new Net_LDAP3($ldap_config);
$this->ldap->config_set('log_hook', array($this, 'log'));
- $this->ldap->connect();
+ $this->ldap->config_set('return_attributes', (array)$config['attributes']);
+
+ // connect + bind to LDAP server
+ if ($this->ldap->connect()) {
+ $this->ready = $this->ldap->bind($config['bind_dn'], $config['bind_pw']);
+ }
+
+ if ($this->ready) {
+ $this->logger->addInfo("Connected to $config[host] with '$config[bind_dn]'");
+ }
+ else {
+ $this->logger->addWarning("Connectiion to $config[host] with '$config[bind_dn]' failed!");
+ }
}
/**
* Callback for Net_LDAP3 logging
*/
- public function log($log)
+ public function log($level, $msg)
{
- // TODO: map $log[0] levels
- $msg = is_array($log[1]) ? join('; ', $log[1]) : strval($log[1]);
- $this->logger->addRecord($log[0], $msg);
+ // map PHP log levels to Monolog levels
+ static $loglevels = array(
+ LOG_DEBUG => Monolog::DEBUG,
+ LOG_NOTICE => Monolog::NOTICE,
+ LOG_INFO => Monolog::INFO,
+ LOG_WARNING => Monolog::WARNING,
+ LOG_ERR => Monolog::ERROR,
+ LOG_CRIT => Monolog::CRITICAL,
+ LOG_ALERT => Monolog::ALERT,
+ LOG_EMERG => Monolog::EMERGENCY,
+ );
+
+ $msg = is_array($msg) ? join('; ', $msg) : strval($msg);
+ $this->logger->addRecord($loglevels[$level], $msg);
}
/**
@@ -45,6 +80,30 @@ class DirectoryLDAP extends Directory
{
$result = array('u' => $user);
+ if ($this->ready) {
+ // search with configured filter
+ $filter = preg_replace('/%u/i', $user, $this->config['filter']);
+ $ldapresult = $this->ldap->search($this->config['base_dn'], $filter, 'sub');
+
+ // got a valid result
+ if ($ldapresult && $ldapresult->count()) {
+ $ldapresult->rewind();
+ $entry = Net_LDAP3::normalize_entry($ldapresult->current()); // get the first entry
+ $this->logger->addInfo("Found " . $ldapresult->count() . " entries for $filter", $entry);
+
+ // convert entry attributes to strings and add them to the final result hash array
+ foreach ($entry as $k => $v) {
+ if (!empty($v)) {
+ $result[$k] = strval(is_array($v) ? $v[0] : $v);
+ }
+ }
+
+ return $result;
+ }
+
+ $this->logger->addInfo("No entry found for $filter");
+ }
+
return false;
}