From 3cf8cfb1aa9e39b63ccfeb6d744e493aa6c85ae5 Mon Sep 17 00:00:00 2001 From: dsdevbe Date: Sun, 22 Feb 2015 03:16:14 +0100 Subject: [PATCH] Code refactoring --- README.md | 21 +++---- .../LdapConnector/LdapUserProvider.php | 62 +++---------------- 2 files changed, 17 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index 1617c25..186513b 100755 --- a/README.md +++ b/README.md @@ -16,32 +16,29 @@ Provides an solution for authentication users with LDAP for Laravel 5.0.x. It us ```php 'driver' => 'ldap', ``` -1. 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 [ADLAP documentation](http://adldap.sourceforge.net/wiki/doku.php?id=documentation_configuration) - ```php - "@domain.local", - 'domain_controllers' => array("192.168.0.1", "dc02.domain.local"), // Load balancing domain controllers - 'base_dn' => 'DC=domain,DC=local', + 'account_suffix'=> "@domain.local", + 'domain_controllers'=> array("192.168.0.1", "dc02.domain.local"), // Load balancing domain controllers + 'base_dn' => 'DC=domain,DC=local', ); ``` 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. - ```php + ``` 'Dsdevbe\LdapConnector\LdapConnectorServiceProvider' ``` ## Usage The LDAP plugin is an extension of the AUTH class and will act the same as normal usage with Eloquent driver. - ```php + ``` if (Auth::attempt(array('username' => $email, 'password' => $password))) { return Redirect::intended('dashboard'); } ``` -You can find more examples on [Laravel Auth Documentation](http://laravel.com/docs/security#authenticating-users) on using the `Auth::` function. +You can find more examples on [Laravel Auth Documentation](http://laravel.com/docs/master/authentication) on using the `Auth::` function. diff --git a/src/Dsdevbe/LdapConnector/LdapUserProvider.php b/src/Dsdevbe/LdapConnector/LdapUserProvider.php index 1a4f46d..d20d257 100755 --- a/src/Dsdevbe/LdapConnector/LdapUserProvider.php +++ b/src/Dsdevbe/LdapConnector/LdapUserProvider.php @@ -1,21 +1,11 @@ config = $config; - $this->connectLdap(); + $this->adldap = new adLDAP($config); } /** @@ -43,11 +32,7 @@ public function __construct($config) */ public function retrieveById($identifier) { - $info = $this->adldap->user()->infoCollection($identifier); - if($info) - { - return new LdapUser($this->mapCollectionToArray($info)); - } + // TODO: Implement retrieveById() method. } /** @@ -79,12 +64,13 @@ public function updateRememberToken(Authenticatable $user, $token) */ public function retrieveByCredentials(array $credentials) { - if($userInfo = $this->adldap->user()->info($credentials['username'])) - { - $userInfo = $userInfo[0]; - foreach($userInfo as $u=>$a){ - $credentials[$u]=$a[0]; + if ($this->adldap->authenticate($credentials['username'], $credentials['password'])) { + $userInfo = $this->adldap->user()->info($credentials['username'], array('*'))[0]; + + foreach($userInfo as $key => $value){ + $credentials[$key] = $value[0]; } + return new LdapUser($credentials); } } @@ -97,36 +83,4 @@ public function validateCredentials(Authenticatable $user, array $credentials) return $this->adldap->authenticate($username, $password); } - /** - * @param adLDAPUserCollection $collection - * @return array - */ - public function mapCollectionToArray(adLDAPUserCollection $collection) - { - $arr = array( - 'username' => $collection->samaccountname, - 'displayname' => $collection->displayname, - 'email' => $collection->mail, - 'memberof' => $collection->memberof - ); - - return $arr; - } - - /** - * Connect to LDAP - * - * @throws \Exception - */ - public function connectLdap() - { - try - { - $this->adldap = new adLDAP($this->config); - } catch(adLDAPException $e) - { - throw new Exception($e->getMessage()); - } - } - } \ No newline at end of file