From 376dedbcab29c9d9485d6c24e92af8b5457dc849 Mon Sep 17 00:00:00 2001 From: Deon George Date: Tue, 11 Feb 2014 16:53:37 +1100 Subject: [PATCH] Added schema retrieval --- classes/Kohana/Database/LDAP.php | 32 ++++++++++++++++++++++++++++++++ classes/Kohana/LDAP.php | 13 +++++++++++++ classes/Model/LDAP/Schema.php | 14 ++++++++++++++ config/database.php | 4 ++++ tests/classes/LDAPSchema.php | 24 ++++++++++++++++++++++++ 5 files changed, 87 insertions(+) create mode 100644 classes/Model/LDAP/Schema.php create mode 100644 tests/classes/LDAPSchema.php diff --git a/classes/Kohana/Database/LDAP.php b/classes/Kohana/Database/LDAP.php index 4e155da..748f3b8 100644 --- a/classes/Kohana/Database/LDAP.php +++ b/classes/Kohana/Database/LDAP.php @@ -109,6 +109,38 @@ abstract class Kohana_Database_LDAP extends Kohana_LDAP { run_hook('post_connect',array('server_id'=>$this->index,'method'=>$method,'id'=>$bind['id'])); */ + // Get our Schema. + if (isset($benchmark)) { + Profiler::stop($benchmark); + $benchmark = Profiler::start("Schema Retrieve ({$this->_instance})", $this->_instance); + } + + if ($this->_instance == 'auth') { + $x = LDAP::factory('schema',NULL,$this->_config); + + try { + // Our Auth Bind credentials are wrong + if ($x->bind((isset($this->_config['schema']['dn']) ? $this->_config['schema']['dn'] : 'fred'),(isset($this->_config['schema']['password']) ? $this->_config['schema']['password'] : 'fred'))) { + + $u = $x->search(array('')) + ->scope('base') + ->where('objectclass','=','*') + ->execute(); + + if (! $u OR ! isset($u[''][0]['subschemasubentry'][0])) + throw new Kohana_Exception('Couldnt find schema?'); + + $x->setSchema(ORM::factory('LDAP_Schema',$u[''][0]['subschemasubentry'][0])); + } + + } catch (Exception $e) { + // If we are a command line, we can just print the error + echo _('Unable to retrieve the SCHEMA from the LDAP server.'); + echo _('The error message is').': '.$e->getMessage(); + die(); + } + } + if (isset($benchmark)) Profiler::stop($benchmark); diff --git a/classes/Kohana/LDAP.php b/classes/Kohana/LDAP.php index fb244f6..48478fb 100644 --- a/classes/Kohana/LDAP.php +++ b/classes/Kohana/LDAP.php @@ -16,6 +16,11 @@ abstract class Kohana_LDAP extends Database { */ protected $_connected = FALSE; + /** + * @var Our LDAP OBJECT that holds the schema + */ + protected $_schema = NULL; + /** * @var string Our default usage when connection */ @@ -96,5 +101,13 @@ abstract class Kohana_LDAP extends Database { public static function instance($name=NULL,array $config=NULL) { throw Kohana_Exception('Sorry, you cant use instance(), you need to use factory()'); } + + public function setSchema(Model_LDAP_Schema $orm) { + $this->_schema = $orm; + } + + public function schema() { + return $this->_schema; + } } ?> diff --git a/classes/Model/LDAP/Schema.php b/classes/Model/LDAP/Schema.php new file mode 100644 index 0000000..e36cbc6 --- /dev/null +++ b/classes/Model/LDAP/Schema.php @@ -0,0 +1,14 @@ + diff --git a/config/database.php b/config/database.php index aa26334..a2fda13 100644 --- a/config/database.php +++ b/config/database.php @@ -38,6 +38,10 @@ return array ( 'profiling' => TRUE, 'login_attr'=>'uid', + 'schema'=>array( + 'dn' => FALSE, + 'password' => FALSE, + ), ), ); ?> diff --git a/tests/classes/LDAPSchema.php b/tests/classes/LDAPSchema.php new file mode 100644 index 0000000..b68fb87 --- /dev/null +++ b/tests/classes/LDAPSchema.php @@ -0,0 +1,24 @@ +bind('bart','eatmyshorts'); + + $this->assertTrue(LDAP::factory('schema')->schema() instanceof Model_LDAP_Schema); + } +}