Skip to content

Commit

Permalink
Merge pull request #15 from SaschaDens/dev
Browse files Browse the repository at this point in the history
Refactor of plugin to fix #14 #13 and add new functionalities
  • Loading branch information
SaschaDens committed Aug 2, 2015
2 parents 4cf60a7 + c20236d commit 15f8fb3
Show file tree
Hide file tree
Showing 14 changed files with 346 additions and 253 deletions.
Empty file modified .gitignore
100755 → 100644
Empty file.
Empty file modified .travis.yml
100755 → 100644
Empty file.
41 changes: 25 additions & 16 deletions README.md
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
# Package abandoned
This package will no longer be maintained due to the lack of time.

# Ldap-connector
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
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

## Installation
1. Install this package through Composer for Laravel v5.0:
1. Install this package through Composer for Laravel v5.x:
```js
composer require dsdevbe/ldap-connector:3.*
```
Expand All @@ -19,15 +16,27 @@ 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)
1. Publish a new configuration file with `php artisan vendor:publish` in the configuration folder of Laravel you will find `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',
'plugins' => array(
'adldap' => 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' => '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!

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.

```
Expand All @@ -37,11 +46,11 @@ Provides an solution for authentication users with LDAP for Laravel 5.0.x. It us
## Usage
The LDAP plugin is an extension of the AUTH class and will act the same as normal usage with Eloquent driver.

```
if (Auth::attempt(array('username' => $email, 'password' => $password)))
{
return Redirect::intended('dashboard');
}
```
```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/master/authentication) 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.
6 changes: 3 additions & 3 deletions composer.json
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
"name": "dsdevbe/ldap-connector",
"description": "Easily authenticate with LDAP in Laravel",
"homepage": "https://github.com/dsdevbe/ldap-connector",
"homepage": "https://github.com/SaschaDens/ldap-connector",
"keywords": ["Laravel", "Ldap"],
"license": "MIT",
"authors": [
{
"name": "Dsdevbe",
"name": "SaschaDens",
"email": "dens.sascha@gmail.com"
}
],
"require": {
"php": ">=5.3.0",
"laravel/framework": "~5.0",
"adldap/adldap": "4.0-stable"
"adldap2/adldap2": "^4.0"
},
"autoload": {
"psr-0": {
Expand Down
Empty file modified phpunit.xml
100755 → 100644
Empty file.
66 changes: 66 additions & 0 deletions src/Dsdevbe/LdapConnector/Adapter/Adldap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
namespace Dsdevbe\LdapConnector\Adapter;

use adLDAP\adLDAP as adLDAPService;
use Dsdevbe\LdapConnector\Model\User as UserModel;

class Adldap implements LdapInterface
{
protected $_ldap;

protected $_username;

protected $_password;

protected function _mapDataToUserModel($username, array $groups)
{
$model = new UserModel([
'username' => $username,
'password' => $this->_password,
]);
$model->setGroups($groups);
return $model;
}

public function __construct($config)
{
$this->_ldap = new adLDAPService($config);
}

/**
* @param String $username
* @param String $password
* @return bool
*/
public function connect($username, $password)
{
$this->_username = $username;
$this->_password = $password;

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

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

/**
* @param String $username
* @return UserModel
*/
public function getUserInfo($username)
{
$user = $this->_ldap->user()->info($username);

if (!$user) {
return null;
}

$groups = $this->_ldap->user()->groups($username);
return $this->_mapDataToUserModel($username, $groups);
}
}
25 changes: 25 additions & 0 deletions src/Dsdevbe/LdapConnector/Adapter/LdapInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace Dsdevbe\LdapConnector\Adapter;

use Dsdevbe\LdapConnector\Model\User as UserModel;

interface LdapInterface
{
/**
* @param String $username
* @param String $password
* @return bool
*/
public function connect($username, $password);

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

/**
* @param $username
* @return UserModel
*/
public function getUserInfo($username);
}
16 changes: 16 additions & 0 deletions src/Dsdevbe/LdapConnector/Config/ldap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

return array(
'plugins' => array(
'adldap' => 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' => 'admin', // This is required for session persistance in the application
'admin_password' => 'yourPassword',
),
),
);
138 changes: 80 additions & 58 deletions src/Dsdevbe/LdapConnector/LdapConnectorServiceProvider.php
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,62 +1,84 @@
<?php namespace Dsdevbe\LdapConnector;
<?php
namespace Dsdevbe\LdapConnector;

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

class LdapConnectorServiceProvider extends ServiceProvider {

/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;

/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
Auth::extend('ldap', function($app) {
$provider = new LdapUserProvider($this->getConfig());
return new Guard($provider, $app['session.store']);
});
}

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
}

/**
* Get the services provided by the provider.
*
* @return array
*/
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'];
}

class LdapConnectorServiceProvider extends ServiceProvider
{

/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;

/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
Auth::extend('ldap', function ($app) {
$ldap = new Adldap(
$this->getLdapAdapterConfig('adldap')
);
$provider = new LdapUserProvider($ldap);

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

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$ldapConfig = __DIR__ . '/Config/ldap.php';
$this->publishConfig($ldapConfig);
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return array('auth');
}

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

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

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

return $pluginsConfig[$pluginName];
}
}
Loading

0 comments on commit 15f8fb3

Please sign in to comment.