summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksander Machniak <alec@alec.pl>2013-12-04 11:00:01 (GMT)
committerAleksander Machniak <alec@alec.pl>2013-12-04 11:19:54 (GMT)
commit3336c74b7b699729563f43cd72e233fa2df8a24b (patch)
treec620f19de3a2713d93d0fb3e93819953e53f465d
parent185d3fec9829aa1aa5f61255d1e0e288293ee8d4 (diff)
downloadkolab-wap-3336c74b7b699729563f43cd72e233fa2df8a24b.tar.gz
Cache domain name to domain DN map
-rw-r--r--lib/Auth/LDAP.php63
1 files changed, 54 insertions, 9 deletions
diff --git a/lib/Auth/LDAP.php b/lib/Auth/LDAP.php
index 54b8943..d6e9119 100644
--- a/lib/Auth/LDAP.php
+++ b/lib/Auth/LDAP.php
@@ -526,12 +526,11 @@ class LDAP extends Net_LDAP3 {
*/
public function effective_rights($subject)
{
- $cache = $this->get_cache();
$ckey = $_SESSION['user']->user_bind_dn . '#'
. md5($this->domain . '::' . $subject . '::' . $_SESSION['user']->user_bind_pw);
// use memcache
- if ($cache && ($result = $cache->get($ckey))) {
+ if ($result = $this->get_cache_data($ckey)) {
return $result;
}
// use internal cache
@@ -565,12 +564,7 @@ class LDAP extends Net_LDAP3 {
$result = $this->legacy_rights($subject);
}
- if ($cache) {
- if (!$cache->replace($ckey, $result, MEMCACHE_COMPRESSED, 3600)) {
- $cache->set($ckey, $result, MEMCACHE_COMPRESSED, 3600);
- }
- }
- else {
+ if (!$this->set_cache_data($ckey, $result)) {
$this->icache[$ckey] = $result;
}
@@ -1510,6 +1504,13 @@ class LDAP extends Net_LDAP3 {
{
$this->_log(LOG_DEBUG, "Auth::LDAP::_find_domain($domain)");
+ $ckey = 'domain::' . $domain;
+
+ // use memcache
+ if ($domain_dn = $this->get_cache_data($ckey)) {
+ return $this->_read($domain_dn, $attributes);
+ }
+
$domain_base_dn = $this->conf->get('ldap', 'domain_base_dn');
$domain_filter = $this->conf->get('ldap', 'domain_filter');
$domain_name_attribute = $this->conf->get('ldap', 'domain_name_attribute');
@@ -1521,7 +1522,14 @@ class LDAP extends Net_LDAP3 {
$domain_filter = "(&" . $domain_filter . "(" . $domain_name_attribute . "=" . $domain . "))";
if ($result = $this->_search($domain_base_dn, $domain_filter, $attributes)) {
- return $result->entries(true);
+ $result = $result->entries(true);
+
+ // cache domain DN
+ if (count($result) == 1) {
+ $this->set_cache_data($ckey, key($result));
+ }
+
+ return $result;
}
}
@@ -1569,6 +1577,7 @@ class LDAP extends Net_LDAP3 {
$this->memcache = false;
return false;
}
+
// add all configured hosts to pool
$pconnect = $this->conf->get('kolab_wap', 'memcache_pconnect', Conf::BOOL);
$hosts = $this->conf->get('kolab_wap', 'memcache_hosts');
@@ -1618,4 +1627,40 @@ class LDAP extends Net_LDAP3 {
}
}
+ /**
+ * Get cached data
+ *
+ * @param string $key Cache key
+ *
+ * @return mixed Cached value
+ */
+ public function get_cache_data($key)
+ {
+ if ($cache = $this->get_cache()) {
+ return $cache->get($ckey);
+ }
+ }
+
+ /**
+ * Store cached data
+ *
+ * @param string $key Cache key
+ * @param mixed $data Data
+ * @param int $ttl Cache TTL in seconds
+ *
+ * @return bool False on failure or when cache is disabled, True if data was saved succesfully
+ */
+ public function set_cache_data($key, $data, $ttl = 3600)
+ {
+ if ($cache = $this->get_cache()) {
+ if (!$cache->replace($ckey, $data, MEMCACHE_COMPRESSED, $ttl)) {
+ return $cache->set($ckey, $data, MEMCACHE_COMPRESSED, $ttl);
+ }
+ else {
+ return true;
+ }
+ }
+
+ return false;
+ }
}