diff --git a/README.md b/README.md index 3d79e63..f5475a7 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 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 36572f6..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,13 +14,19 @@ class Adldap implements LdapInterface protected $_password; - protected function mapDataToUserModel($username, array $groups) + protected function mapDataToUserModel(adLDAPUserCollection $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; } @@ -30,8 +37,8 @@ public function __construct($config) } /** - * @param String $username - * @param String $password + * @param string $username + * @param string $password * * @return bool */ @@ -48,17 +55,17 @@ public function connect($username, $password) */ public function isConnected() { - return !!$this->_ldap->getLdapBind(); + return (bool) $this->_ldap->getLdapBind(); } /** - * @param String $username + * @param string $username * * @return UserModel */ public function getUserInfo($username) { - $user = $this->_ldap->user()->info($username); + $user = $this->_ldap->user()->infoCollection($username, ['samaccountname', 'givenname', 'sn', 'mail']); if (!$user) { return; @@ -66,6 +73,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/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 c73d872..22b7e16 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; @@ -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,51 @@ 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['email']; + } }