From 5d6171c3d9b4f03eb212d1b93a1b09f06187fb5b Mon Sep 17 00:00:00 2001 From: Deon George Date: Fri, 9 Jan 2015 13:58:11 +1100 Subject: [PATCH] Enabled caching --- classes/Kohana/Database/LDAP.php | 12 +++++++----- classes/Kohana/Database/LDAP/Search.php | 16 +++++++++++----- classes/Kohana/Database/LDAP/Search/Result.php | 5 ++++- classes/Kohana/ORM/LDAP.php | 6 ++++++ 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/classes/Kohana/Database/LDAP.php b/classes/Kohana/Database/LDAP.php index be80b64..636f2f9 100644 --- a/classes/Kohana/Database/LDAP.php +++ b/classes/Kohana/Database/LDAP.php @@ -78,7 +78,7 @@ abstract class Kohana_Database_LDAP extends Kohana_LDAP { */ if (Kohana::$profiling) - $benchmark = Profiler::start("Database Bind ({$this->_instance})", $this->_instance); + $benchmark = Profiler::start("Database LDAP ({$this->_instance})",__METHOD__); try { $br = ldap_bind($this->_connection,$u,$p); @@ -115,7 +115,7 @@ abstract class Kohana_Database_LDAP extends Kohana_LDAP { // Get our Schema. if (isset($benchmark)) { Profiler::stop($benchmark); - $benchmark = Profiler::start("Schema Retrieve ({$this->_instance})", $this->_instance); + $benchmark = Profiler::start("Database LDAP ({$this->_instance})", 'Schema Retrieve'); } if (isset($benchmark)) @@ -197,7 +197,7 @@ abstract class Kohana_Database_LDAP extends Kohana_LDAP { foreach ($u as $dn => $leaf) if ($this->_bind($dn,$pass)) - return $leaf; + return ORM::factory('LDAP',$dn); // We didnt find an AUTH DN to bind with return FALSE; @@ -233,7 +233,7 @@ abstract class Kohana_Database_LDAP extends Kohana_LDAP { // Benchmark this connection for the current instance if (Kohana::$profiling) - $benchmark = Profiler::start("Database Connect ({$this->_instance})", $this->_instance); + $benchmark = Profiler::start("Database LDAP ({$this->_instance})",__METHOD__); $r = (! empty($this->_config['port'])) ? ldap_connect($hostname,$port) : ldap_connect($hostname); @@ -296,13 +296,15 @@ abstract class Kohana_Database_LDAP extends Kohana_LDAP { if ($this->_instance == 'schema' AND $this->_config['login_attr'] != 'DN') $this->_config['login_attr'] = 'DN'; - $x = LDAP::factory('schema'); + $x = LDAP::factory('schema',NULL,$this->_config); try { // @todo We should bind as specific shema DN, logged in User or anonymous. if ($x->bind((isset($this->_config['schema']['dn']) ? $this->_config['schema']['dn'] : FALSE),(isset($this->_config['schema']['password']) ? $this->_config['schema']['password'] : FALSE))) { $u = $x->search(array('')) ->scope('base') + // @todo: This should be a config item + ->cached(86400) ->execute(); if (! $u OR ! isset($u[''][0]['subschemasubentry'][0])) diff --git a/classes/Kohana/Database/LDAP/Search.php b/classes/Kohana/Database/LDAP/Search.php index 5ee6b97..5261bd4 100644 --- a/classes/Kohana/Database/LDAP/Search.php +++ b/classes/Kohana/Database/LDAP/Search.php @@ -188,14 +188,14 @@ abstract class Kohana_Database_LDAP_Search { * @uses Kohana::$cache_life */ public function cached($lifetime=NULL,$force=FALSE) { + // Use the global setting + if ($lifetime === NULL) + $lifetime = Kohana::$cache_life; + + // Global is caching enabled. if (! Kohana::$caching) $lifetime = NULL; - if ($lifetime === NULL) { - // Use the global setting - $lifetime = Kohana::$cache_life; - } - $this->_force_execute = $force; $this->_lifetime = $lifetime; @@ -255,6 +255,9 @@ abstract class Kohana_Database_LDAP_Search { } } + if (Kohana::$profiling) + $benchmark = Profiler::start("Database LDAP ({$this->_db})",$this->_scope.':'.$base.':'.$this->_filter); + // Search is not cached, OR caching is disabled, so we'll query if (! isset($result[$base])) { try { @@ -284,6 +287,9 @@ abstract class Kohana_Database_LDAP_Search { if (isset($cache_key) AND $this->_lifetime > 0) Kohana::cache($cache_key,$result[$base]->as_array(),$this->_lifetime); } + + if (isset($benchmark)) + Profiler::stop($benchmark); } return $result; diff --git a/classes/Kohana/Database/LDAP/Search/Result.php b/classes/Kohana/Database/LDAP/Search/Result.php index efd9ffe..0f22827 100644 --- a/classes/Kohana/Database/LDAP/Search/Result.php +++ b/classes/Kohana/Database/LDAP/Search/Result.php @@ -54,6 +54,9 @@ abstract class Kohana_Database_LDAP_Search_Result implements ArrayAccess,Iterato if (! $this->_rewound) $this->rewind(); + if (! is_object(current($this->result))) + throw HTTP_Exception::factory(501,'The current array is not an object'); + return current($this->result)->current(); } @@ -94,7 +97,7 @@ abstract class Kohana_Database_LDAP_Search_Result implements ArrayAccess,Iterato } public function valid() { - return is_object(current($this->result)) ? current($this->result)->valid() : FALSE; + return is_array(current($this->result)) ? TRUE : (is_object(current($this->result)) ? current($this->result)->valid() : FALSE); } } // End Database_LDAP_Search_Result ?> diff --git a/classes/Kohana/ORM/LDAP.php b/classes/Kohana/ORM/LDAP.php index 24d3972..e9acb1e 100644 --- a/classes/Kohana/ORM/LDAP.php +++ b/classes/Kohana/ORM/LDAP.php @@ -89,6 +89,12 @@ abstract class Kohana_ORM_LDAP extends ORM { } } + // During authentication, is this user activated + // @todo This should probably query a group membership, etc + public function activated() { + return TRUE; + } + public function base($value) { // Add pending database call which is executed after query type is determined $this->_db_pending[] = array(