Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
SaschaDens committed Feb 22, 2015
1 parent 6e0dfcf commit 3cf8cfb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 66 deletions.
21 changes: 9 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,29 @@ Provides an solution for authentication users with LDAP for Laravel 5.0.x. It us
```php
'driver' => 'ldap',
```
1. Create a new configuration file `ldap.php` in the configuration folder of Laravel `app/config/ldap.php` and modify to your needs.

For more detail of the configuration you can always check on [ADLAP documentation](http://adldap.sourceforge.net/wiki/doku.php?id=documentation_configuration)
```php
<?php
1. Create a new configuration file `ldap.php` in the configuration folder of Laravel `app/config/ldap.php` and modify to your needs. For more detail of the configuration you can always check on [ADLAP documentation](http://adldap.sourceforge.net/wiki/doku.php?id=documentation_configuration)

```
return array(
'account_suffix' => "@domain.local",
'domain_controllers' => array("192.168.0.1", "dc02.domain.local"), // Load balancing domain controllers
'base_dn' => 'DC=domain,DC=local',
'account_suffix'=> "@domain.local",
'domain_controllers'=> array("192.168.0.1", "dc02.domain.local"), // Load balancing domain controllers
'base_dn' => 'DC=domain,DC=local',
);
```
1. Once this is done you arrived at the final step and you will need to add a service provider. Open `config/app.php`, and add a new item to the providers array.

```php
```
'Dsdevbe\LdapConnector\LdapConnectorServiceProvider'
```

## Usage
The LDAP plugin is an extension of the AUTH class and will act the same as normal usage with Eloquent driver.

```php
```
if (Auth::attempt(array('username' => $email, 'password' => $password)))
{
return Redirect::intended('dashboard');
}
```

You can find more examples on [Laravel Auth Documentation](http://laravel.com/docs/security#authenticating-users) on using the `Auth::` function.
You can find more examples on [Laravel Auth Documentation](http://laravel.com/docs/master/authentication) on using the `Auth::` function.
62 changes: 8 additions & 54 deletions src/Dsdevbe/LdapConnector/LdapUserProvider.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
<?php namespace Dsdevbe\LdapConnector;

use Exception;
use adLDAP\adLDAP;
use adLDAP\collections\adLDAPUserCollection;
use adLDAP\adLDAPException;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider as UserProviderInterface;

class LdapUserProvider implements UserProviderInterface {

/**
* Configuration to connect to LDAP.
*
* @var string
*/
protected $config;

/**
* Stores connection to LDAP.
*
Expand All @@ -31,8 +21,7 @@ class LdapUserProvider implements UserProviderInterface {
*/
public function __construct($config)
{
$this->config = $config;
$this->connectLdap();
$this->adldap = new adLDAP($config);
}

/**
Expand All @@ -43,11 +32,7 @@ public function __construct($config)
*/
public function retrieveById($identifier)
{
$info = $this->adldap->user()->infoCollection($identifier);
if($info)
{
return new LdapUser($this->mapCollectionToArray($info));
}
// TODO: Implement retrieveById() method.
}

/**
Expand Down Expand Up @@ -79,12 +64,13 @@ public function updateRememberToken(Authenticatable $user, $token)
*/
public function retrieveByCredentials(array $credentials)
{
if($userInfo = $this->adldap->user()->info($credentials['username']))
{
$userInfo = $userInfo[0];
foreach($userInfo as $u=>$a){
$credentials[$u]=$a[0];
if ($this->adldap->authenticate($credentials['username'], $credentials['password'])) {
$userInfo = $this->adldap->user()->info($credentials['username'], array('*'))[0];

foreach($userInfo as $key => $value){
$credentials[$key] = $value[0];
}

return new LdapUser($credentials);
}
}
Expand All @@ -97,36 +83,4 @@ public function validateCredentials(Authenticatable $user, array $credentials)
return $this->adldap->authenticate($username, $password);
}

/**
* @param adLDAPUserCollection $collection
* @return array
*/
public function mapCollectionToArray(adLDAPUserCollection $collection)
{
$arr = array(
'username' => $collection->samaccountname,
'displayname' => $collection->displayname,
'email' => $collection->mail,
'memberof' => $collection->memberof
);

return $arr;
}

/**
* Connect to LDAP
*
* @throws \Exception
*/
public function connectLdap()
{
try
{
$this->adldap = new adLDAP($this->config);
} catch(adLDAPException $e)
{
throw new Exception($e->getMessage());
}
}

}

0 comments on commit 3cf8cfb

Please sign in to comment.