Skip to content

Commit

Permalink
Merge pull request #7 from dsdevbe/dev
Browse files Browse the repository at this point in the history
Update for Laravel 5.0
  • Loading branch information
Sascha Dens committed Feb 6, 2015
2 parents 2d6eb33 + 421ac70 commit 8965771
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 66 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
composer.phar
composer.lock
.DS_Store
/.idea
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
# Ldap-connector
Provides an solution for authentication users with LDAP for Laravel 4.2.x
Provides an solution for authentication users with LDAP for Laravel 5.0.x. It uses ADLDAP library to create a bridge between Laravel and LDAP

## Installation
1. Install this package through Composer:

1. Install this package through Composer for Laravel v5.0:
```js
composer require dsdevbe/ldap-connector:3.*
```
If you still want to use Ldap-connector for Laravel v4.2 please refer to the following package
```js
composer require dsdevbe/ldap-connector:2.*
```

1. Change the authentication driver in the Laravel config to use the ldap driver. You can find this in the following file `app/config/auth.php`
1. Change the authentication driver in the Laravel config to use the ldap driver. You can find this in the following file `config/auth.php`

```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
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',
'admin_username' => 'dummy', // Just needs to be an valid account to query other users if they exists
'admin_password' => 'password'
);
```
1. Once this is done you arrived at the final step and you will need to add a service provider. Open `app/config/app.php`, and add a new item to the providers array.
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'
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
"require": {
"php": ">=5.3.0",
"illuminate/support": "4.2.*",
"laravel/framework": "~5.0",
"adldap/adldap": "4.0-stable"
},
"autoload": {
Expand Down
36 changes: 18 additions & 18 deletions src/Dsdevbe/LdapConnector/LdapConnectorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use Illuminate\Auth\Guard;
use Illuminate\Support\ServiceProvider;
use LdapManager;
use Auth;
use Exception;

class LdapConnectorServiceProvider extends ServiceProvider {
Expand All @@ -21,12 +21,10 @@ class LdapConnectorServiceProvider extends ServiceProvider {
*/
public function boot()
{
$this->package('dsdevbe/ldap-connector');

\Auth::extend('ldap', function($app) {
$provider = new LdapUserProvider($this->getConfig());
return new Guard($provider, $app['session.store']);
});
Auth::extend('ldap', function($app) {
$provider = new LdapUserProvider($this->getConfig());
return new Guard($provider, $app['session.store']);
});
}

/**
Expand All @@ -48,15 +46,17 @@ public function provides()
return array('auth');
}

/**
* Get ldap configuration
*
* @return array
*/
public function getConfig()
{
if(!$this->app['config']['ldap']) throw new Exception('LDAP config not found. Check if app/config/ldap.php exists.');

return $this->app['config']['ldap'];
}
/**
* Get ldap configuration
*
* @return array
*/
public function getConfig()
{
if(!$this->app['config']['ldap']) {
throw new Exception('LDAP config not found. Check if app/config/ldap.php exists.');
}

return $this->app['config']['ldap'];
}
}
12 changes: 2 additions & 10 deletions src/Dsdevbe/LdapConnector/LdapUser.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php namespace Dsdevbe\LdapConnector;


use Illuminate\Auth\UserInterface;
use Illuminate\Contracts\Auth\Authenticatable;

class LdapUser implements UserInterface
class LdapUser implements Authenticatable
{

/**
Expand Down Expand Up @@ -74,8 +74,6 @@ public function getRememberTokenName()
}

/**
* Dynamically access the user's attributes.
*
* @param string $key
* @return mixed
*/
Expand All @@ -85,8 +83,6 @@ public function __get($key)
}

/**
* Dynamically set an attribute on the user.
*
* @param string $key
* @param mixed $value
* @return void
Expand All @@ -97,8 +93,6 @@ public function __set($key, $value)
}

/**
* Dynamically check if a value is set on the user.
*
* @param string $key
* @return bool
*/
Expand All @@ -108,8 +102,6 @@ public function __isset($key)
}

/**
* Dynamically unset a value on the user.
*
* @param string $key
* @return bool
*/
Expand Down
51 changes: 21 additions & 30 deletions src/Dsdevbe/LdapConnector/LdapUserProvider.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php namespace Dsdevbe\LdapConnector;

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

class LdapUserProvider implements UserProviderInterface{
class LdapUserProvider implements UserProviderInterface {

/**
* Configuration to connect to LDAP.
Expand Down Expand Up @@ -37,14 +39,14 @@ public function __construct($config)
* Retrieve a user by their unique identifier.
*
* @param mixed $identifier
* @return \Illuminate\Auth\UserInterface|null
* @return Authenticatable
*/
public function retrieveById($identifier)
{
$info = $this->adldap->user()->infoCollection($identifier);
if($info)
{
return new LdapUser($this->fetchObject($info));
return new LdapUser($this->mapCollectionToArray($info));
}
}

Expand All @@ -53,21 +55,18 @@ public function retrieveById($identifier)
*
* @param mixed $identifier
* @param string $token
* @return \Illuminate\Auth\UserInterface|null
* @return Authenticatable|null
*/
public function retrieveByToken($identifier, $token)
{
// TODO: Implement retrieveByToken() method.
}

/**
* Update the "remember me" token for the given user in storage.
*
* @param \Illuminate\Auth\UserInterface $user
* @param string $token
* @return void
* @param Authenticatable $user
* @param string $token
*/
public function updateRememberToken(UserInterface $user, $token)
public function updateRememberToken(Authenticatable $user, $token)
{
// TODO: Implement updateRememberToken() method.
}
Expand All @@ -76,7 +75,7 @@ public function updateRememberToken(UserInterface $user, $token)
* Retrieve a user by the given credentials.
*
* @param array $credentials
* @return \Illuminate\Auth\UserInterface|null
* @return Authenticatable|null
*/
public function retrieveByCredentials(array $credentials)
{
Expand All @@ -86,14 +85,7 @@ public function retrieveByCredentials(array $credentials)
}
}

/**
* Validate a user against the given credentials.
*
* @param \Illuminate\Auth\UserInterface $user
* @param array $credentials
* @return bool
*/
public function validateCredentials(UserInterface $user, array $credentials)
public function validateCredentials(Authenticatable $user, array $credentials)
{
$username = $credentials['username'];
$password = $credentials['password'];
Expand All @@ -102,18 +94,16 @@ public function validateCredentials(UserInterface $user, array $credentials)
}

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

return $arr;
Expand All @@ -131,7 +121,8 @@ public function connectLdap()
$this->adldap = new adLDAP($this->config);
} catch(adLDAPException $e)
{
throw new \Exception($e->getMessage());
throw new Exception($e->getMessage());
}
}

}

0 comments on commit 8965771

Please sign in to comment.