From 20724f62fc164135ebe57cd5033e49dcc6245e9b Mon Sep 17 00:00:00 2001 From: Deon George Date: Fri, 18 Jul 2014 12:35:27 +1000 Subject: [PATCH] Fixed LDAP caching --- classes/Kohana/Database/LDAP.php | 21 ++++++++++++------- .../Kohana/Database/LDAP/Result/Cached.php | 2 +- classes/Kohana/Database/LDAP/Search.php | 1 + .../Kohana/Database/LDAP/Search/Result.php | 10 +++++++++ 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/classes/Kohana/Database/LDAP.php b/classes/Kohana/Database/LDAP.php index c13a4ba..412a995 100644 --- a/classes/Kohana/Database/LDAP.php +++ b/classes/Kohana/Database/LDAP.php @@ -65,6 +65,8 @@ abstract class Kohana_Database_LDAP extends Kohana_LDAP { * @return boolean TRUE|FALSE */ private function _bind($u,$p) { + Log::instance()->add(LOG_NOTICE,':instance :method BINDing, Username :user, Pass :pass',array(':instance'=>$this->_instance,':method'=>__METHOD__,':user'=>$u,':pass'=>md5($p))); + /* // @todo To implement // If SASL has been configured for binding, then start it now. @@ -92,6 +94,7 @@ abstract class Kohana_Database_LDAP extends Kohana_LDAP { if (! $br) return FALSE; + Log::instance()->add(LOG_NOTICE,':instance :method BOUND, Username :user, Pass :pass',array(':instance'=>$this->_instance,':method'=>__METHOD__,':user'=>$u,':pass'=>md5($p))); $this->_connected = TRUE; /* @@ -115,11 +118,6 @@ abstract class Kohana_Database_LDAP extends Kohana_LDAP { $benchmark = Profiler::start("Schema Retrieve ({$this->_instance})", $this->_instance); } - if ($this->_instance == 'auth') { - $this->getSchema(); - - } - if (isset($benchmark)) Profiler::stop($benchmark); @@ -144,8 +142,12 @@ abstract class Kohana_Database_LDAP extends Kohana_LDAP { // Make sure we are connected. $this->_connection OR $this->connect(); + Log::instance()->add(LOG_NOTICE,':instance :method BINDing, Username :user, Pass :pass, LoginAttr :login_attr',array(':instance'=>$this->_instance,':method'=>__METHOD__,':user'=>$user,':pass'=>md5($pass),':login_attr'=>$this->_config['login_attr'])); + // Do we need to do an search to find the DN if (! empty($this->_config['login_attr']) AND strtoupper($this->_config['login_attr']) != 'DN') { + Log::instance()->add(LOG_NOTICE,':instance :method BINDing, Searching for DN',array(':instance'=>$this->_instance,':method'=>__METHOD__)); + // Do we need to authenticate for this search? // Extract the connection parameters, adding required variabels extract($this->_config['connection'] + array( @@ -171,6 +173,7 @@ abstract class Kohana_Database_LDAP extends Kohana_LDAP { )); try { + Log::instance()->add(LOG_NOTICE,':instance :method AUTH BINDing, Username :user, Pass :pass, LoginAttr :login_attr',array(':instance'=>$this->_instance,':method'=>__METHOD__,':user'=>$username,':pass'=>md5($password))); $x = LDAP::factory('auth',NULL,$config); // Our Auth Bind credentials are wrong @@ -180,7 +183,7 @@ abstract class Kohana_Database_LDAP extends Kohana_LDAP { $u = $x->search(NULL) ->scope('sub') ->where($this->_config['login_attr'],'=',$user) - ->execute(); + ->execute(NULL,'Model_LDAP'); if (! $u) return FALSE; @@ -194,7 +197,7 @@ abstract class Kohana_Database_LDAP extends Kohana_LDAP { foreach ($u as $dn => $leaf) if ($this->_bind($dn,$pass)) - return ORM::factory('LDAP',$dn); + return $leaf; // We didnt find an AUTH DN to bind with return FALSE; @@ -220,6 +223,8 @@ abstract class Kohana_Database_LDAP extends Kohana_LDAP { 'port' => '', )); + Log::instance()->add(LOG_NOTICE,':instance :method CONNECT:- host :hostname, port :port',array(':instance'=>$this->_instance,':method'=>__METHOD__,':hostname'=>$hostname,':port'=>$port)); + /* // @todo To implement if (function_exists('run_hook')) @@ -258,6 +263,8 @@ abstract class Kohana_Database_LDAP extends Kohana_LDAP { if (isset($benchmark)) Profiler::stop($benchmark); + Log::instance()->add(LOG_NOTICE,':instance :method connectED',array(':instance'=>$this->_instance,':method'=>__METHOD__)); + $this->_connection = $r; } diff --git a/classes/Kohana/Database/LDAP/Result/Cached.php b/classes/Kohana/Database/LDAP/Result/Cached.php index 4e97b8e..d4b5bd6 100644 --- a/classes/Kohana/Database/LDAP/Result/Cached.php +++ b/classes/Kohana/Database/LDAP/Result/Cached.php @@ -42,7 +42,7 @@ abstract class Kohana_Database_LDAP_Result_Cached extends Database_Result_Cached } public function key() { - return $this->_current_row; + return current($this->_result)->pk(); } public function next() { diff --git a/classes/Kohana/Database/LDAP/Search.php b/classes/Kohana/Database/LDAP/Search.php index 8639458..5ee6b97 100644 --- a/classes/Kohana/Database/LDAP/Search.php +++ b/classes/Kohana/Database/LDAP/Search.php @@ -241,6 +241,7 @@ abstract class Kohana_Database_LDAP_Search { throw HTTP_Exception::factory(501,'Cant run a search without a connection (:type,:filter)',array(':type'=>$this->_db,':filter'=>$this->_filter)); $result = new Database_LDAP_Search_Result; + foreach ($this->_base as $base) { if (($this->_lifetime !== NULL AND $this->_db->caching()) OR (Kohana::$caching AND $this->_db->caching() AND $this->cached())) { // Set the cache key based on the database instance name and SQL diff --git a/classes/Kohana/Database/LDAP/Search/Result.php b/classes/Kohana/Database/LDAP/Search/Result.php index 8ba54fc..efd9ffe 100644 --- a/classes/Kohana/Database/LDAP/Search/Result.php +++ b/classes/Kohana/Database/LDAP/Search/Result.php @@ -62,21 +62,31 @@ abstract class Kohana_Database_LDAP_Search_Result implements ArrayAccess,Iterato } public function next() { + // If our current entry has more valid entries, skip to the next one. if (current($this->result)->valid() AND current($this->result)->next()->valid()) return current($this->result); + // Skip to our next record next($this->result); + // If the next record is invalid, keep jumping to find a valid record while (current($this->result) AND ! current($this->result)->valid()) if (next($this->result) === FALSE) break; + // Rewind our current record + if (is_object($this->result)) + current($this->result)->rewind(); + return current($this->result); } public function rewind() { is_resource($this->result) ? rewind($this->result) : reset($this->result); + current($this->result)->rewind(); + + // If our current record is invalid, skip to the next one. if (! current($this->result)->valid()) $this->next(FALSE);