Skip to content

Commit 64fd1b9

Browse files
authored
Merge pull request #18748 from nextcloud/backport/18740/stable16
[stable16] cache group existence early to save useless requests to LDAP
2 parents 62b4de0 + 1fa8fc7 commit 64fd1b9

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

apps/user_ldap/lib/Access.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ public function extractRangeData($result, $attribute) {
345345
}
346346
return [];
347347
}
348-
348+
349349
/**
350350
* Set password for an LDAP user identified by a DN
351351
*
@@ -646,6 +646,8 @@ public function mapAndAnnounceIfApplicable(
646646
if ($this->ncUserManager instanceof PublicEmitter && $isUser) {
647647
$this->cacheUserExists($name);
648648
$this->ncUserManager->emit('\OC\User', 'assignedUserId', [$name]);
649+
} elseif (!$isUser) {
650+
$this->cacheGroupExists($name);
649651
}
650652
return true;
651653
}
@@ -749,6 +751,13 @@ public function cacheUserExists($ocName) {
749751
$this->connection->writeToCache('userExists'.$ocName, true);
750752
}
751753

754+
/**
755+
* caches a group as existing
756+
*/
757+
public function cacheGroupExists(string $gid): void {
758+
$this->connection->writeToCache('groupExists'.$gid, true);
759+
}
760+
752761
/**
753762
* caches the user display name
754763
* @param string $ocName the internal Nextcloud username
@@ -938,7 +947,15 @@ public function batchApplyUserAttributes(array $ldapRecords){
938947
* @return array
939948
*/
940949
public function fetchListOfGroups($filter, $attr, $limit = null, $offset = null) {
941-
return $this->fetchList($this->searchGroups($filter, $attr, $limit, $offset), $this->manyAttributes($attr));
950+
$groupRecords = $this->searchGroups($filter, $attr, $limit, $offset);
951+
array_walk($groupRecords, function($record) {
952+
$newlyMapped = false;
953+
$gid = $this->dn2ocname($record['dn'][0], null, false, $newlyMapped, $record);
954+
if(!$newlyMapped && is_string($gid)) {
955+
$this->cacheGroupExists($gid);
956+
}
957+
});
958+
return $this->fetchList($groupRecords, $this->manyAttributes($attr));
942959
}
943960

944961
/**

apps/user_ldap/lib/Group_LDAP.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,7 @@ public function createGroup($gid) {
11461146
$uuid,
11471147
false
11481148
);
1149-
$this->access->connection->writeToCache("groupExists" . $gid, true);
1149+
$this->access->cacheGroupExists($gid);
11501150
}
11511151
}
11521152
return $dn != null;

0 commit comments

Comments
 (0)