Enabled caching

This commit is contained in:
Deon George 2015-01-09 13:58:11 +11:00
parent a8135d939e
commit 5d6171c3d9
4 changed files with 28 additions and 11 deletions

View File

@ -78,7 +78,7 @@ abstract class Kohana_Database_LDAP extends Kohana_LDAP {
*/ */
if (Kohana::$profiling) if (Kohana::$profiling)
$benchmark = Profiler::start("Database Bind ({$this->_instance})", $this->_instance); $benchmark = Profiler::start("Database LDAP ({$this->_instance})",__METHOD__);
try { try {
$br = ldap_bind($this->_connection,$u,$p); $br = ldap_bind($this->_connection,$u,$p);
@ -115,7 +115,7 @@ abstract class Kohana_Database_LDAP extends Kohana_LDAP {
// Get our Schema. // Get our Schema.
if (isset($benchmark)) { if (isset($benchmark)) {
Profiler::stop($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)) if (isset($benchmark))
@ -197,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 $leaf; return ORM::factory('LDAP',$dn);
// We didnt find an AUTH DN to bind with // We didnt find an AUTH DN to bind with
return FALSE; return FALSE;
@ -233,7 +233,7 @@ abstract class Kohana_Database_LDAP extends Kohana_LDAP {
// Benchmark this connection for the current instance // Benchmark this connection for the current instance
if (Kohana::$profiling) 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); $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') if ($this->_instance == 'schema' AND $this->_config['login_attr'] != 'DN')
$this->_config['login_attr'] = 'DN'; $this->_config['login_attr'] = 'DN';
$x = LDAP::factory('schema'); $x = LDAP::factory('schema',NULL,$this->_config);
try { try {
// @todo We should bind as specific shema DN, logged in User or anonymous. // @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))) { 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('')) $u = $x->search(array(''))
->scope('base') ->scope('base')
// @todo: This should be a config item
->cached(86400)
->execute(); ->execute();
if (! $u OR ! isset($u[''][0]['subschemasubentry'][0])) if (! $u OR ! isset($u[''][0]['subschemasubentry'][0]))

View File

@ -188,14 +188,14 @@ abstract class Kohana_Database_LDAP_Search {
* @uses Kohana::$cache_life * @uses Kohana::$cache_life
*/ */
public function cached($lifetime=NULL,$force=FALSE) { 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) if (! Kohana::$caching)
$lifetime = NULL; $lifetime = NULL;
if ($lifetime === NULL) {
// Use the global setting
$lifetime = Kohana::$cache_life;
}
$this->_force_execute = $force; $this->_force_execute = $force;
$this->_lifetime = $lifetime; $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 // Search is not cached, OR caching is disabled, so we'll query
if (! isset($result[$base])) { if (! isset($result[$base])) {
try { try {
@ -284,6 +287,9 @@ abstract class Kohana_Database_LDAP_Search {
if (isset($cache_key) AND $this->_lifetime > 0) if (isset($cache_key) AND $this->_lifetime > 0)
Kohana::cache($cache_key,$result[$base]->as_array(),$this->_lifetime); Kohana::cache($cache_key,$result[$base]->as_array(),$this->_lifetime);
} }
if (isset($benchmark))
Profiler::stop($benchmark);
} }
return $result; return $result;

View File

@ -54,6 +54,9 @@ abstract class Kohana_Database_LDAP_Search_Result implements ArrayAccess,Iterato
if (! $this->_rewound) if (! $this->_rewound)
$this->rewind(); $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(); return current($this->result)->current();
} }
@ -94,7 +97,7 @@ abstract class Kohana_Database_LDAP_Search_Result implements ArrayAccess,Iterato
} }
public function valid() { 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 } // End Database_LDAP_Search_Result
?> ?>

View File

@ -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) { public function base($value) {
// Add pending database call which is executed after query type is determined // Add pending database call which is executed after query type is determined
$this->_db_pending[] = array( $this->_db_pending[] = array(