Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(LDAP): ensure stored groups are formatted as simple list #42405

Merged
merged 1 commit into from
Dec 22, 2023
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
4 changes: 2 additions & 2 deletions apps/user_ldap/lib/Group_LDAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ private function isUserOnLDAP(string $uid): bool {

protected function getCachedGroupsForUserId(string $uid): array {
$groupStr = $this->config->getUserValue($uid, 'user_ldap', 'cached-group-memberships-' . $this->access->connection->getConfigPrefix(), '[]');
return json_decode($groupStr) ?? [];
return json_decode($groupStr, true) ?? [];
}

/**
Expand Down Expand Up @@ -838,7 +838,7 @@ public function getUserGroups($uid): array {
return $groups;
}

$groups = array_unique($groups, SORT_LOCALE_STRING);
$groups = array_values(array_unique($groups, SORT_LOCALE_STRING));
$this->access->connection->writeToCache($cacheKey, $groups);

$groupStr = \json_encode($groups);
Expand Down
27 changes: 27 additions & 0 deletions apps/user_ldap/tests/Group_LDAPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,33 @@ public function testGetUserGroupsOfflineUser() {
$this->assertTrue(in_array('groupF', $returnedGroups));
}

/**
* regression tests against a case where a json object was stored instead of expected list
* @see https://github.com/nextcloud/server/issues/42374
*/
public function testGetUserGroupsOfflineUserUnexpectedJson() {
$this->enableGroups();

$offlineUser = $this->createMock(OfflineUser::class);

$this->config->expects($this->any())
->method('getUserValue')
->with('userX', 'user_ldap', 'cached-group-memberships-', $this->anything())
// results in a json object: {"0":"groupB","2":"groupF"}
->willReturn(\json_encode([0 => 'groupB', 2 => 'groupF']));

$this->access->userManager->expects($this->any())
->method('get')
->with('userX')
->willReturn($offlineUser);

$this->initBackend();
$returnedGroups = $this->groupBackend->getUserGroups('userX');
$this->assertCount(2, $returnedGroups);
$this->assertTrue(in_array('groupB', $returnedGroups));
$this->assertTrue(in_array('groupF', $returnedGroups));
}

public function testGetUserGroupsUnrecognizedOfflineUser() {
$this->enableGroups();
$dn = 'cn=userX,dc=foobar';
Expand Down
Loading