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
-
Install this package through Composer for Laravel v5.0:
composer require T3chn0crat/laravel-ldap-connector:dev-master
-
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',
-
The
config/auth.php
must also have a valid model set. That model must includepublic function getAuthIdentifier() { if (isset($this->ldap)) { return $this->ldap->samaccountname; } }
-
Create a new configuration file
ldap.php
in the configuration folder of Laravelapp/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',
]
];
-
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'
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.
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 }}
- 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'; }
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;