Skip to content

Commit

Permalink
feat(keepEmptyGroups): Add app configuration parameter to keep empty …
Browse files Browse the repository at this point in the history
…groups

Signed-off-by: Marco Jarjour <[email protected]>
  • Loading branch information
marcojarjour committed Jan 15, 2025
1 parent 4368a73 commit cf0b448
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/GroupManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,16 @@ protected function unassignUserFromGroup(IUser $user, string $gid): void {
return;
}

// keep empty groups if general-keep_groups is set to 1
$keepEmptyGroups = $this->config->getAppValue(
'user_saml',
'general-keep_groups',
'0',
);

if ($this->hasSamlBackend($group)) {
$this->ownGroupBackend->removeFromGroup($user->getUID(), $group->getGID());
if ($this->ownGroupBackend->countUsersInGroup($gid) === 0) {
if ($this->ownGroupBackend->countUsersInGroup($gid) === 0 && !$keepEmptyGroups == '1') {
$this->dispatcher->dispatchTyped(new BeforeGroupDeletedEvent($group));
$this->ownGroupBackend->deleteGroup($group->getGID());
$this->dispatcher->dispatchTyped(new GroupDeletedEvent($group));
Expand Down
42 changes: 42 additions & 0 deletions tests/unit/GroupManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,48 @@ public function testUnassignUserFromGroups() {
$this->invokePrivate($this->ownGroupManager, 'handleUserUnassignedFromGroups', [$user, ['groupA']]);
}

public function testUnassignUserFromGroupsWithKeepEmpytGroups() {
$this->getGroupManager();
// set general-keep_groups to 1 and assert it was read
$this->config
->expects($this->exactly(1))
->method('getAppValue')
->with('user_saml', 'general-keep_groups', '0')
->willReturn('1');
// create user and group mock
$user = $this->createMock(IUser::class);
$groupA = $this->createMock(IGroup::class);
$groupA->method('getBackendNames')
->willReturn(['Database', 'user_saml']);
$this->groupManager
->method('get')
->with('groupA')
->willReturn($groupA);
$user->expects($this->once())
->method('getUID')
->willReturn('uid');
$groupA->expects($this->exactly(1))
->method('getGID')
->willReturn('gid');
// assert membership gets removed
$this->ownGroupBackend
->expects($this->once())
->method('removeFromGroup');
// assert no remaining group memberships
$this->ownGroupBackend
->expects($this->once())
->method('countUsersInGroup')
->willReturn(0);
// assert group is not deleted
$this->ownGroupBackend
->expects($this->never())
->method('deleteGroup');
$this->eventDispatcher->expects($this->exactly(0))
->method('dispatchTyped');

$this->invokePrivate($this->ownGroupManager, 'handleUserUnassignedFromGroups', [$user, ['groupA']]);
}

public function testAssignUserToGroups() {
$this->getGroupManager(['hasSamlBackend', 'createGroupInBackend']);
$user = $this->createMock(IUser::class);
Expand Down

0 comments on commit cf0b448

Please sign in to comment.