Skip to content

Commit

Permalink
Merge pull request #1081 from arawa/refactor/destroy-a-group-folder-b…
Browse files Browse the repository at this point in the history
…ackend-side

Remove group folder handling from the back-end
  • Loading branch information
zak39 authored Nov 7, 2024
2 parents 43b0c06 + c1a2a4e commit bbb0fb0
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 308 deletions.
2 changes: 1 addition & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
],
[
'name' => 'workspace#destroy',
'url' => '/api/delete/space',
'url' => '/spaces/{spaceId}',
'verb' => 'DELETE'
],
[
Expand Down
24 changes: 15 additions & 9 deletions lib/Controller/WorkspaceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
use OCA\Workspace\Db\SpaceMapper;
use OCA\Workspace\Exceptions\BadRequestException;
use OCA\Workspace\Folder\RootFolder;
use OCA\Workspace\Group\Admin\AdminGroup;
use OCA\Workspace\Group\Admin\AdminUserGroup;
use OCA\Workspace\Helper\GroupfolderHelper;
use OCA\Workspace\Service\Group\GroupFormatter;
use OCA\Workspace\Service\Group\ManagersWorkspace;
Expand All @@ -50,6 +52,8 @@
class WorkspaceController extends Controller {
public function __construct(
IRequest $request,
private AdminGroup $adminGroup,
private AdminUserGroup $adminUserGroup,
private GroupfolderHelper $folderHelper,
private IGroupManager $groupManager,
private RootFolder $rootFolder,
Expand Down Expand Up @@ -101,36 +105,38 @@ public function createWorkspace(string $spaceName): JSONResponse {
*
* @NoAdminRequired
* @GeneralManagerRequired
* @param array $workspace
* @param int $spaceId
*
*/
public function destroy(array $workspace): JSONResponse {
public function destroy(int $spaceId): 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) {
foreach ($this->adminGroup->getUsers($spaceId) as $user) {
if ($this->userService->canRemoveWorkspaceManagers($user)) {
$this->userService->removeGEFromWM($user);
$this->adminUserGroup->removeUser($user);
}
}

// Removes all workspaces groups
$space = $this->spaceManager->get($spaceId);
$groups = [];
$this->logger->debug('Removing workspaces groups.');
foreach (array_keys($workspace['groups']) as $group) {
foreach (array_keys($space['groups']) as $group) {
$groups[] = $group;
$this->groupManager->get($group)->delete();
}

$this->spaceManager->remove($spaceId);

return new JSONResponse([
'http' => [
'statuscode' => 200,
'message' => 'The space is deleted.'
],
'data' => [
'name' => $workspace['name'],
'name' => $space['name'],
'groups' => $groups,
'space_id' => $workspace['id'],
'groupfolder_id' => $workspace['groupfolderId'],
'space_id' => $space['id'],
'groupfolder_id' => $space['groupfolderId'],
'state' => 'delete'
]
]);
Expand Down
14 changes: 13 additions & 1 deletion lib/Group/Admin/AdminGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@

namespace OCA\Workspace\Group\Admin;

use OCP\IGroupManager;
use OCP\IUser;
use Psr\Log\LoggerInterface;

/**
* This class represents a Workspace Manager (GE-) group.
Expand All @@ -33,7 +35,9 @@ class AdminGroup {
public const GID_PREFIX = 'SPACE-GE-';

public function __construct(private AdminUserGroup $adminUserGroup,
private AdminGroupManager $adminGroupManager) {
private AdminGroupManager $adminGroupManager,
private LoggerInterface $logger,
private IGroupManager $groupManager) {
}

public function addUser(IUser $user, string $gid): bool {
Expand All @@ -43,4 +47,12 @@ public function addUser(IUser $user, string $gid): bool {

return true;
}

/**
* @return IUser[]
*/
public function getUsers(int $spaceId): array {
$group = $this->groupManager->get(self::GID_PREFIX . $spaceId);
return $group->getUsers();
}
}
12 changes: 11 additions & 1 deletion lib/Group/Admin/AdminUserGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

use OCP\IGroupManager;
use OCP\IUser;
use Psr\Log\LoggerInterface;

/**
* This class gathers all users from the
Expand All @@ -35,7 +36,10 @@
class AdminUserGroup {
public const GID = 'WorkspacesManagers';

public function __construct(private IGroupManager $groupManager) {
public function __construct(
private IGroupManager $groupManager,
private LoggerInterface $logger,
) {
}

public function addUser(IUser $user): bool {
Expand All @@ -49,4 +53,10 @@ public function addUser(IUser $user): bool {

return true;
}

public function removeUser(IUser $user): void {
$this->logger->debug('The ' . $user->getUID() . 'User is not manager of any other workspace, removing it from the ' . self::GID . ' group.');
$workspaceUserGroup = $this->groupManager->get(self::GID);
$workspaceUserGroup->removeUser($user);
}
}
12 changes: 10 additions & 2 deletions lib/Helper/GroupfolderHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ public function createFolder(string $mountpoint): int {
public function getFolder(int $folderId, int $rootStorageId) {
try {
return $this->folderManager->getFolder($folderId, $rootStorageId);
} catch(\Exception $e) {
} catch (\Exception $e) {
throw new GroupFolderFunctionException($e->getMessage() . 'Impossible to use the getFolder function from FolderManager.');
}
}

public function setFolderAcl(int $folderId, bool $acl): void {
try {
$this->folderManager->setFolderAcl($folderId, $acl);
} catch(\Exception $e) {
} catch (\Exception $e) {
throw new GroupFolderFunctionException($e->getMessage() . 'Impossible to use the setFolderAcl from FolderManager.');
}
}
Expand Down Expand Up @@ -96,4 +96,12 @@ public function setFolderQuota(int $folderId, int $quota): void {
throw new GroupFolderFunctionException($e->getMessage() . 'Impossible to use the setFolderQuota from FolderManager.');
}
}

public function removeFolder(int $folderId): void {
try {
$this->folderManager->removeFolder($folderId);
} catch (\Exception $e) {
throw new GroupFolderFunctionException($e->getMessage() . 'Impossible to use the removeFolder from FolderManager.');
}
}
}
4 changes: 4 additions & 0 deletions lib/Service/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ public function canRemoveWorkspaceManagers(IUser $user): bool {
*
* @param IUser $user
* @return void
*
* @deprecated
*
* @uses OCA\Workspace\Group\Admin\AdminUserGroup::removeUser
*/
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.');
Expand Down
6 changes: 6 additions & 0 deletions lib/Space/SpaceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,10 @@ public function attachGroup(int $folderId, string $gid): void {
private function deleteBlankSpaceName(string $spaceName): string {
return trim($spaceName);
}

public function remove(string $spaceId): void {
$space = $this->get($spaceId);
$folderId = $space['groupfolder_id'];
$this->folderHelper->removeFolder($folderId);
}
}
Loading

0 comments on commit bbb0fb0

Please sign in to comment.