2017 © Pedro Peláez
 

project adldap2-laravel

Adldap2 for Laravel 5.

image

minkbear/adldap2-laravel

Adldap2 for Laravel 5.

  • Wednesday, November 23, 2016
  • by minkbear
  • Repository
  • 1 Watchers
  • 1 Stars
  • 170 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 99 Forks
  • 0 Open issues
  • 44 Versions
  • 0 % Grown

The README.md

Adldap2 - Laravel

Build Status Scrutinizer Code Quality Total Downloads Latest Stable Version License, (*1)

Description

Adldap2 - Laravel allows easy configuration, access, and management to active directory utilizing the root Adldap2 Repository., (*2)

It includes:, (*3)

  • An Adldap contract (Adldap\Contracts\AdldapInterface) for dependency injection through Laravel's IoC
  • An Auth driver for easily allowing users to login to your application using active directory
  • An Adldap facade (Adldap\Laravel\Facades\Adldap) for easily retrieving the Adldap instance from the IoC
  • Support for multiple LDAP connections

Installation

Quick Start - From Scratch, (*4)

Insert Adldap2-Laravel into your composer.json file:, (*5)

"adldap2/adldap2-laravel": "2.0.*",

Then run composer update., (*6)

Once finished, insert the service provider in your config/app.php file:, (*7)

Adldap\Laravel\AdldapServiceProvider::class,

Then insert the facade:, (*8)

'Adldap' => Adldap\Laravel\Facades\Adldap::class

Publish the configuration file by running:, (*9)

php artisan vendor:publish --tag="adldap"

Now you're all set!, (*10)

Usage

You can perform all methods on Adldap through its facade like so:, (*11)

// Finding a user.
$user = Adldap::getProvider('default')->search()->users()->find('john doe');

// Searching for a user.
$search = Adldap::getProvider('default')->search()->where('cn', '=', 'John Doe')->get();

// Authenticating.
if (Adldap::getProvider('default')->auth()->attempt($username, $password)) {
    // Passed!
}

Or you can inject the Adldap contract:, (*12)

use Adldap\Contracts\AdldapInterface;

class UserController extends Controller
{
    /**
     * @var Adldap
     */
    protected $adldap;

    /**
     * Constructor.
     *
     * @param AdldapInterface $adldap
     */
    public function __construct(AdldapInterface $adldap)
    {
        $this->adldap = $adldap;
    }

    /**
     * Displays the all LDAP users.
     *
     * @return \Illuminate\View\View
     */
    public function index()
    {
        $users = $this->adldap->getProvider('default')->search()->users()->get();

        return view('users.index', compact('users'));
    }
}

To see more usage in detail, please visit the Adldap2 Repository;, (*13)

Auth Driver

The Adldap Laravel auth driver allows you to seamlessly authenticate active directory users, as well as have a local database record of the user. This allows you to easily attach information to the users as you would a regular laravel application., (*14)

Note: The Adldap auth driver actually extends from and utilizes Laravel's eloquent auth driver., (*15)

Installation

Laravel 5.1

Insert the AdldapAuthServiceProvider into your config/app.php file:, (*16)

Adldap\Laravel\AdldapAuthServiceProvider::class,

Publish the auth configuration:, (*17)

php artisan vendor:publish --tag="adldap"

Change the auth driver in config/auth.php to adldap:, (*18)

/*
|--------------------------------------------------------------------------
| Default Authentication Driver
|--------------------------------------------------------------------------
|
| This option controls the authentication driver that will be utilized.
| This driver manages the retrieval and authentication of the users
| attempting to get access to protected areas of your application.
|
| Supported: "database", "eloquent"
|
*/

'driver' => 'adldap',

Laravel 5.2

Insert the AdldapAuthServiceProvider into your config/app.php file:, (*19)

Adldap\Laravel\AdldapAuthServiceProvider::class,

Publish the auth configuration:, (*20)

php artisan vendor:publish --tag="adldap"

Open your config/auth.php configuration file and change the following:, (*21)

Change the provider entry inside the web authentication guard:, (*22)

/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| Of course, a great default configuration has been defined for you
| here which uses session storage and the Eloquent user provider.
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| Supported: "session", "token"
|
*/

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'adldap',
    ],
    'api' => [
        'driver' => 'token',
        'provider' => 'users',
    ],
],

Now add the adldap provider to your providers array:, (*23)

/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| If you have multiple user tables or models you may configure multiple
| sources which represent each model / table. These sources may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database", "eloquent"
|
*/
'providers' => [
    'adldap' => [
        'driver' => 'adldap',
        'model' => App\User::class,
    ],
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],
    // 'users' => [
    //     'driver' => 'database',
    //     'table' => 'users',
    // ],
],

Usage

Username Attributes

Inside your config/adldap_auth.php file there is a configuration option named username_attribute. The key of the array indicates the input name of your login form, and the value indicates the LDAP attribute that this references., (*24)

This option just allows you to set your input name to however you see fit, and allow different ways of logging in a user., (*25)

In your login form, change the username form input name to your configured input name., (*26)

By default this is set to email:, (*27)

<input type="text" name="email" />

<input type="password" name="password" />

You'll also need to add the following to your AuthController if you're not overriding the default postLogin method., (*28)

protected $username = 'email';

If you'd like to use the users samaccountname to login instead, just change your input name and auth configuration:, (*29)

<input type="text" name="username" />

<input type="password" name="password" />

Note: If you're using the username input field, make sure you have the username field inside your users database table as well. By default, laravel's migrations use the email field., (*30)

Inside config/adldap_auth.php, (*31)

'username_attribute' => ['username' => 'samaccountname'],

Note: The actual authentication is done with the login_attribute inside your config/adldap_auth.php file., (*32)

Logging In

Login a user regularly using Auth::attempt($credentials);. Using Auth::user() when a user is logged in will return your configured App\User model in config/auth.php., (*33)

Synchronizing Attributes

Inside your config/adldap_auth.php file there is a configuration option named sync_attributes. This is an array of attributes where the key is the User model attribute, and the value is the active directory users attribute., (*34)

By default, the User models name attribute is synchronized to the AD users cn attribute. This means, upon login, the users name attribute on Laravel User Model will be set to the active directory common name (cn) attribute, then saved., (*35)

Feel free to add more attributes here, however be sure that your users database table contains the key you've entered., (*36)

Sync Attribute Callbacks

Note: This feature was introduced in v1.3.8., (*37)

If you're looking to synchronize an attribute from an Adldap model that contains an array or an object, you can use a callback to return a specific value to your Laravel model's attribute. For example:, (*38)

'sync_attributes' => [

    'name' => 'App\Handlers\LdapAttributeHandler@name',

],

The LdapAttributeHandler class:, (*39)

namespace App\Handlers;

use Adldap\Models\User;

class LdapAttributeHandler
{
    /**
     * Returns the common name of the AD User.
     *
     * @param User $user
     *
     * @return string
     */
    public function name(User $user)
    {
        return $user->getAccountName();
    }
}

Note: Attribute handlers are constructed using the app() helper. This means you can type-hint any application dependencies you may need in the handlers constructor., (*40)

Binding the Adldap User Model to the Laravel User Model

Note: Before we begin, enabling this option will perform a single query on your AD server for a logged in user per request. Eloquent already does this for authentication, however this could lead to slightly longer load times (depending on your AD server and network speed of course)., (*41)

Inside your config/adldap_auth.php file there is a configuration option named bind_user_to_model. Setting this to true sets the adldapUser property on your configured auth User model to the Adldap User model. For example:, (*42)

if (Auth::attempt($credentials)) {
    $user = Auth::user();

    var_dump($user); // Returns instance of App\User;

    var_dump($user->adldapUser); // Returns instance of Adldap\Models\User;

    // Retrieving the authenticated LDAP users groups
    $groups = $user->adldapUser->getGroups();
}

You must insert the trait Adldap\Laravel\Traits\AdldapUserModelTrait onto your configured auth User model, OR Add the public property adldapUser to your model., (*43)

<?php
// app/User.php

namespace App;

use Adldap\Laravel\Traits\AdldapUserModelTrait;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Authenticatable, CanResetPassword, AdldapUserModelTrait; // Insert trait here

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'email', 'password'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];
}

Login Fallback

Note: This feature was introduced in v1.3.9. You'll will need to re-publish the Adldap Auth configuration file to receive this option., (*44)

The login fallback option allows you to login as a local database user using the Eloquent authentication driver if active directory authentication fails. This option would be handy in environments where:, (*45)

  • You may have some active directory users and other users registering through the website itself (user does not exist in your AD).
  • Local development where your AD server may be unavailable

To enable it, simply set the option to true in your adldap_auth.php configuration file:, (*46)

'login_fallback' => false, // Set to true.

Windows Authentication (SSO) Middleware

Note: This feature was introduced in v1.4.3. You will need to re-publish the Adldap Auth configuration file to receive this option., (*47)

Requirements: This feature assumes that you have enabled Windows Authentication in IIS, or have enabled it in some other means with Apache. Adldap does not set this up for you. To enable Windows Authentication, visit: https://www.iis.net/configreference/system.webserver/security/authentication/windowsauthentication/providers/add, (*48)

SSO authentication allows you to authenticate your users by the pre-populated $_SERVER['AUTH_USER'] (or $_SERVER['REMOTE_USER]) that is filled when users visit your site when SSO is enabled on your server. This is configurable in your adldap_auth.php configuration file., (*49)

To use the middleware, insert it on your middleware stack:, (*50)

protected $middlewareGroups = [
    'web' => [
        Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        Middleware\VerifyCsrfToken::class,
        \Adldap\Laravel\Middleware\WindowsAuthenticate::class, // Inserted here.
    ],
];

Now when you visit your site, a user account will be created (if one doesn't exist already) with a random 16 character string password and then automatically logged in. Neat huh?, (*51)

Login Limitation Filter

Note: This feature was introduced in v1.4.6. You will need to re-publish the Adldap Auth configuration file to receive this option., (*52)

Inside of your config/adldap_auth.php configuration, you can now insert a raw LDAP filter to specify which users are allowed to authenticate., (*53)

This filter persists to the Windows Authentication Middleware as well., (*54)

For example, to allow only users to that contain an email address to login, insert the filter: (mail=*):, (*55)

 /*
 |--------------------------------------------------------------------------
 | Limitation Filter
 |--------------------------------------------------------------------------
 |
 | The limitation filter allows you to enter a raw filter to only allow
 | specific users / groups / ous to authenticate.
 |
 | This should be a standard LDAP filter.
 |
 */

 'limitation_filter' => '(mail=*)',

For another example, here's how you can limit users logging in that are apart of a specific group:, (*56)

Note: This will also allow nested group users to login as well., (*57)

'limitation_filter' => '(memberof:1.2.840.113556.1.4.1941:=CN=MyGroup,DC=example,DC=com)',

Multiple Authentication Connections

Note: This feature was introduced in v2.0.0., (*58)

To swap connections on the fly, set your configurations default connection and try re-authenticating the user:, (*59)

$auth = false;

if (Auth::attempt($credentials)) {
    $auth = true; // Logged in successfully
} else {
    // Login failed, swap and try other connection.
    Config::set('adldap_auth.connection', 'other-connection');

    if (Auth::attempt($credentials)) {
        $auth = true; // Passed logging in with other connection.
    }
}

if ($auth === true) {
    return $this->handleUserWasAuthenticated($request, $throttles);
}

return 'Login incorrect!';

The Versions

23/11 2016

v1.4.x-dev

1.4.9999999.9999999-dev

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

23/11 2016

v1.4.10

1.4.10.0

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

22/11 2016

dev-master

9999999-dev

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

09/05 2016

v1.4.9

1.4.9.0

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

29/04 2016

v2.0.6

2.0.6.0

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

28/04 2016

v2.0.5

2.0.5.0

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

13/04 2016

v2.0.4

2.0.4.0

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

07/04 2016

v2.0.3

2.0.3.0

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

06/04 2016

v2.0.2

2.0.2.0

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

04/04 2016

v2.0.1

2.0.1.0

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

02/04 2016

v2.0.0

2.0.0.0

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

29/02 2016

v1.4.8

1.4.8.0

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

23/02 2016

v1.4.7

1.4.7.0

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

16/02 2016

v1.4.6

1.4.6.0

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

11/02 2016

v1.4.5

1.4.5.0

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

11/02 2016

v1.4.4

1.4.4.0

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

11/02 2016

v1.4.3

1.4.3.0

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

02/02 2016

v1.4.2

1.4.2.0

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

19/01 2016

1.3.x-dev

1.3.9999999.9999999-dev

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

19/01 2016

v1.3.10

1.3.10.0

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

11/01 2016

v1.4.1

1.4.1.0

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

22/12 2015

v1.4.0

1.4.0.0

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

09/12 2015

v1.3.9

1.3.9.0

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

04/12 2015

v1.3.8

1.3.8.0

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

25/11 2015

v1.3.7

1.3.7.0

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

23/11 2015

v1.3.6

1.3.6.0

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

18/11 2015

v1.3.5

1.3.5.0

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

17/11 2015

v1.3.4

1.3.4.0

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

13/11 2015

v1.3.3

1.3.3.0

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

13/11 2015

v1.3.2

1.3.2.0

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

04/11 2015

v1.3.1

1.3.1.0

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

21/10 2015

v1.3.0

1.3.0.0

Adldap2 for Laravel 5.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

28/09 2015

v1.2.5

1.2.5.0

Adldap2 for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

22/09 2015

v1.2.4

1.2.4.0

Adldap2 for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

22/09 2015

v1.2.3

1.2.3.0

Adldap2 for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

22/09 2015

v1.2.2

1.2.2.0

Adldap2 for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

22/09 2015

v1.2.1

1.2.1.0

Adldap2 for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

21/09 2015

v1.2.0

1.2.0.0

Adldap2 for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

09/09 2015

v1.1.2

1.1.2.0

Adldap2 for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

09/09 2015

v1.1.1

1.1.1.0

Adldap2 for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

01/09 2015

v1.1.0

1.1.0.0

Adldap2 for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

28/08 2015

v1.0.2

1.0.2.0

Adldap2 for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

13/08 2015

v1.0.1

1.0.1.0

Adldap2 for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2

11/08 2015

v1.0.0

1.0.0.0

Adldap2 for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel ldap adldap adldap2