From 9b527749d4d5c2ceeb38e89fd9c996c2bb2077c9 Mon Sep 17 00:00:00 2001 From: zak39 Date: Wed, 30 Oct 2024 15:39:23 +0100 Subject: [PATCH] refactor: Remove group folder handling from the back-end --- appinfo/routes.php | 2 +- lib/Controller/WorkspaceController.php | 21 ++++++++----- lib/Helper/GroupfolderHelper.php | 8 +++++ lib/Middleware/IsGeneralManagerMiddleware.php | 1 + src/SpaceDetails.vue | 7 +++-- src/services/groupfoldersService.js | 31 ++++++++++--------- src/services/spaceService.js | 16 ++++++++++ 7 files changed, 60 insertions(+), 26 deletions(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index 755947410..fca9962dd 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -85,7 +85,7 @@ ], [ 'name' => 'workspace#destroy', - 'url' => '/api/delete/space', + 'url' => '/spaces/{spaceId}', 'verb' => 'DELETE' ], [ diff --git a/lib/Controller/WorkspaceController.php b/lib/Controller/WorkspaceController.php index f92d2946c..e36e6487c 100644 --- a/lib/Controller/WorkspaceController.php +++ b/lib/Controller/WorkspaceController.php @@ -41,6 +41,7 @@ use OCA\Workspace\Service\UserService; use OCA\Workspace\Service\Workspace\WorkspaceCheckService; use OCA\Workspace\Service\WorkspaceService; +use OCA\Workspace\Space\SpaceManager; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\JSONResponse; @@ -64,6 +65,7 @@ public function __construct( private WorkspaceService $workspaceService, private UserGroup $userGroup, private WorkspaceManagerGroup $workspaceManagerGroup, + private SpaceManager $spaceManager, public $AppName ) { parent::__construct($AppName, $request); @@ -140,12 +142,15 @@ public function createWorkspace(string $spaceName, * * @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'])); + $space = $this->spaceManager->get($spaceId); + $folderId = $space['groupfolder_id']; + + $GEGroup = $this->groupManager->get(WorkspaceManagerGroup::get($spaceId)); foreach ($GEGroup->getUsers() as $user) { if ($this->userService->canRemoveWorkspaceManagers($user)) { $this->userService->removeGEFromWM($user); @@ -155,21 +160,23 @@ public function destroy(array $workspace): JSONResponse { // Removes all workspaces groups $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->folderHelper->removeFolder($folderId); + 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' ] ]); diff --git a/lib/Helper/GroupfolderHelper.php b/lib/Helper/GroupfolderHelper.php index d27b625e2..d5ab88925 100644 --- a/lib/Helper/GroupfolderHelper.php +++ b/lib/Helper/GroupfolderHelper.php @@ -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.'); + } + } } diff --git a/lib/Middleware/IsGeneralManagerMiddleware.php b/lib/Middleware/IsGeneralManagerMiddleware.php index 346899770..3cda899a9 100644 --- a/lib/Middleware/IsGeneralManagerMiddleware.php +++ b/lib/Middleware/IsGeneralManagerMiddleware.php @@ -33,6 +33,7 @@ use OCP\AppFramework\Middleware; use OCP\AppFramework\Utility\IControllerMethodReflector; use OCP\IRequest; +use PHPUnit\Util\Json; class IsGeneralManagerMiddleware extends Middleware { public function __construct( diff --git a/src/SpaceDetails.vue b/src/SpaceDetails.vue index 3e2cc5072..a83893718 100644 --- a/src/SpaceDetails.vue +++ b/src/SpaceDetails.vue @@ -109,6 +109,7 @@ import SelectUsers from './SelectUsers.vue' import RemoveSpace from './RemoveSpace.vue' import UserTable from './UserTable.vue' import { destroy, rename, checkGroupfolderNameExist } from './services/groupfoldersService.js' +import { removeWorkspace } from './services/spaceService.js' import showNotificationError from './services/Notifications/NotificationError.js' export default { @@ -148,12 +149,12 @@ export default { methods: { // Deletes a space deleteSpace() { - const space = this.$route.params.space - destroy(this.$store.state.spaces[space]) + const space = this.$store.state.spaces[this.$route.params.space] + removeWorkspace(space.id) .then(resp => { if (resp.http.statuscode === 200) { this.$store.dispatch('removeSpace', { - space: this.$store.state.spaces[space], + space, }) this.$router.push({ path: '/', diff --git a/src/services/groupfoldersService.js b/src/services/groupfoldersService.js index 8745be773..5e95d432e 100644 --- a/src/services/groupfoldersService.js +++ b/src/services/groupfoldersService.js @@ -57,21 +57,21 @@ export function getAll() { * @return {Promise} * @throws {GetGroupfolderError} */ -export function get(groupfolderId) { - return axios.get(generateUrl(`/apps/groupfolders/folders/${groupfolderId}`)) - .then(resp => { - if (resp.data.ocs.meta.status === 'ok') { - const workspace = resp.data.ocs.data - return workspace - } else { - throw new GetGroupfolderError('Impossible to get the groupfolder. May be an error network ?') - } - }) - .catch((error) => { - showNotificationError('Error to get the groupfolder', error.message, 5000) - throw new Error(error.message) - }) -} +// export function get(groupfolderId) { +// return axios.get(generateUrl(`/apps/groupfolders/folders/${groupfolderId}`)) +// .then(resp => { +// if (resp.data.ocs.meta.status === 'ok') { +// const workspace = resp.data.ocs.data +// return workspace +// } else { +// throw new GetGroupfolderError('Impossible to get the groupfolder. May be an error network ?') +// } +// }) +// .catch((error) => { +// showNotificationError('Error to get the groupfolder', error.message, 5000) +// throw new Error(error.message) +// }) +// } /** * @@ -257,6 +257,7 @@ export function createGroupfolder(spaceName) { /** * @param {object} workspace it's an object relative to workspace + * @deprecated * @return {Promise} */ export function destroy(workspace) { diff --git a/src/services/spaceService.js b/src/services/spaceService.js index f7495137c..2fbd38fd6 100644 --- a/src/services/spaceService.js +++ b/src/services/spaceService.js @@ -125,3 +125,19 @@ export function addGroupToWorkspace(spaceId, gid) { throw new AddGroupToGroupfolderError('Error to add Space Manager group in the groupfolder') }) } + +/** + * @param {integer} spaceId it's the id relative to workspace + * @return {Promise} + */ +export function removeWorkspace(spaceId) { + const result = axios.delete(generateUrl(`/apps/workspace/spaces/${spaceId}`)) + .then(resp => { + console.log(`The workspace with the ${spaceId} id, is deleted.`) + return resp.data + }) + .catch(error => { + console.error('Error to delete a workspace. May be a problem network ?', error) + }) + return result +}