From 94a970bf181a825db710f2706365c7948ff4de39 Mon Sep 17 00:00:00 2001 From: codybuell Date: Wed, 9 Dec 2015 16:30:19 -0500 Subject: [PATCH 1/3] Provide additional user information Grab some additional user information from adldap and provide it through the user model. Specifically username, first name, last name, and email address. --- README.md | 5 ++ src/Dsdevbe/LdapConnector/Adapter/Adldap.php | 14 ++++-- src/Dsdevbe/LdapConnector/Model/User.php | 49 ++++++++++++++++++++ 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3d79e63..a17eccd 100644 --- a/README.md +++ b/README.md @@ -68,3 +68,8 @@ Laravel documentation: [Authentication Quickstart](http://laravel.com/docs/maste - `Auth::user()->getGroups()` returns `array` with groups the current user belongs to. - `Auth::user()->inGroup('GROUPNAME')` returns `boolean` if user belongs to `GROUPNAME` +### Ldap User Information +- `Auth::user()->getUsername()` returns authenticated users username. +- `Auth::user()->getFirstname()` returns authenticated users first name. +- `Auth::user()->getLastname()` returns authenticated users last name. +- `Auth::user()->getEmail()` returns authenticated users email address. diff --git a/src/Dsdevbe/LdapConnector/Adapter/Adldap.php b/src/Dsdevbe/LdapConnector/Adapter/Adldap.php index 36572f6..acba528 100644 --- a/src/Dsdevbe/LdapConnector/Adapter/Adldap.php +++ b/src/Dsdevbe/LdapConnector/Adapter/Adldap.php @@ -13,13 +13,19 @@ class Adldap implements LdapInterface protected $_password; - protected function mapDataToUserModel($username, array $groups) + protected function mapDataToUserModel($user, array $groups) { $model = new UserModel([ - 'username' => $username, + 'username' => $user->samaccountname, 'password' => $this->_password, ]); $model->setGroups($groups); + $model->setUserInfo([ + 'username' => $user->samaccountname, + 'firstname' => $user->givenname, + 'lastname' => $user->sn, + 'email' => $user->mail, + ]); return $model; } @@ -58,7 +64,7 @@ public function isConnected() */ public function getUserInfo($username) { - $user = $this->_ldap->user()->info($username); + $user = $this->_ldap->user()->infoCollection($username, array('samaccountname','givenname','sn','mail')); if (!$user) { return; @@ -66,6 +72,6 @@ public function getUserInfo($username) $groups = $this->_ldap->user()->groups($username); - return $this->mapDataToUserModel($username, $groups); + return $this->mapDataToUserModel($user, $groups); } } diff --git a/src/Dsdevbe/LdapConnector/Model/User.php b/src/Dsdevbe/LdapConnector/Model/User.php index c73d872..6949a71 100644 --- a/src/Dsdevbe/LdapConnector/Model/User.php +++ b/src/Dsdevbe/LdapConnector/Model/User.php @@ -26,6 +26,11 @@ class User implements Authenticatable */ protected $_groups; + /** + * @var array + */ + protected $_user; + public function __construct(array $attributes) { $this->_authIdentifier = $attributes['username']; @@ -95,8 +100,52 @@ public function setGroups(array $groups) $this->_groups = $groups; } + /** + * @return bool + */ public function inGroup($groupName) { return in_array($groupName, $this->_groups); } + + /** + * @param array $user + */ + public function setUserInfo(array $user) + { + $this->_user = $user; + } + + /** + * @return string + */ + public function getUsername() + { + return $this->_user['username']; + } + + /** + * @return string + */ + public function getFirstname() + { + return $this->_user['firstname']; + } + + /** + * @return string + */ + public function getLastname() + { + return $this->_user['lastname']; + } + + /** + * @return string + */ + public function getEmail() + { + return $this->_user['mail']; + } + } From 49d5d99a66bd077347c4e287549c66feadc60378 Mon Sep 17 00:00:00 2001 From: codybuell Date: Wed, 9 Dec 2015 16:59:39 -0500 Subject: [PATCH 2/3] Correct getEmail method --- src/Dsdevbe/LdapConnector/Adapter/Adldap.php | 8 ++++---- src/Dsdevbe/LdapConnector/Model/User.php | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Dsdevbe/LdapConnector/Adapter/Adldap.php b/src/Dsdevbe/LdapConnector/Adapter/Adldap.php index acba528..3619d69 100644 --- a/src/Dsdevbe/LdapConnector/Adapter/Adldap.php +++ b/src/Dsdevbe/LdapConnector/Adapter/Adldap.php @@ -36,8 +36,8 @@ public function __construct($config) } /** - * @param String $username - * @param String $password + * @param string $username + * @param string $password * * @return bool */ @@ -58,13 +58,13 @@ public function isConnected() } /** - * @param String $username + * @param string $username * * @return UserModel */ public function getUserInfo($username) { - $user = $this->_ldap->user()->infoCollection($username, array('samaccountname','givenname','sn','mail')); + $user = $this->_ldap->user()->infoCollection($username, ['samaccountname','givenname','sn','mail']); if (!$user) { return; diff --git a/src/Dsdevbe/LdapConnector/Model/User.php b/src/Dsdevbe/LdapConnector/Model/User.php index 6949a71..e0f61e8 100644 --- a/src/Dsdevbe/LdapConnector/Model/User.php +++ b/src/Dsdevbe/LdapConnector/Model/User.php @@ -7,17 +7,17 @@ class User implements Authenticatable { /** - * @var String + * @var string */ protected $_authIdentifier; /** - * @var String + * @var string */ protected $_authPassword; /** - * @var String + * @var string */ protected $_rememberToken; @@ -145,7 +145,7 @@ public function getLastname() */ public function getEmail() { - return $this->_user['mail']; + return $this->_user['email']; } } From b09c7443f78710a3407590794ae569d05540495e Mon Sep 17 00:00:00 2001 From: SaschaDens Date: Sat, 12 Dec 2015 09:33:59 +0100 Subject: [PATCH 3/3] Added some minor changes to PR #26 --- README.md | 8 ++++---- src/Dsdevbe/LdapConnector/Adapter/Adldap.php | 7 ++++--- src/Dsdevbe/LdapConnector/Adapter/LdapInterface.php | 4 ++-- .../LdapConnector/LdapConnectorServiceProvider.php | 4 ++-- src/Dsdevbe/LdapConnector/Model/User.php | 1 - 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index a17eccd..f5475a7 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ Laravel documentation: [Authentication Quickstart](http://laravel.com/docs/maste - `Auth::user()->inGroup('GROUPNAME')` returns `boolean` if user belongs to `GROUPNAME` ### Ldap User Information -- `Auth::user()->getUsername()` returns authenticated users username. -- `Auth::user()->getFirstname()` returns authenticated users first name. -- `Auth::user()->getLastname()` returns authenticated users last name. -- `Auth::user()->getEmail()` returns authenticated users email address. +- `Auth::user()->getUsername()` returns authenticated username. +- `Auth::user()->getFirstname()` returns authenticated first name. +- `Auth::user()->getLastname()` returns authenticated last name. +- `Auth::user()->getEmail()` returns authenticated email address. diff --git a/src/Dsdevbe/LdapConnector/Adapter/Adldap.php b/src/Dsdevbe/LdapConnector/Adapter/Adldap.php index 3619d69..0aef216 100644 --- a/src/Dsdevbe/LdapConnector/Adapter/Adldap.php +++ b/src/Dsdevbe/LdapConnector/Adapter/Adldap.php @@ -3,6 +3,7 @@ namespace Dsdevbe\LdapConnector\Adapter; use adLDAP\adLDAP as adLDAPService; +use adLDAP\collections\adLDAPUserCollection as adLDAPUserCollection; use Dsdevbe\LdapConnector\Model\User as UserModel; class Adldap implements LdapInterface @@ -13,7 +14,7 @@ class Adldap implements LdapInterface protected $_password; - protected function mapDataToUserModel($user, array $groups) + protected function mapDataToUserModel(adLDAPUserCollection $user, array $groups) { $model = new UserModel([ 'username' => $user->samaccountname, @@ -54,7 +55,7 @@ public function connect($username, $password) */ public function isConnected() { - return !!$this->_ldap->getLdapBind(); + return (bool) $this->_ldap->getLdapBind(); } /** @@ -64,7 +65,7 @@ public function isConnected() */ public function getUserInfo($username) { - $user = $this->_ldap->user()->infoCollection($username, ['samaccountname','givenname','sn','mail']); + $user = $this->_ldap->user()->infoCollection($username, ['samaccountname', 'givenname', 'sn', 'mail']); if (!$user) { return; diff --git a/src/Dsdevbe/LdapConnector/Adapter/LdapInterface.php b/src/Dsdevbe/LdapConnector/Adapter/LdapInterface.php index 93ee360..efe8858 100644 --- a/src/Dsdevbe/LdapConnector/Adapter/LdapInterface.php +++ b/src/Dsdevbe/LdapConnector/Adapter/LdapInterface.php @@ -7,8 +7,8 @@ interface LdapInterface { /** - * @param String $username - * @param String $password + * @param string $username + * @param string $password * * @return bool */ diff --git a/src/Dsdevbe/LdapConnector/LdapConnectorServiceProvider.php b/src/Dsdevbe/LdapConnector/LdapConnectorServiceProvider.php index ba15791..62c911d 100644 --- a/src/Dsdevbe/LdapConnector/LdapConnectorServiceProvider.php +++ b/src/Dsdevbe/LdapConnector/LdapConnectorServiceProvider.php @@ -23,7 +23,7 @@ class LdapConnectorServiceProvider extends ServiceProvider */ public function boot() { - Auth::extend('ldap', function ($app) { + Auth::extend('ldap', function($app) { $ldap = new Adldap( $this->getLdapAdapterConfig('adldap') ); @@ -40,7 +40,7 @@ public function boot() */ public function register() { - $ldapConfig = __DIR__.'/Config/ldap.php'; + $ldapConfig = __DIR__ . '/Config/ldap.php'; $this->publishConfig($ldapConfig); } diff --git a/src/Dsdevbe/LdapConnector/Model/User.php b/src/Dsdevbe/LdapConnector/Model/User.php index e0f61e8..22b7e16 100644 --- a/src/Dsdevbe/LdapConnector/Model/User.php +++ b/src/Dsdevbe/LdapConnector/Model/User.php @@ -147,5 +147,4 @@ public function getEmail() { return $this->_user['email']; } - }