Skip to content

Commit

Permalink
Unify a bit the types of the fetcher
Browse files Browse the repository at this point in the history
Now it will only accept a string as parameter instead of either a string
(DN) or a array (complete record).

Signed-off-by: Carl Schwan <carl@carlschwan.eu>
  • Loading branch information
CarlSchwan committed Dec 12, 2021
1 parent 424d9dc commit 26b2f92
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions apps/user_ldap/lib/Group_LDAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Group_LDAP extends BackendUtility implements GroupInterface, IGroupLDAP, I

/** @var string[][] $cachedGroupMembers array of users with gid as key */
protected $cachedGroupMembers;
/** @var string[] $cachedGroupsByMember array of groups with uid as key */
/** @var string[][] $cachedGroupsByMember array of groups with uid as key */
protected $cachedGroupsByMember;
/** @var string[] $cachedNestedGroups array of groups with gid (DN) as key */
protected $cachedNestedGroups;
Expand Down Expand Up @@ -304,7 +304,7 @@ private function _groupMembers(string $dnGroup, ?array &$seen = null): array {
$rawMemberReads[$dnGroup] = $members;
}
if (is_array($members)) {
$fetcher = function ($memberDN) use (&$seen) {
$fetcher = function (string $memberDN) use (&$seen) {
return $this->_groupMembers($memberDN, $seen);
};
$allMembers = $this->walkNestedGroupsReturnDNs($dnGroup, $fetcher, $members, $seen);
Expand Down Expand Up @@ -342,7 +342,7 @@ private function _getGroupDNsFromMemberOf(string $dn): array {
return [];
}

$fetcher = function ($groupDN) {
$fetcher = function (string $groupDN) {
if (isset($this->cachedNestedGroups[$groupDN])) {
$nestedGroups = $this->cachedNestedGroups[$groupDN];
} else {
Expand All @@ -362,7 +362,7 @@ private function _getGroupDNsFromMemberOf(string $dn): array {
/**
* @psalm-param list<array{dn: list<string>}|string> $list
* @psalm-param array<string, int|array|string> $seen List of DN that have already been processed.
* @param Closure(string) $fetcher
* @psalm-param Closure(string) $fetcher
*/
private function processListFromWalkingNestedGroups(array &$list, array &$seen, string $dn, Closure $fetcher): void {
while ($record = array_shift($list)) {
Expand All @@ -375,7 +375,7 @@ private function processListFromWalkingNestedGroups(array &$list, array &$seen,
$cacheKey = 'walkNestedGroups_' . $recordDN;
$fetched = $this->access->connection->getFromCache($cacheKey);
if ($fetched === null) {
$fetched = $fetcher($record);
$fetched = $fetcher($recordDN);
$fetched = $this->access->connection->writeToCache($cacheKey, $fetched);
}
$list = array_merge($list, $fetched);
Expand All @@ -388,7 +388,7 @@ private function processListFromWalkingNestedGroups(array &$list, array &$seen,
/**
* @psalm-param list<array{dn: list<string>}|string> $list
* @psalm-param array<string, int|array|string> $seen List of DN that have already been processed.
* @param Closure(string) $fetcher
* @psalm-param Closure(string) $fetcher
*/
private function walkNestedGroupsReturnDNs(string $dn, Closure $fetcher, array $list, array &$seen = []): array {
$nesting = (int)$this->access->connection->ldapNestedGroups;
Expand All @@ -404,15 +404,15 @@ private function walkNestedGroupsReturnDNs(string $dn, Closure $fetcher, array $
/**
* @psalm-param list<array{dn: list<string>}> $list
* @psalm-param array<string, int|array|string> $seen List of DN that have already been processed.
* @psalm-param Closure(string) $fetcher
* @return array[] An array of records
* @param Closure(string) $fetcher
*/
private function walkNestedGroupsReturnRecords(string $dn, Closure $fetcher, array $list, array &$seen = []): array {
$nesting = (int)$this->access->connection->ldapNestedGroups;

if ($nesting !== 1) {
// the keys are numeric, but should hold the DN
return array_reduce($list, function ($transformed, $record) use ($dn) {
return array_reduce($list, function (array $transformed, array $record) use ($dn) {
if ($record['dn'][0] != $dn) {
$transformed[$record['dn'][0]] = $record;
}
Expand Down Expand Up @@ -893,10 +893,7 @@ private function getGroupsByMember(string $dn, array &$seen = null): array {
$groups = $this->access->fetchListOfGroups($filter,
[strtolower($this->access->connection->ldapGroupMemberAssocAttr), $this->access->connection->ldapGroupDisplayName, 'dn']);
if (is_array($groups)) {
$fetcher = function ($dn) use (&$seen) {
if (is_array($dn) && isset($dn['dn'][0])) {
$dn = $dn['dn'][0];
}
$fetcher = function (string $dn) use (&$seen) {
return $this->getGroupsByMember($dn, $seen);
};

Expand Down Expand Up @@ -1215,7 +1212,7 @@ protected function filterValidGroups(array $listOfGroups): array {
$validGroupDNs = [];
foreach ($listOfGroups as $key => $item) {
$dn = is_string($item) ? $item : $item['dn'][0];
if(is_array($item) && !isset($item[$this->access->connection->ldapGroupDisplayName][0])) {
if (is_array($item) && !isset($item[$this->access->connection->ldapGroupDisplayName][0])) {
continue;
}
$name = $item[$this->access->connection->ldapGroupDisplayName][0] ?? null;
Expand Down

0 comments on commit 26b2f92

Please sign in to comment.