t3chn0crat/laravel-ldap-connector

Authenticate with and fecth data from LDAP in Laravel

0.2-Alpha 2015-05-30 20:09 UTC

This package is not auto-updated.

Last update: 2024-11-23 18:18:25 UTC


README

travis build

Package

This package is a fork of dsdevbe's package (https://github.com/dsdevbe/ldap-connector).

This package will allow you to authenticate to and fetch data from LDAP using Laravel 5.0.x.

It uses adLDAP library to create a bridge between Laravel and LDAP. adLDAP requires PHP 5 and both the LDAP and SSL libraries

Installation

  1. Install this package through Composer for Laravel v5.0:

    composer require T3chn0crat/laravel-ldap-connector:dev-master
  2. Change the authentication driver in the Laravel config to use the ldap driver. You can find this in the following file config/auth.php

    'driver' => 'ldap',
  3. The config/auth.php must also have a valid model set. That model must include

    public function getAuthIdentifier()
    {
        if (isset($this->ldap)) {
            return $this->ldap->samaccountname;
        }
    }
  4. 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 adLDAP documentation

All of these are required

return [
	'account_suffix'=>  "@domain.local",
    // Load balancing domain controllers, but only one is requried
	'domain_controllers'=>  [
        "192.168.0.1", 
        "dc02.domain.local"
    ],
	'base_dn'   =>  'DC=domain,DC=local',
    // AD attributes to get http://msdn.microsoft.com/en-us/library/windows/desktop/ms675090%28v=vs.85%29.aspx
    'fields' => [
        'company',
        'department',
        'displayname',
        'homephone',
        'mail',
        'memberof',
        'mobile',
        'primarygroupid',
        'samaccountname',
        'telephonenumber',
        'title',
    ]
];
  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.

    'T3chn0crat\LdapConnector\LdapConnectorServiceProvider'

Usage

Authentication

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');
}

You can find more examples on Laravel Auth Documentation on using the Auth:: function.

Getting LDAP fields

All the LDAP fields are stored in the Auth::user()->ldap object as public properties.

Email: {{ Auth::user()->ldap->mail }}
Department {{ Auth::user()->ldap->department }}

LdapUserObject Methods

  1. isMemberOf($group)

Will test a user to see if they are a member of the passed in group. Returns a bool

if (Auth::user()->ldap->isMemberOf('Git Hub Users')) { return 'yes'; }

Getting all users

You can use the LdapService object and getAllUsersWithFields to return a Laravel Collection of LdapUserObjects.

$ldap = App::make('T3chn0crat\LdapConnector\LdapService', [Config::get('ldap')]);
$collection = $ldap->getAllUsersWithFields();

You can now apply all the collection function to it. The results will be a collection of LdapUserObjects

$test = $collection->where('mail', 'test@foo.com');
$department = $test->department;