Skip to content

Commit

Permalink
Fix offset error being thrown if there is no user found. Also format …
Browse files Browse the repository at this point in the history
…code to PSR-2
  • Loading branch information
maen-bn committed Oct 29, 2015
1 parent 99b7053 commit 94b8304
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 82 deletions.
19 changes: 4 additions & 15 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,35 @@

namespace Maenbn\LdapLookup;


class Connection implements ConnectionInterface {


class Connection implements ConnectionInterface
{
public $config;

public $connection;


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);

}

}
}
9 changes: 5 additions & 4 deletions src/ConnectionInterface.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php namespace Maenbn\LdapLookup;
<?php

namespace Maenbn\LdapLookup;

interface ConnectionInterface {

interface ConnectionInterface
{
public function connect();

public function close();
}
}
1 change: 0 additions & 1 deletion src/Facades/LdapLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

class LdapLookup extends Facade
{

protected static function getFacadeAccessor()
{
return 'ldaplookup';
Expand Down
42 changes: 13 additions & 29 deletions src/LdapLookup.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php namespace Maenbn\LdapLookup;
<?php

use Maenbn\LdapLookup\ConnectionInterface as LdapConnection;
namespace Maenbn\LdapLookup;

class LdapLookup implements LookupInterface {
use Maenbn\LdapLookup\ConnectionInterface as LdapConnection;

class LdapLookup implements LookupInterface
{
protected $connection;

public $connected = false;
Expand All @@ -17,81 +19,63 @@ public function __construct(LdapConnection $connection)
$this->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;


}

}
}
15 changes: 7 additions & 8 deletions src/LdapLookupServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php namespace Maenbn\LdapLookup;
<?php

namespace Maenbn\LdapLookup;

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\ServiceProvider;

class LdapLookupServiceProvider extends ServiceProvider {

class LdapLookupServiceProvider extends ServiceProvider
{
/**
* Boot the service provider.
*
Expand Down Expand Up @@ -51,22 +53,19 @@ protected function registerConnection(Application $app)
return new Connection($config);
});*/

$app->bind('ConnectionInterface', function ($app)
{
$app->bind('ConnectionInterface', function ($app) {

$config = $app['config']['ldaplookup'];

return new Connection($config);
});

$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'];

Expand Down
11 changes: 5 additions & 6 deletions src/LookupInterface.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php namespace Maenbn\LdapLookup;


interface LookupInterface {
<?php

namespace Maenbn\LdapLookup;

interface LookupInterface
{
public function connect();

}
}
23 changes: 4 additions & 19 deletions tests/LdapLookupTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<?php namespace Orchestra\Testbench\TestCase;


class LdapLookupTest extends \Orchestra\Testbench\TestCase {

class LdapLookupTest extends \Orchestra\Testbench\TestCase
{
protected $config;

protected function getEnvironmentSetUp($app)
{

$config = $this->mockLdapLookupConfig();

$this->config = $config;
Expand All @@ -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)
Expand All @@ -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);

}

}
}

0 comments on commit 94b8304

Please sign in to comment.