Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions apps/user_ldap/group_ldap.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ private function _getGroupDNsFromMemberOf($DN, &$seen = null) {
$subGroups = $this->_getGroupDNsFromMemberOf($group, $seen);
$allGroups = array_merge($allGroups, $subGroups);
}
}
}
return $allGroups;
}

Expand Down Expand Up @@ -240,7 +240,7 @@ public function primaryGroupID2Name($gid, $dn) {
if(empty($result)) {
return false;
}
$dn = $result[0];
$dn = $result[0]['dn'][0];

//and now the group name
//NOTE once we have separate ownCloud group IDs and group names we can
Expand Down Expand Up @@ -484,7 +484,7 @@ private function getGroupsByMember($dn, &$seen = null) {
array($this->access->connection->ldapGroupDisplayName, 'dn'));
if (is_array($groups)) {
foreach ($groups as $groupobj) {
$groupDN = $groupobj['dn'];
$groupDN = $groupobj['dn'][0];
$allGroups[$groupDN] = $groupobj;
$nestedGroups = $this->access->connection->ldapNestedGroups;
if (!empty($nestedGroups)) {
Expand Down Expand Up @@ -648,7 +648,7 @@ public function countUsersInGroup($gid, $search = '') {
$this->access->connection->ldapLoginFilter, 'UTF-8'),
$this->access->getFilterPartForUserSearch($search)
));
$ldap_users = $this->access->fetchListOfUsers($filter, 'dn');
$ldap_users = $this->access->fetchListOfUsers($filter, 'dn', 1);
if(count($ldap_users) < 1) {
continue;
}
Expand Down
109 changes: 74 additions & 35 deletions apps/user_ldap/lib/access.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ public function dn2ocname($fdn, $ldapName = null, $isUser = true) {

/**
* gives back the user names as they are used ownClod internally
* @param array $ldapUsers an array with the ldap Users result in style of array ( array ('dn' => foo, 'uid' => bar), ... )
* @param array $ldapUsers as returned by fetchList()
* @return array an array with the user names to use in ownCloud
*
* gives back the user names as they are used ownClod internally
Expand All @@ -497,7 +497,7 @@ public function ownCloudUserNames($ldapUsers) {

/**
* gives back the group names as they are used ownClod internally
* @param array $ldapGroups an array with the ldap Groups result in style of array ( array ('dn' => foo, 'cn' => bar), ... )
* @param array $ldapGroups as returned by fetchList()
* @return array an array with the group names to use in ownCloud
*
* gives back the group names as they are used ownClod internally
Expand All @@ -507,7 +507,7 @@ public function ownCloudGroupNames($ldapGroups) {
}

/**
* @param array $ldapObjects
* @param array $ldapObjects as returned by fetchList()
* @param bool $isUsers
* @return array
*/
Expand All @@ -520,22 +520,42 @@ private function ldap2ownCloudNames($ldapObjects, $isUsers) {
$ownCloudNames = array();

foreach($ldapObjects as $ldapObject) {
$nameByLDAP = isset($ldapObject[$nameAttribute]) ? $ldapObject[$nameAttribute] : null;
$ocName = $this->dn2ocname($ldapObject['dn'], $nameByLDAP, $isUsers);
$nameByLDAP = null;
if( isset($ldapObject[$nameAttribute])
&& is_array($ldapObject[$nameAttribute])
&& isset($ldapObject[$nameAttribute][0])
) {
// might be set, but not necessarily. if so, we use it.
$nameByLDAP = $ldapObject[$nameAttribute][0];
}

$ocName = $this->dn2ocname($ldapObject['dn'][0], $nameByLDAP, $isUsers);
if($ocName) {
$ownCloudNames[] = $ocName;
if($isUsers) {
//cache the user names so it does not need to be retrieved
//again later (e.g. sharing dialogue).
$this->cacheUserExists($ocName);
$this->cacheUserDisplayName($ocName, $nameByLDAP);
if(!is_null($nameByLDAP)) {
$this->cacheUserDisplayName($ocName, $nameByLDAP);
}
}
}
continue;
}
return $ownCloudNames;
}

/**
* caches the user display name
* @param string $ocName the internal ownCloud username
* @param string|false $home the home directory path
*/
public function cacheUserHome($ocName, $home) {
$cacheKey = 'getHome'.$ocName;
$this->connection->writeToCache($cacheKey, $home);
}

/**
* caches a user as existing
* @param string $ocName the internal ownCloud username
Expand Down Expand Up @@ -657,7 +677,36 @@ public function fetchUsersByLoginName($loginName, $attributes = array('dn')) {
* @return array
*/
public function fetchListOfUsers($filter, $attr, $limit = null, $offset = null) {
return $this->fetchList($this->searchUsers($filter, $attr, $limit, $offset), (count($attr) > 1));
$ldapRecords = $this->searchUsers($filter, $attr, $limit, $offset);
$this->batchApplyUserAttributes($ldapRecords);
return $this->fetchList($ldapRecords, (count($attr) > 1));
}

/**
* provided with an array of LDAP user records the method will fetch the
* user object and requests it to process the freshly fetched attributes and
* and their values
* @param array $ldapRecords
*/
public function batchApplyUserAttributes(array $ldapRecords){
$displayNameAttribute = strtolower($this->connection->ldapUserDisplayName);
foreach($ldapRecords as $userRecord) {
if(!isset($userRecord[$displayNameAttribute])) {
// displayName is obligatory
continue;
}

$ocName = $this->dn2ocname($userRecord['dn'][0], $userRecord[$displayNameAttribute]);
if(!$ocName) {
// no user name, skip.
continue;
}
$this->cacheUserExists($ocName);
$user = $this->userManager->get($ocName);
if(!is_null($user)) {
$user->processAttributes($userRecord);
}
}
}

/**
Expand All @@ -681,6 +730,11 @@ private function fetchList($list, $manyAttributes) {
if($manyAttributes) {
return $list;
} else {
$list = array_reduce($list, function($carry, $item) {
$attribute = array_keys($item)[0];
$carry[] = $item[$attribute][0];
return $carry;
}, array());
return array_unique($list, SORT_LOCALE_STRING);
}
}
Expand Down Expand Up @@ -953,44 +1007,29 @@ private function search($filter, $base, $attr = null, $limit = null, $offset = n

if(!is_null($attr)) {
$selection = array();
$multiArray = false;
if(count($attr) > 1) {
$multiArray = true;
$i = 0;
}
$i = 0;
foreach($findings as $item) {
if(!is_array($item)) {
continue;
}
$item = \OCP\Util::mb_array_change_key_case($item, MB_CASE_LOWER, 'UTF-8');

if($multiArray) {
foreach($attr as $key) {
$key = mb_strtolower($key, 'UTF-8');
if(isset($item[$key])) {
if($key !== 'dn') {
$selection[$i][$key] = $this->resemblesDN($key) ?
$this->sanitizeDN($item[$key][0])
: $item[$key][0];
} else {
$selection[$i][$key] = $this->sanitizeDN($item[$key]);
}
}

}
$i++;
} else {
//tribute to case insensitivity
$key = mb_strtolower($attr[0], 'UTF-8');

foreach($attr as $key) {
$key = mb_strtolower($key, 'UTF-8');
if(isset($item[$key])) {
if($this->resemblesDN($key)) {
$selection[] = $this->sanitizeDN($item[$key]);
if(is_array($item[$key]) && isset($item[$key]['count'])) {
unset($item[$key]['count']);
}
if($key !== 'dn') {
$selection[$i][$key] = $this->resemblesDN($key) ?
$this->sanitizeDN($item[$key])
: $item[$key];
} else {
$selection[] = $item[$key];
$selection[$i][$key] = [$this->sanitizeDN($item[$key])];
}
}

}
$i++;
}
$findings = $selection;
}
Expand Down
37 changes: 37 additions & 0 deletions apps/user_ldap/lib/user/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,43 @@ private function checkAccess() {
}
}

/**
* returns a list of attributes that will be processed further, e.g. quota,
* email, displayname, or others.
* @param bool $minimal - optional, set to true to skip attributes with big
* payload
* @return string[]
*/
public function getAttributes($minimal = false) {
$attributes = array('dn', 'uid', 'samaccountname', 'memberof');
$possible = array(
$this->access->getConnection()->ldapQuotaAttribute,
$this->access->getConnection()->ldapEmailAttribute,
$this->access->getConnection()->ldapUserDisplayName,
);
foreach($possible as $attr) {
if(!is_null($attr)) {
$attributes[] = $attr;
}
}

$homeRule = $this->access->getConnection()->homeFolderNamingRule;
if(strpos($homeRule, 'attr:') === 0) {
$attributes[] = substr($homeRule, strlen('attr:'));
}

if(!$minimal) {
// attributes that are not really important but may come with big
// payload.
$attributes = array_merge($attributes, array(
'jpegphoto',
'thumbnailphoto'
));
}

return $attributes;
}

/**
* Checks whether the specified user is marked as deleted
* @param string $id the ownCloud user name
Expand Down
Loading