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

feat(keepEmptyGroups): Add app configuration parameter to keep empty groups #911

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 8 additions & 1 deletion lib/GroupManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,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 ($keepEmptyGroups !== '1' && $this->ownGroupBackend->countUsersInGroup($gid) === 0) {
$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->never())
->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