Skip to content

Commit

Permalink
Add more type hinting
Browse files Browse the repository at this point in the history
Signed-off-by: Carl Schwan <carl@carlschwan.eu>
  • Loading branch information
CarlSchwan committed Dec 12, 2021
1 parent 81d2ece commit 424d9dc
Showing 1 changed file with 44 additions and 32 deletions.
76 changes: 44 additions & 32 deletions apps/user_ldap/lib/Group_LDAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function __construct(Access $access, GroupPluginManager $groupPluginManag
}

/**
* is user in group?
* Check if user is in group
*
* @param string $uid uid of the user
* @param string $gid gid of the group
Expand Down Expand Up @@ -239,18 +239,21 @@ public function getDynamicGroupMembers(string $dnGroup): array {
}

/**
* Get group members from dn.
* @psalm-param array<string, int|array|string> $seen List of DN that have already been processed.
* @throws ServerNotAvailableException
*/
private function _groupMembers(string $dnGroup, ?array &$seen = null): array {
if ($seen === null) {
$seen = [];
// the root entry has to be marked as processed to avoind infinit loops,
// but not included in the results laters on
// the root entry has to be marked as processed to avoid infinite loops,
// but not included in the results later on
$excludeFromResult = $dnGroup;
}
// cache only base groups, otherwise groups get additional unwarranted members
$shouldCacheResult = count($seen) === 0;

/** @psalm-var array<string, string[]|bool> $rawMemberReads */
static $rawMemberReads = []; // runtime cache for intermediate ldap read results
$allMembers = [];

Expand Down Expand Up @@ -330,6 +333,7 @@ private function _groupMembers(string $dnGroup, ?array &$seen = null): array {
}

/**
* @return string[]
* @throws ServerNotAvailableException
*/
private function _getGroupDNsFromMemberOf(string $dn): array {
Expand All @@ -355,6 +359,11 @@ private function _getGroupDNsFromMemberOf(string $dn): array {
return $this->filterValidGroups($groups);
}

/**
* @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
*/
private function processListFromWalkingNestedGroups(array &$list, array &$seen, string $dn, Closure $fetcher): void {
while ($record = array_shift($list)) {
$recordDN = $record['dn'][0] ?? $record;
Expand All @@ -376,6 +385,11 @@ 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
*/
private function walkNestedGroupsReturnDNs(string $dn, Closure $fetcher, array $list, array &$seen = []): array {
$nesting = (int)$this->access->connection->ldapNestedGroups;

Expand All @@ -387,6 +401,12 @@ private function walkNestedGroupsReturnDNs(string $dn, Closure $fetcher, array $
return array_keys($seen);
}

/**
* @psalm-param list<array{dn: list<string>}> $list
* @psalm-param array<string, int|array|string> $seen List of DN that have already been processed.
* @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;

Expand All @@ -406,9 +426,9 @@ private function walkNestedGroupsReturnRecords(string $dn, Closure $fetcher, arr
}

/**
* translates a gidNumber into an ownCloud internal name
* Translates a gidNumber into the Nextcloud internal name.
*
* @return string|bool
* @return string|false The nextcloud internal name.
* @throws Exception
* @throws ServerNotAvailableException
*/
Expand All @@ -429,6 +449,7 @@ public function gidNumber2Name(string $gid, string $dn) {
}

/**
* @return string|null|false The name of the group
* @throws ServerNotAvailableException
* @throws Exception
*/
Expand All @@ -451,9 +472,7 @@ private function getNameOfGroup(string $filter, string $cacheKey) {
}

/**
* returns the entry's gidNumber
*
* @return string|bool
* @return string|bool The entry's gidNumber
* @throws ServerNotAvailableException
*/
private function getEntryGidNumber(string $dn, string $attribute) {
Expand All @@ -465,17 +484,15 @@ private function getEntryGidNumber(string $dn, string $attribute) {
}

/**
* @return string|bool
* @return string|bool The group's gidNumber
* @throws ServerNotAvailableException
*/
public function getGroupGidNumber(string $dn) {
return $this->getEntryGidNumber($dn, 'gidNumber');
}

/**
* returns the user's gidNumber
*
* @return string|bool
* @return string|bool The user's gidNumber
* @throws ServerNotAvailableException
*/
public function getUserGidNumber(string $dn) {
Expand Down Expand Up @@ -510,8 +527,7 @@ private function prepareFilterForUsersHasGidNumber(string $groupDN, string $sear
}

/**
* returns a list of users that have the given group as gid number
*
* @return A list of users that have the given group as gid number
* @throws ServerNotAvailableException
*/
public function getUsersInGidNumber(
Expand All @@ -538,7 +554,7 @@ public function getUsersInGidNumber(

/**
* @throws ServerNotAvailableException
* @return bool
* @return false|string
*/
public function getUserGroupByGid(string $dn) {
$groupID = $this->getUserGidNumber($dn);
Expand All @@ -553,9 +569,9 @@ public function getUserGroupByGid(string $dn) {
}

/**
* translates a primary group ID into an Nextcloud internal name
* Translates a primary group ID into an Nextcloud internal name
*
* @return string|bool
* @return string|false
* @throws Exception
* @throws ServerNotAvailableException
*/
Expand All @@ -580,9 +596,7 @@ public function primaryGroupID2Name(string $gid, string $dn) {
}

/**
* returns the entry's primary group ID
*
* @return string|bool
* @return string|false The entry's group Id
* @throws ServerNotAvailableException
*/
private function getEntryGroupID(string $dn, string $attribute) {
Expand All @@ -594,15 +608,15 @@ private function getEntryGroupID(string $dn, string $attribute) {
}

/**
* @return string|bool
* @return string|false The entry's primary group Id
* @throws ServerNotAvailableException
*/
public function getGroupPrimaryGroupID(string $dn) {
return $this->getEntryGroupID($dn, 'primaryGroupToken');
}

/**
* @return string|bool
* @return string|false
* @throws ServerNotAvailableException
*/
public function getUserPrimaryGroupIDs(string $dn) {
Expand Down Expand Up @@ -682,7 +696,7 @@ public function countUsersInPrimaryGroup(
}

/**
* @return string|bool
* @return string|false
* @throws ServerNotAvailableException
*/
public function getUserPrimaryGroup(string $dn) {
Expand Down Expand Up @@ -775,14 +789,12 @@ public function getUserGroups($uid) {
&& $this->ldapGroupMemberAssocAttr !== 'memberuid'
&& $this->ldapGroupMemberAssocAttr !== 'zimbramailforwardingaddress') {
$groupDNs = $this->_getGroupDNsFromMemberOf($userDN);
if (is_array($groupDNs)) {
foreach ($groupDNs as $dn) {
$groupName = $this->access->dn2groupname($dn);
if (is_string($groupName)) {
// be sure to never return false if the dn could not be
// resolved to a name, for whatever reason.
$groups[] = $groupName;
}
foreach ($groupDNs as $dn) {
$groupName = $this->access->dn2groupname($dn);
if (is_string($groupName)) {
// be sure to never return false if the dn could not be
// resolved to a name, for whatever reason.
$groups[] = $groupName;
}
}

Expand Down Expand Up @@ -939,7 +951,7 @@ public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {

$groupDN = $this->access->groupname2dn($gid);
if (!$groupDN) {
// group couldn't be found, return empty resultset
// group couldn't be found, return empty result-set
$this->access->connection->writeToCache($cacheKey, []);
return [];
}
Expand Down

0 comments on commit 424d9dc

Please sign in to comment.