Skip to content

Commit

Permalink
fix(php): Check if an user is a space manager
Browse files Browse the repository at this point in the history
I have checked incorrectly whether an user is a space manager of another
space.

Signed-off-by: Baptiste Fotia <[email protected]>
  • Loading branch information
zak39 committed Mar 26, 2024
1 parent 3d7840a commit ee756d4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
8 changes: 6 additions & 2 deletions lib/Controller/GroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,9 @@ public function removeUser(array|string $space,
$this->logger->debug('User removed from group: ' . $NCGroup->getDisplayName());
if ($groupId === WorkspaceManagerGroup::get($space['id'])) {
$this->logger->debug('Removing user from a workspace manager group, removing it from the WorkspacesManagers group if needed.');
$this->userService->removeGEFromWM($NCUser, $space);
if ($this->userService->canRemoveWorkspaceManagers($NCUser, $space)) {
$this->userService->removeGEFromWM($NCUser);
}
}
}
} else {
Expand All @@ -272,7 +274,9 @@ public function removeUser(array|string $space,
if ($gid === WorkspaceManagerGroup::get($space['id'])) {
// Removing user from a GE- group
$this->logger->debug('Removing user from a workspace manager group, removing it from the WorkspacesManagers group if needed.');
$this->userService->removeGEFromWM($NCUser, $space);
if ($this->userService->canRemoveWorkspaceManagers($NCUser, $space)) {
$this->userService->removeGEFromWM($NCUser);
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions lib/Controller/WorkspaceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ public function destroy(array $workspace): JSONResponse {
$this->logger->debug('Removing GE users from the WorkspacesManagers group if needed.');
$GEGroup = $this->groupManager->get(WorkspaceManagerGroup::get($workspace['id']));
foreach ($GEGroup->getUsers() as $user) {
$this->userService->removeGEFromWM($user, $workspace);
if ($this->userService->canRemoveWorkspaceManagers($user, $workspace)) {
$this->userService->removeGEFromWM($user);
}
}

// Removes all workspaces groups
Expand Down Expand Up @@ -245,7 +247,9 @@ public function changeUserRole(array|string $space,
// Changing a user's role from admin to user
$GEgroup->removeUser($user);
$this->logger->debug('Removing a user from a GE group. Removing it from the ' . ManagersWorkspace::WORKSPACES_MANAGERS . ' group if needed.');
$this->userService->removeGEFromWM($user, $space);
if ($this->userService->canRemoveWorkspaceManagers($user, $space)) {
$this->userService->removeGEFromWM($user, $space);
}
} else {
// Changing a user's role from user to admin
$this->groupManager->get(WorkspaceManagerGroup::get($space['id']))->addUser($user);
Expand Down
37 changes: 25 additions & 12 deletions lib/Service/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,33 +123,46 @@ public function isSpaceManagerOfSpace(array $space): bool {
return false;
}


/**
* This function removes a GE from the WorkspaceManagers group when necessary
* Return `true` if the user can be removed from workspace manager group (SPACE-GE), Otherwise, `false`.
*
* @param IUser $user
* @param array|object $space
* @return boolean
*/
public function removeGEFromWM(IUser $user, array|object $space): void {
$found = false;
public function canRemoveWorkspaceManagers(IUser $user, array|object $space): bool {
$canRemove = true;
$groups = $this->groupManager->getUserGroups($user);

// Checks if the user is member of the GE- group of another workspace
foreach ($groups as $group) {
$gid = $group->getGID();
if (strpos($gid, WorkspaceManagerGroup::get($space['id'])) === 0 &&
if (strpos($gid, WorkspaceManagerGroup::getPrefix()) === 0 &&
$gid !== UserGroup::get($space['id'])
) {
$found = true;
$canRemove = false;
break;
}
}

// Removing the user from the WorkspacesManagers group if needed
if (!$found) {
$this->logger->debug('User is not manager of any other workspace, removing it from the ' . ManagersWorkspace::WORKSPACES_MANAGERS . ' group.');
$workspaceUserGroup = $this->groupManager->get(ManagersWorkspace::WORKSPACES_MANAGERS);
$workspaceUserGroup->removeUser($user);
} else {
if (!$canRemove) {
$this->logger->debug('User is still manager of other workspaces, will not remove it from the ' . ManagersWorkspace::WORKSPACES_MANAGERS . ' group.');
}

return $canRemove;
}

/**
* This function removes a GE from the WorkspaceManagers group when necessary
*
* @param IUser $user
* @return void
*/
public function removeGEFromWM(IUser $user): void {
$this->logger->debug('User is not manager of any other workspace, removing it from the ' . ManagersWorkspace::WORKSPACES_MANAGERS . ' group.');
$workspaceUserGroup = $this->groupManager->get(ManagersWorkspace::WORKSPACES_MANAGERS);
$workspaceUserGroup->removeUser($user);

return;
}
}

0 comments on commit ee756d4

Please sign in to comment.