PHPackages                             minkbear/adldap2-laravel - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. minkbear/adldap2-laravel

ActiveProject[Authentication &amp; Authorization](/categories/authentication)

minkbear/adldap2-laravel
========================

Adldap2 for Laravel 5.

v2.0.6(10y ago)01791MITPHPPHP &gt;=5.5.0

Since Aug 11Pushed 9y ago1 watchersCompare

[ Source](https://github.com/minkbear/Adldap2-Laravel)[ Packagist](https://packagist.org/packages/minkbear/adldap2-laravel)[ RSS](/packages/minkbear-adldap2-laravel/feed)WikiDiscussions master Synced today

READMEChangelog (3)Dependencies (3)Versions (44)Used By (0)

Adldap2 - Laravel
=================

[](#adldap2---laravel)

[![Build Status](https://camo.githubusercontent.com/be9278c3b7f9304baf72393f567e7a1cec86a53995d3d166c51e428c960eaa26/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f41646c646170322f41646c646170322d4c61726176656c2e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/Adldap2/Adldap2-Laravel)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/c16fd4384873ab035b12f62c0fa85a89da01ca2fc59aba1af85e0da6e0bddd6e/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f41646c646170322f41646c646170322d6c61726176656c2f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/Adldap2/Adldap2-laravel/?branch=master)[![Total Downloads](https://camo.githubusercontent.com/e70858a2f1205e1b5b0ab2b311077685d38e0d312489a1796cc701d3e8054481/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f61646c646170322f61646c646170322d6c61726176656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/adldap2/adldap2-laravel)[![Latest Stable Version](https://camo.githubusercontent.com/67c3f399830534385557e966d15ea1e1040f2caeaed966e2443e77609b93dcfe/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f61646c646170322f61646c646170322d6c61726176656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/adldap2/adldap2-laravel)[![License](https://camo.githubusercontent.com/5e802d48dc4abf3619c15054100370e1e07bc2ca83565abba28df9cd12c9f780/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f61646c646170322f61646c646170322d6c61726176656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/adldap2/adldap2-laravel)

Description
-----------

[](#description)

Adldap2 - Laravel allows easy configuration, access, and management to active directory utilizing the root [Adldap2 Repository](http://www.github.com/Adldap2/Adldap2).

It includes:

- 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
------------

[](#installation)

[Quick Start - From Scratch](quick-start.md)

Insert Adldap2-Laravel into your `composer.json` file:

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

Then run `composer update`.

Once finished, insert the service provider in your `config/app.php` file:

```
Adldap\Laravel\AdldapServiceProvider::class,
```

Then insert the facade:

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

Publish the configuration file by running:

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

Now you're all set!

Usage
-----

[](#usage)

You can perform all methods on Adldap through its facade like so:

```
// 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:

```
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](http://github.com/Adldap2/Adldap2);

Auth Driver
-----------

[](#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.

> **Note**: The Adldap auth driver actually extends from and utilizes Laravel's eloquent auth driver.

### Installation

[](#installation-1)

#### Laravel 5.1

[](#laravel-51)

Insert the `AdldapAuthServiceProvider` into your `config/app.php` file:

```
Adldap\Laravel\AdldapAuthServiceProvider::class,
```

Publish the auth configuration:

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

Change the auth driver in `config/auth.php` to `adldap`:

```
/*
|--------------------------------------------------------------------------
| 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

[](#laravel-52)

Insert the `AdldapAuthServiceProvider` into your `config/app.php` file:

```
Adldap\Laravel\AdldapAuthServiceProvider::class,
```

Publish the auth configuration:

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

Open your `config/auth.php` configuration file and change the following:

Change the `provider` entry inside the `web` authentication guard:

```
/*
|--------------------------------------------------------------------------
| 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:

```
/*
|--------------------------------------------------------------------------
| 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

[](#usage-1)

#### Username Attributes

[](#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.

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

In your login form, change the username form input name to your configured input name.

By default this is set to `email`:

```

```

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

```
protected $username = 'email';
```

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

```

```

> **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.

Inside `config/adldap_auth.php`

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

> **Note**: The actual authentication is done with the `login_attribute` inside your `config/adldap_auth.php` file.

#### Logging In

[](#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`.

#### Synchronizing Attributes

[](#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.

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**.

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

##### Sync Attribute Callbacks

[](#sync-attribute-callbacks)

> **Note**: This feature was introduced in `v1.3.8`.

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:

```
'sync_attributes' => [

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

],
```

The `LdapAttributeHandler` class:

```
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.

#### Binding the Adldap User Model to the Laravel User Model

[](#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).

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:

```
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.

```
