diff --git a/src/Connection.php b/src/Connection.php index a28d78d..c9dd962 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -2,10 +2,8 @@ namespace Maenbn\LdapLookup; - -class Connection implements ConnectionInterface { - - +class Connection implements ConnectionInterface +{ public $config; public $connection; @@ -13,35 +11,26 @@ class Connection implements ConnectionInterface { public function __construct($config) { - $this->config = $config; - } public function connect() { - $this->connection = ldap_connect($this->config['hostname'], $this->config['port']) or die("Couldn't connect to AD!"); - if(!is_null($this->config['version'])){ - + if (!is_null($this->config['version'])) { ldap_set_option($this->connection, LDAP_OPT_PROTOCOL_VERSION, $this->config['version']); - } $bind = ldap_bind($this->connection, $this->config['bindRdn'], $this->config['bindPassword']); return $bind; - } public function close() { - ldap_close($this->connection); - } - -} \ No newline at end of file +} diff --git a/src/ConnectionInterface.php b/src/ConnectionInterface.php index d6e442c..903493b 100644 --- a/src/ConnectionInterface.php +++ b/src/ConnectionInterface.php @@ -1,9 +1,10 @@ -config = $connection->config; $this->connect(); - } public function connect() { - $this->connected = $this->connection->connect(); return $this; - } protected function search($filter) { - return ldap_search($this->connection->connection, $this->config['baseDn'], $filter); - } protected function getEntries($resultsId, $type = null) { - $info = ldap_get_entries($this->connection->connection, $resultsId); $entries = []; - for ($i = 0; $i < $info["count"]; $i ++) - { + for ($i = 0; $i < $info["count"]; $i ++) { $entry = []; - foreach ($info[$i] as $key => $value) - { - - if (is_string($key)) - { + foreach ($info[$i] as $key => $value) { + if (is_string($key)) { $entry[$key] = $value[0]; } - } $entries[] = $entry; } - switch ($type) - { + switch ($type) { case 'first': - $entries = $entries[0]; + if (isset($entries[0])) { + $entries = $entries[0]; + } break; } return $entries; - } public function getByUid($uid) { - $filter = "uid=" . $uid; $entries = $this->runSearch($filter, 'first'); return $entries; - - } public function runSearch($filter, $type = null) { - $resultsId = $this->search($filter); $entries = $this->getEntries($resultsId, $type); return $entries; - - } - -} \ No newline at end of file +} diff --git a/src/LdapLookupServiceProvider.php b/src/LdapLookupServiceProvider.php index 79477ec..5c0f8e6 100644 --- a/src/LdapLookupServiceProvider.php +++ b/src/LdapLookupServiceProvider.php @@ -1,10 +1,12 @@ -bind('ConnectionInterface', function ($app) - { + $app->bind('ConnectionInterface', function ($app) { $config = $app['config']['ldaplookup']; @@ -60,13 +61,11 @@ protected function registerConnection(Application $app) }); $app->alias('ldaplookup.connection', 'Maenbn\LdapLookup\Connection'); - } protected function registerLdapLookup(Application $app) { - $app->singleton('ldaplookup', function ($app) - { + $app->singleton('ldaplookup', function ($app) { $connection = $app['ConnectionInterface']; diff --git a/src/LookupInterface.php b/src/LookupInterface.php index dc218e1..7ef1f7f 100644 --- a/src/LookupInterface.php +++ b/src/LookupInterface.php @@ -1,9 +1,8 @@ -mockLdapLookupConfig(); $this->config = $config; @@ -17,24 +15,18 @@ protected function getEnvironmentSetUp($app) $app['config']->set('ldaplookup.baseDn', $config['baseDn']); $app['config']->set('ldaplookup.bindRdn', $config['bindRdn']); $app['config']->set('ldaplookup.bindPassword', $config['bindPassword']); - } protected function mockLdapLookupConfig() { - $configFile = dirname(__FILE__) . '/config.php'; - if (!file_exists($configFile)) - { - + if (!file_exists($configFile)) { $this->fail('Please take the config.php.dist and create a config.php file ' . 'with your LDAP server details within the same directory'); - } return include $configFile; - } protected function getPackageProviders($app) @@ -53,29 +45,22 @@ protected function getPackageAliases($app) public function testFacadeCanBeResolvedToServiceInstance() { - $ldapLookup = \LdapLookup::connect(); $this->assertInstanceOf('Maenbn\LdapLookup\LdapLookup', $ldapLookup); - } public function testConnection() { - $ldapLookup = \LdapLookup::connect(); $this->assertTrue($ldapLookup->connected); - } public function testGetByUid() { - $entry = \LdapLookup::getByUid($this->config['test_user']); $this->assertArrayHasKey('cn', $entry); - } - -} \ No newline at end of file +}