Skip to content

Commit

Permalink
Merge pull request #31 from SaschaDens/laravel-5-2-support
Browse files Browse the repository at this point in the history
Laravel 5 2 support closes #28
  • Loading branch information
SaschaDens committed Mar 1, 2016
2 parents 07a393e + 89beed1 commit de5547f
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 157 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- hhvm

before_install: echo "extension=ldap.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini

before_script:
- composer self-update
Expand Down
86 changes: 79 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@
[![Total Downloads](https://poser.pugx.org/dsdevbe/ldap-connector/downloads)](https://packagist.org/packages/dsdevbe/ldap-connector)
[![License](https://poser.pugx.org/dsdevbe/ldap-connector/license)](https://packagist.org/packages/dsdevbe/ldap-connector)

Provides an solution for authentication users with LDAP for Laravel 5.x. It uses ADLDAP 4.0 library forked on [Adldap2](https://github.com/Adldap2/Adldap2) to create a bridge between Laravel and LDAP
Provides an solution for authentication users with LDAP for Laravel 5.x. It uses ADLDAP library on [Adldap2](https://github.com/Adldap2/Adldap2) to create a bridge between Laravel and LDAP

## Installation
1. Install this package through Composer for Laravel v5.x:
- [Laravel 5.1 - 5.0](#laravel-51---50)
- [Laravel 5.2 - ...](#laravel-52---)

## Laravel 5.1 - 5.0
1. Install this package through Composer
```js
composer require dsdevbe/ldap-connector:3.*
composer require dsdevbe/3.*
```

1. Add the service provider in the app configuration by opening `config/app.php`, and add a new item to the providers array.

```
Dsdevbe\LdapConnector\LdapConnectorServiceProvider::class
```
```
Dsdevbe\LdapConnector\LdapConnectorServiceProvider::class
```

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
Expand All @@ -43,7 +48,7 @@ Provides an solution for authentication users with LDAP for Laravel 5.x. It uses

Please note that the fields 'admin_username' and 'admin_password' are required for session persistance!

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

```php
Expand Down Expand Up @@ -73,3 +78,70 @@ Laravel documentation: [Authentication Quickstart](http://laravel.com/docs/maste
- `Auth::user()->getFirstname()` returns authenticated first name.
- `Auth::user()->getLastname()` returns authenticated last name.
- `Auth::user()->getEmail()` returns authenticated email address.

## Laravel 5.2 - ...
1. Install this package through Composer
```js
composer require dsdevbe/4.0.*
```

1. Add the service provider in the app configuration by opening `config/app.php`, and add a new item to the providers array.

```
Dsdevbe\LdapConnector\LdapConnectorServiceProvider::class
```

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
'providers' => [
'users' => [
'driver' => 'ldap',
'adldap' => [
'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' => 'admin', // This is required for session persistance in the application
'admin_password' => 'yourPassword',
],
],
],
```
Please note that the fields 'admin_username' and 'admin_password' are required for session persistance!

### 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' => $username, 'password' => $password)))
{
return Redirect::intended('dashboard');
}
```
You can find more examples on [Laravel Auth Documentation](http://laravel.com/docs/master/authentication) on using the `Auth::` function.

### Use AuthController
If you want to use the authentication controller that ships with Laravel you will need to change the following files.
By default `App\Http\Controllers\Auth\AuthController` checks for the `email` field if nothing is provided. To overwrite this value add the following line in the `AuthController`.

```php
protected $username = 'username';
```

Laravel documentation: [Authentication Quickstart](http://laravel.com/docs/master/authentication#authentication-quickstart)

### Ldap User Information
Difference with ldap-connector V3 is that now the adLDAP model is directly exposed on the user model. This means that you can fetch all data directly from the user.
To access the adldap model you can use now `Auth::user()->getAdLDAP()`.

Examples:
- `Auth::user()->getAdLDAP()->getAccountName()`
- `Auth::user()->getAdLDAP()->getFirstName()`

To fetch more properties please check [adLDAP2 documentation](https://github.com/Adldap2/Adldap2/blob/v5.2/docs/models/USER.md)

## Contributing
Feel free to contribute to this project for new features or bug fixes. We are open for improvements!
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
}
],
"require": {
"php": ">=5.4.0",
"php": ">=5.5.9",
"ext-ldap": "*",
"laravel/framework": "~5.0",
"adldap2/adldap2": "^4.0"
"adldap2/adldap2": "5.2.*"
},
"require-dev": {
"phpspec/phpspec": "^2.2"
Expand Down
6 changes: 3 additions & 3 deletions spec/Dsdevbe/LdapConnector/LdapUserProviderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
namespace spec\Dsdevbe\LdapConnector;

use Dsdevbe\LdapConnector\Adapter\LdapInterface;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
use PhpSpec\ObjectBehavior;

class LdapUserProviderSpec extends ObjectBehavior
{
public function let(LdapInterface $interface)
public function let(HasherContract $hasher, LdapInterface $interface)
{
$this->beConstructedWith($interface);
$this->beConstructedWith($hasher, $interface);
}

public function it_is_initializable()
Expand All @@ -33,7 +34,6 @@ public function it_retrieves_user_by_id(LdapInterface $interface)
$identifier = 'john.doe@example.com';

$interface->getUserInfo($identifier)->shouldBeCalled();

$this->retrieveById($identifier);
}
}
70 changes: 32 additions & 38 deletions src/Dsdevbe/LdapConnector/Adapter/Adldap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,36 @@

namespace Dsdevbe\LdapConnector\Adapter;

use adLDAP\adLDAP as adLDAPService;
use adLDAP\collections\adLDAPUserCollection as adLDAPUserCollection;
use Adldap\Adldap as adLDAPService;
use Adldap\Models\User as adLDAPUserModel;
use Dsdevbe\LdapConnector\Model\User as UserModel;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;

class Adldap implements LdapInterface
{
/**
* @var HasherContract
*/
protected $_hasher;

/**
* @var adLDAPService
*/
protected $_ldap;

/**
* @var string
*/
protected $_username;

/**
* @var string
*/
protected $_password;

protected function mapDataToUserModel(adLDAPUserCollection $user, array $groups)
{
$model = new UserModel([
'username' => $user->samaccountname,
'password' => $this->_password,
]);
$model->setGroups($groups);
$model->setUserInfo([
'username' => $user->samaccountname,
'firstname' => $user->givenname,
'lastname' => $user->sn,
'email' => $user->mail,
]);

return $model;
}

public function __construct($config)
public function __construct(HasherContract $hasher, array $config)
{
$this->_hasher = $hasher;
$this->_ldap = new adLDAPService($config);
}

Expand All @@ -44,35 +43,30 @@ public function __construct($config)
*/
public function connect($username, $password)
{
$this->_username = $username;
$this->_password = $password;

return $this->_ldap->authenticate($username, $password);
}

/**
* @return bool
*/
public function isConnected()
{
return (bool) $this->_ldap->getLdapBind();
}

/**
* @param string $username
* @param string $password
*
* @return UserModel
*/
public function getUserInfo($username)
public function getUserInfo($username, $password = null)
{
$user = $this->_ldap->user()->infoCollection($username, ['samaccountname', 'givenname', 'sn', 'mail']);
$user = $this->_ldap->search()->where('samaccountname', '=', $username)->first();

if (!$user) {
return;
}
return $this->mapDataToUserModel($user, $password);
}

$groups = $this->_ldap->user()->groups($username);
protected function mapDataToUserModel(adLDAPUserModel $user, $password)
{
$model = new UserModel([
'username' => $user->getAccountName(),
'password' => ($password) ? $this->_hasher->make($password) : null,
]);
$model->setUserInfo($user);

return $this->mapDataToUserModel($user, $groups);
return $model;
}
}
8 changes: 2 additions & 6 deletions src/Dsdevbe/LdapConnector/Adapter/LdapInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@ interface LdapInterface
*/
public function connect($username, $password);

/**
* @return bool
*/
public function isConnected();

/**
* @param $username
* @param string|null $password
*
* @return UserModel
*/
public function getUserInfo($username);
public function getUserInfo($username, $password = null);
}
9 changes: 9 additions & 0 deletions src/Dsdevbe/LdapConnector/Exception/MissingConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Dsdevbe\LdapConnector\Exception;

use Exception;

class MissingConfiguration extends Exception
{
}
43 changes: 11 additions & 32 deletions src/Dsdevbe/LdapConnector/LdapConnectorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Auth;
use Dsdevbe\LdapConnector\Adapter\Adldap;
use Illuminate\Auth\Guard;
use Dsdevbe\LdapConnector\Exception\MissingConfiguration;
use Illuminate\Support\ServiceProvider;

class LdapConnectorServiceProvider extends ServiceProvider
Expand All @@ -23,13 +23,13 @@ class LdapConnectorServiceProvider extends ServiceProvider
*/
public function boot()
{
Auth::extend('ldap', function($app) {
$ldap = new Adldap(
$this->getLdapAdapterConfig('adldap')
);
$provider = new LdapUserProvider($ldap);
Auth::provider('ldap', function ($app, array $config) {
if (!$this->hasLdapConfiguration($config)) {
throw new MissingConfiguration('Please check if your configuration is available in config/auth.php');
}
$ldap = new Adldap($app['hash'], $config['adldap']);

return new Guard($provider, $app['session.store']);
return new LdapUserProvider($app['hash'], $ldap);
});
}

Expand All @@ -40,8 +40,6 @@ public function boot()
*/
public function register()
{
$ldapConfig = __DIR__ . '/Config/ldap.php';
$this->publishConfig($ldapConfig);
}

/**
Expand All @@ -54,32 +52,13 @@ public function provides()
return ['auth'];
}

protected function publishConfig($configPath)
{
$this->publishes([
$configPath => config_path('ldap.php'),
]);
}

/**
* Get ldap configuration.
*
* @return array
*/
public function getLdapConfig()
{
return $this->app['config']->get('ldap');
}

/**
* @param $pluginName
* @param $config
*
* @return array
* @return bool
*/
public function getLdapAdapterConfig($pluginName)
protected function hasLdapConfiguration($config)
{
$pluginsConfig = $this->app['config']->get('ldap.plugins');

return $pluginsConfig[$pluginName];
return isset($config['adldap']) && is_array($config['adldap']);
}
}
Loading

0 comments on commit de5547f

Please sign in to comment.