Fixed LDAP caching

This commit is contained in:
Deon George 2014-07-18 12:35:27 +10:00
parent 7f2f037406
commit 20724f62fc
4 changed files with 26 additions and 8 deletions

View File

@ -65,6 +65,8 @@ abstract class Kohana_Database_LDAP extends Kohana_LDAP {
* @return boolean TRUE|FALSE * @return boolean TRUE|FALSE
*/ */
private function _bind($u,$p) { 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 // @todo To implement
// If SASL has been configured for binding, then start it now. // 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) if (! $br)
return FALSE; 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; $this->_connected = TRUE;
/* /*
@ -115,11 +118,6 @@ abstract class Kohana_Database_LDAP extends Kohana_LDAP {
$benchmark = Profiler::start("Schema Retrieve ({$this->_instance})", $this->_instance); $benchmark = Profiler::start("Schema Retrieve ({$this->_instance})", $this->_instance);
} }
if ($this->_instance == 'auth') {
$this->getSchema();
}
if (isset($benchmark)) if (isset($benchmark))
Profiler::stop($benchmark); Profiler::stop($benchmark);
@ -144,8 +142,12 @@ abstract class Kohana_Database_LDAP extends Kohana_LDAP {
// Make sure we are connected. // Make sure we are connected.
$this->_connection OR $this->connect(); $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 // Do we need to do an search to find the DN
if (! empty($this->_config['login_attr']) AND strtoupper($this->_config['login_attr']) != '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? // Do we need to authenticate for this search?
// Extract the connection parameters, adding required variabels // Extract the connection parameters, adding required variabels
extract($this->_config['connection'] + array( extract($this->_config['connection'] + array(
@ -171,6 +173,7 @@ abstract class Kohana_Database_LDAP extends Kohana_LDAP {
)); ));
try { 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); $x = LDAP::factory('auth',NULL,$config);
// Our Auth Bind credentials are wrong // Our Auth Bind credentials are wrong
@ -180,7 +183,7 @@ abstract class Kohana_Database_LDAP extends Kohana_LDAP {
$u = $x->search(NULL) $u = $x->search(NULL)
->scope('sub') ->scope('sub')
->where($this->_config['login_attr'],'=',$user) ->where($this->_config['login_attr'],'=',$user)
->execute(); ->execute(NULL,'Model_LDAP');
if (! $u) if (! $u)
return FALSE; return FALSE;
@ -194,7 +197,7 @@ abstract class Kohana_Database_LDAP extends Kohana_LDAP {
foreach ($u as $dn => $leaf) foreach ($u as $dn => $leaf)
if ($this->_bind($dn,$pass)) if ($this->_bind($dn,$pass))
return ORM::factory('LDAP',$dn); return $leaf;
// We didnt find an AUTH DN to bind with // We didnt find an AUTH DN to bind with
return FALSE; return FALSE;
@ -220,6 +223,8 @@ abstract class Kohana_Database_LDAP extends Kohana_LDAP {
'port' => '', '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 // @todo To implement
if (function_exists('run_hook')) if (function_exists('run_hook'))
@ -258,6 +263,8 @@ abstract class Kohana_Database_LDAP extends Kohana_LDAP {
if (isset($benchmark)) if (isset($benchmark))
Profiler::stop($benchmark); Profiler::stop($benchmark);
Log::instance()->add(LOG_NOTICE,':instance :method connectED',array(':instance'=>$this->_instance,':method'=>__METHOD__));
$this->_connection = $r; $this->_connection = $r;
} }

View File

@ -42,7 +42,7 @@ abstract class Kohana_Database_LDAP_Result_Cached extends Database_Result_Cached
} }
public function key() { public function key() {
return $this->_current_row; return current($this->_result)->pk();
} }
public function next() { public function next() {

View File

@ -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)); 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; $result = new Database_LDAP_Search_Result;
foreach ($this->_base as $base) { foreach ($this->_base as $base) {
if (($this->_lifetime !== NULL AND $this->_db->caching()) OR (Kohana::$caching AND $this->_db->caching() AND $this->cached())) { 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 // Set the cache key based on the database instance name and SQL

View File

@ -62,21 +62,31 @@ abstract class Kohana_Database_LDAP_Search_Result implements ArrayAccess,Iterato
} }
public function next() { 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()) if (current($this->result)->valid() AND current($this->result)->next()->valid())
return current($this->result); return current($this->result);
// Skip to our next record
next($this->result); 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()) while (current($this->result) AND ! current($this->result)->valid())
if (next($this->result) === FALSE) if (next($this->result) === FALSE)
break; break;
// Rewind our current record
if (is_object($this->result))
current($this->result)->rewind();
return current($this->result); return current($this->result);
} }
public function rewind() { public function rewind() {
is_resource($this->result) ? rewind($this->result) : reset($this->result); 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()) if (! current($this->result)->valid())
$this->next(FALSE); $this->next(FALSE);