Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
SaschaDens committed Dec 12, 2015
2 parents 4f45c53 + b09c744 commit 07a393e
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 15 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
23 changes: 15 additions & 8 deletions src/Dsdevbe/LdapConnector/Adapter/Adldap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand All @@ -30,8 +37,8 @@ public function __construct($config)
}

/**
* @param String $username
* @param String $password
* @param string $username
* @param string $password
*
* @return bool
*/
Expand All @@ -48,24 +55,24 @@ 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;
}

$groups = $this->_ldap->user()->groups($username);

return $this->mapDataToUserModel($username, $groups);
return $this->mapDataToUserModel($user, $groups);
}
}
4 changes: 2 additions & 2 deletions src/Dsdevbe/LdapConnector/Adapter/LdapInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
interface LdapInterface
{
/**
* @param String $username
* @param String $password
* @param string $username
* @param string $password
*
* @return bool
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Dsdevbe/LdapConnector/LdapConnectorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
);
Expand All @@ -40,7 +40,7 @@ public function boot()
*/
public function register()
{
$ldapConfig = __DIR__.'/Config/ldap.php';
$ldapConfig = __DIR__ . '/Config/ldap.php';
$this->publishConfig($ldapConfig);
}

Expand Down
54 changes: 51 additions & 3 deletions src/Dsdevbe/LdapConnector/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -26,6 +26,11 @@ class User implements Authenticatable
*/
protected $_groups;

/**
* @var array
*/
protected $_user;

public function __construct(array $attributes)
{
$this->_authIdentifier = $attributes['username'];
Expand Down Expand Up @@ -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'];
}
}

0 comments on commit 07a393e

Please sign in to comment.