LDAP authentication in Laravel with Adldap2-Laravel

Adldap2-Laravel is an extension to the core Adldap2 package.

Features

Up and running in minutes. Effortlessly connect to your LDAP servers and start running queries & operations in a matter of minutes.

Fluent query builder. Building LDAP queries has never been so easy. Find the records you're looking for in a couple lines or less with a fluent interface.

Supercharged Active Record. Create and modify LDAP records with ease. All LDAP records are individual models. Simply modify the attributes on the model and save it to persist the changes to your LDAP server.

 

Install

Run this command to install the Adldap2-Laravel package:

composer require adldap2/adldap2-laravel

 

Publish the configuration file using:

php artisan vendor:publish --provider="Adldap\Laravel\AdldapServiceProvider"

 

Configure your LDAP connection in the published ldap.php file.

Usage

use the Adldap\Laravel\Facades\Adldap facade:

use Adldap\Laravel\Facades\Adldap; 

// Finding a user:

$user = Adldap::search()->users()->find('john doe');

// Searching for a user:

$search = Adldap::search()->where('cn', '=', 'John Doe')->get();

// Running an operation under a different connection:

$users = Adldap::getProvider('other-connection')->search()->users()->get();

// Creating a user: $user = Adldap::make()->user([ 'cn' => 'John Doe', ]);

// Modifying Attributes:

$user->cn = 'Jane Doe';

// Saving a user:

$user->save();

 

Or inject the Adldap\AdldapInterface:

use Adldap\AdldapInterface;

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

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

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

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

    /**
     * Displays the specified LDAP user.
     *
     * @return \Illuminate\View\View
     */
    public function show($id)
    {
        $user = $this->ldap->search()->findByGuid($id);

        return view('users.show', compact('user'));
    }
}

 

Thanks for reading this post,for a complete documentation,check this link .

For the Github doc ,you can check here .

Tags :