Added support for LDAP Posix Groups (OpenLDAP with memberUid)
This commit is contained in:
@@ -22,15 +22,25 @@ class User
|
||||
*/
|
||||
protected $query;
|
||||
|
||||
/**
|
||||
* LDAP Group object
|
||||
*
|
||||
* @access protected
|
||||
* @var Group
|
||||
*/
|
||||
protected $group;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @access public
|
||||
* @param Query $query
|
||||
* @param Query $query
|
||||
* @param Group $group
|
||||
*/
|
||||
public function __construct(Query $query)
|
||||
public function __construct(Query $query, Group $group = null)
|
||||
{
|
||||
$this->query = $query;
|
||||
$this->group = $group;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -44,7 +54,7 @@ class User
|
||||
*/
|
||||
public static function getUser(Client $client, $username)
|
||||
{
|
||||
$self = new static(new Query($client));
|
||||
$self = new static(new Query($client), new Group(new Query($client)));
|
||||
return $self->find($self->getLdapUserPattern($username));
|
||||
}
|
||||
|
||||
@@ -53,7 +63,7 @@ class User
|
||||
*
|
||||
* @access public
|
||||
* @param string $query
|
||||
* @return null|LdapUserProvider
|
||||
* @return LdapUserProvider
|
||||
*/
|
||||
public function find($query)
|
||||
{
|
||||
@@ -67,6 +77,56 @@ class User
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user groupIds (DN)
|
||||
*
|
||||
* 1) If configured, use memberUid and posixGroup
|
||||
* 2) Otherwise, use memberOf
|
||||
*
|
||||
* @access protected
|
||||
* @param Entry $entry
|
||||
* @param string $username
|
||||
* @return string[]
|
||||
*/
|
||||
protected function getGroups(Entry $entry, $username)
|
||||
{
|
||||
$groupIds = array();
|
||||
|
||||
if (! empty($username) && $this->group !== null && $this->hasGroupUserFilter()) {
|
||||
$groups = $this->group->find(sprintf($this->getGroupUserFilter(), $username));
|
||||
|
||||
foreach ($groups as $group) {
|
||||
$groupIds[] = $group->getExternalId();
|
||||
}
|
||||
} else {
|
||||
$groupIds = $entry->getAll($this->getAttributeGroup());
|
||||
}
|
||||
|
||||
return $groupIds;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get role from LDAP groups
|
||||
*
|
||||
* @access protected
|
||||
* @param string[] $groupIds
|
||||
* @return string
|
||||
*/
|
||||
protected function getRole(array $groupIds)
|
||||
{
|
||||
foreach ($groupIds as $groupId) {
|
||||
$groupId = strtolower($groupId);
|
||||
|
||||
if ($groupId === strtolower($this->getGroupAdminDn())) {
|
||||
return Role::APP_ADMIN;
|
||||
} elseif ($groupId === strtolower($this->getGroupManagerDn())) {
|
||||
return Role::APP_MANAGER;
|
||||
}
|
||||
}
|
||||
|
||||
return Role::APP_USER;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build user profile
|
||||
*
|
||||
@@ -76,21 +136,16 @@ class User
|
||||
protected function build()
|
||||
{
|
||||
$entry = $this->query->getEntries()->getFirstEntry();
|
||||
$role = Role::APP_USER;
|
||||
|
||||
if ($entry->hasValue($this->getAttributeGroup(), $this->getGroupAdminDn())) {
|
||||
$role = Role::APP_ADMIN;
|
||||
} elseif ($entry->hasValue($this->getAttributeGroup(), $this->getGroupManagerDn())) {
|
||||
$role = Role::APP_MANAGER;
|
||||
}
|
||||
$username = $entry->getFirstValue($this->getAttributeUsername());
|
||||
$groupIds = $this->getGroups($entry, $username);
|
||||
|
||||
return new LdapUserProvider(
|
||||
$entry->getDn(),
|
||||
$entry->getFirstValue($this->getAttributeUsername()),
|
||||
$username,
|
||||
$entry->getFirstValue($this->getAttributeName()),
|
||||
$entry->getFirstValue($this->getAttributeEmail()),
|
||||
$role,
|
||||
$entry->getAll($this->getAttributeGroup())
|
||||
$this->getRole($groupIds),
|
||||
$groupIds
|
||||
);
|
||||
}
|
||||
|
||||
@@ -124,7 +179,7 @@ class User
|
||||
throw new LogicException('LDAP username attribute empty, check the parameter LDAP_USER_ATTRIBUTE_USERNAME');
|
||||
}
|
||||
|
||||
return LDAP_USER_ATTRIBUTE_USERNAME;
|
||||
return strtolower(LDAP_USER_ATTRIBUTE_USERNAME);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -139,7 +194,7 @@ class User
|
||||
throw new LogicException('LDAP full name attribute empty, check the parameter LDAP_USER_ATTRIBUTE_FULLNAME');
|
||||
}
|
||||
|
||||
return LDAP_USER_ATTRIBUTE_FULLNAME;
|
||||
return strtolower(LDAP_USER_ATTRIBUTE_FULLNAME);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -154,18 +209,40 @@ class User
|
||||
throw new LogicException('LDAP email attribute empty, check the parameter LDAP_USER_ATTRIBUTE_EMAIL');
|
||||
}
|
||||
|
||||
return LDAP_USER_ATTRIBUTE_EMAIL;
|
||||
return strtolower(LDAP_USER_ATTRIBUTE_EMAIL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get LDAP account memberof attribute
|
||||
* Get LDAP account memberOf attribute
|
||||
*
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function getAttributeGroup()
|
||||
{
|
||||
return LDAP_USER_ATTRIBUTE_GROUPS;
|
||||
return strtolower(LDAP_USER_ATTRIBUTE_GROUPS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get LDAP Group User filter
|
||||
*
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function getGroupUserFilter()
|
||||
{
|
||||
return LDAP_GROUP_USER_FILTER;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if LDAP Group User filter is defined
|
||||
*
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function hasGroupUserFilter()
|
||||
{
|
||||
return $this->getGroupUserFilter() !== '' && $this->getGroupUserFilter() !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -176,7 +253,7 @@ class User
|
||||
*/
|
||||
public function getGroupAdminDn()
|
||||
{
|
||||
return LDAP_GROUP_ADMIN_DN;
|
||||
return strtolower(LDAP_GROUP_ADMIN_DN);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user