Skip to content

Commit cf0b448

Browse files
committed
feat(keepEmptyGroups): Add app configuration parameter to keep empty groups
Signed-off-by: Marco Jarjour <[email protected]>
1 parent 4368a73 commit cf0b448

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

lib/GroupManager.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,16 @@ protected function unassignUserFromGroup(IUser $user, string $gid): void {
150150
return;
151151
}
152152

153+
// keep empty groups if general-keep_groups is set to 1
154+
$keepEmptyGroups = $this->config->getAppValue(
155+
'user_saml',
156+
'general-keep_groups',
157+
'0',
158+
);
159+
153160
if ($this->hasSamlBackend($group)) {
154161
$this->ownGroupBackend->removeFromGroup($user->getUID(), $group->getGID());
155-
if ($this->ownGroupBackend->countUsersInGroup($gid) === 0) {
162+
if ($this->ownGroupBackend->countUsersInGroup($gid) === 0 && !$keepEmptyGroups == '1') {
156163
$this->dispatcher->dispatchTyped(new BeforeGroupDeletedEvent($group));
157164
$this->ownGroupBackend->deleteGroup($group->getGID());
158165
$this->dispatcher->dispatchTyped(new GroupDeletedEvent($group));

tests/unit/GroupManagerTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,48 @@ public function testUnassignUserFromGroups() {
187187
$this->invokePrivate($this->ownGroupManager, 'handleUserUnassignedFromGroups', [$user, ['groupA']]);
188188
}
189189

190+
public function testUnassignUserFromGroupsWithKeepEmpytGroups() {
191+
$this->getGroupManager();
192+
// set general-keep_groups to 1 and assert it was read
193+
$this->config
194+
->expects($this->exactly(1))
195+
->method('getAppValue')
196+
->with('user_saml', 'general-keep_groups', '0')
197+
->willReturn('1');
198+
// create user and group mock
199+
$user = $this->createMock(IUser::class);
200+
$groupA = $this->createMock(IGroup::class);
201+
$groupA->method('getBackendNames')
202+
->willReturn(['Database', 'user_saml']);
203+
$this->groupManager
204+
->method('get')
205+
->with('groupA')
206+
->willReturn($groupA);
207+
$user->expects($this->once())
208+
->method('getUID')
209+
->willReturn('uid');
210+
$groupA->expects($this->exactly(1))
211+
->method('getGID')
212+
->willReturn('gid');
213+
// assert membership gets removed
214+
$this->ownGroupBackend
215+
->expects($this->once())
216+
->method('removeFromGroup');
217+
// assert no remaining group memberships
218+
$this->ownGroupBackend
219+
->expects($this->once())
220+
->method('countUsersInGroup')
221+
->willReturn(0);
222+
// assert group is not deleted
223+
$this->ownGroupBackend
224+
->expects($this->never())
225+
->method('deleteGroup');
226+
$this->eventDispatcher->expects($this->exactly(0))
227+
->method('dispatchTyped');
228+
229+
$this->invokePrivate($this->ownGroupManager, 'handleUserUnassignedFromGroups', [$user, ['groupA']]);
230+
}
231+
190232
public function testAssignUserToGroups() {
191233
$this->getGroupManager(['hasSamlBackend', 'createGroupInBackend']);
192234
$user = $this->createMock(IUser::class);

0 commit comments

Comments
 (0)