Make possible reusage of LDAP object by other plugins

This commit is contained in:
Aleksander Machniak 2012-12-04 14:43:36 +01:00
parent a30344314c
commit 82d9fc6fed
2 changed files with 34 additions and 22 deletions

View file

@ -12,7 +12,7 @@
* @version @package_version@ * @version @package_version@
* @author Aleksander Machniak <machniak@kolabsys.com> * @author Aleksander Machniak <machniak@kolabsys.com>
* *
* Copyright (C) 2011, Kolab Systems AG <contact@kolabsys.com> * Copyright (C) 2011-2012, Kolab Systems AG <contact@kolabsys.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as * it under the terms of the GNU Affero General Public License as
@ -30,7 +30,7 @@
class kolab_auth extends rcube_plugin class kolab_auth extends rcube_plugin
{ {
private $ldap; static $ldap;
private $data = array(); private $data = array();
public function init() public function init()
@ -256,9 +256,8 @@ class kolab_auth extends rcube_plugin
*/ */
public function authenticate($args) public function authenticate($args)
{ {
$this->load_config(); $ldap = self::ldap();
if (!$ldap || !$ldap->ready) {
if (!$this->init_ldap()) {
$args['abort'] = true; $args['abort'] = true;
return $args; return $args;
} }
@ -298,8 +297,8 @@ class kolab_auth extends rcube_plugin
// Login As... // Login As...
if (!empty($loginas) && $admin_login) { if (!empty($loginas) && $admin_login) {
// Authenticate to LDAP // Authenticate to LDAP
$dn = $this->ldap->dn_decode($record['ID']); $dn = rcube_ldap::dn_decode($record['ID']);
$result = $this->ldap->bind($dn, $pass); $result = $ldap->bind($dn, $pass);
if (!$result) { if (!$result) {
$args['abort'] = true; $args['abort'] = true;
@ -325,9 +324,9 @@ class kolab_auth extends rcube_plugin
// check group // check group
if (!$isadmin && !empty($group)) { if (!$isadmin && !empty($group)) {
$groups = $this->ldap->get_record_groups($record['ID']); $groups = $ldap->get_record_groups($record['ID']);
foreach ($groups as $g) { foreach ($groups as $g) {
if ($group == $this->ldap->dn_decode($g)) { if ($group == rcube_ldap::dn_decode($g)) {
$isadmin = true; $isadmin = true;
break; break;
} }
@ -363,8 +362,9 @@ class kolab_auth extends rcube_plugin
$_SESSION['kolab_auth_password'] = $rcmail->encrypt($admin_pass); $_SESSION['kolab_auth_password'] = $rcmail->encrypt($admin_pass);
} }
// Store UID in session for use by other plugins // Store UID and DN of logged user in session for use by other plugins
$_SESSION['kolab_uid'] = is_array($record['uid']) ? $record['uid'][0] : $record['uid']; $_SESSION['kolab_uid'] = is_array($record['uid']) ? $record['uid'][0] : $record['uid'];
$_SESSION['kolab_dn'] = $record['ID']; // encoded
// Set user login // Set user login
if ($login_attr) { if ($login_attr) {
@ -436,14 +436,24 @@ class kolab_auth extends rcube_plugin
/** /**
* Initializes LDAP object and connects to LDAP server * Initializes LDAP object and connects to LDAP server
*/ */
private function init_ldap() public static function ldap()
{ {
if ($this->ldap) { if (self::$ldap) {
return $this->ldap->ready; return self::$ldap;
} }
$rcmail = rcube::get_instance(); $rcmail = rcube::get_instance();
// $this->load_config();
// we're in static method, load config manually
$fpath = $rcmail->plugins->dir . '/kolab_auth/config.inc.php';
if (is_file($fpath) && !$rcmail->config->load_from_file($fpath)) {
rcube::raise_error(array(
'code' => 527, 'type' => 'php',
'file' => __FILE__, 'line' => __LINE__,
'message' => "Failed to load config from $fpath"), true, false);
}
$addressbook = $rcmail->config->get('kolab_auth_addressbook'); $addressbook = $rcmail->config->get('kolab_auth_addressbook');
if (!is_array($addressbook)) { if (!is_array($addressbook)) {
@ -452,16 +462,18 @@ class kolab_auth extends rcube_plugin
} }
if (empty($addressbook)) { if (empty($addressbook)) {
return false; return null;
} }
$this->ldap = new kolab_auth_ldap_backend( self::$ldap = new kolab_auth_ldap_backend(
$addressbook, $addressbook,
$rcmail->config->get('ldap_debug'), $rcmail->config->get('ldap_debug'),
$rcmail->config->mail_domain($_SESSION['imap_host']) $rcmail->config->mail_domain($_SESSION['imap_host'])
); );
return $this->ldap->ready; $rcmail->add_shutdown_function(array(self::$ldap, 'close'));
return self::$ldap;
} }
/** /**
@ -471,15 +483,15 @@ class kolab_auth extends rcube_plugin
{ {
$rcmail = rcube::get_instance(); $rcmail = rcube::get_instance();
$filter = $rcmail->config->get('kolab_auth_filter'); $filter = $rcmail->config->get('kolab_auth_filter');
$filter = $this->parse_vars($filter, $user, $host); $filter = $this->parse_vars($filter, $user, $host);
$ldap = self::ldap();
// reset old result // reset old result
$this->ldap->reset(); $ldap->reset();
// get record // get record
$this->ldap->set_filter($filter); $ldap->set_filter($filter);
$results = $this->ldap->list_records(); $results = $ldap->list_records();
if (count($results->records) == 1) { if (count($results->records) == 1) {
return $results->records[0]; return $results->records[0];

View file

@ -18,9 +18,9 @@
<email>machniak@kolabsys.com</email> <email>machniak@kolabsys.com</email>
<active>yes</active> <active>yes</active>
</lead> </lead>
<date>2012-10-08</date> <date>2012-12-04</date>
<version> <version>
<release>0.4</release> <release>0.5</release>
<api>0.1</api> <api>0.1</api>
</version> </version>
<stability> <stability>