From 9b527749d4d5c2ceeb38e89fd9c996c2bb2077c9 Mon Sep 17 00:00:00 2001 From: zak39 Date: Wed, 30 Oct 2024 15:39:23 +0100 Subject: [PATCH 01/12] 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 +} From 9b7f341cb3abb59935edb2df23c4ee320633aec3 Mon Sep 17 00:00:00 2001 From: zak39 Date: Tue, 5 Nov 2024 18:03:33 +0100 Subject: [PATCH 02/12] chore: Remove the get function that is deprecated --- src/SelectGroupfolders.vue | 2 +- src/tests/unit/groupfoldersService.test.js | 21 --------------------- 2 files changed, 1 insertion(+), 22 deletions(-) diff --git a/src/SelectGroupfolders.vue b/src/SelectGroupfolders.vue index 73b421d4e..16ef92c0c 100644 --- a/src/SelectGroupfolders.vue +++ b/src/SelectGroupfolders.vue @@ -55,7 +55,7 @@ - - From 3910e3609e5f7f5e43679a6249ee02e93abb731f Mon Sep 17 00:00:00 2001 From: zak39 Date: Wed, 6 Nov 2024 17:40:03 +0100 Subject: [PATCH 11/12] refactor: Use SpaceManager to remove a space from the Controller --- lib/Controller/WorkspaceController.php | 15 ++++++++------- lib/Group/Admin/AdminGroup.php | 14 +++++++++++++- lib/Group/Admin/AdminUserGroup.php | 12 +++++++++++- lib/Service/UserService.php | 4 ++++ lib/Space/SpaceManager.php | 6 ++++++ 5 files changed, 42 insertions(+), 9 deletions(-) diff --git a/lib/Controller/WorkspaceController.php b/lib/Controller/WorkspaceController.php index 42b7180a2..28681bcb6 100644 --- a/lib/Controller/WorkspaceController.php +++ b/lib/Controller/WorkspaceController.php @@ -32,6 +32,8 @@ use OCA\Workspace\Exceptions\CreateWorkspaceException; use OCA\Workspace\Exceptions\WorkspaceNameExistException; 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; @@ -53,6 +55,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, @@ -147,17 +151,14 @@ public function createWorkspace(string $spaceName, */ public function destroy(int $spaceId): JSONResponse { $this->logger->debug('Removing GE users from the WorkspacesManagers group if needed.'); - $space = $this->spaceManager->get($spaceId); - $folderId = $space['groupfolder_id']; - - $GEGroup = $this->groupManager->get(WorkspaceManagerGroup::get($spaceId)); - 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($space['groups']) as $group) { @@ -165,7 +166,7 @@ public function destroy(int $spaceId): JSONResponse { $this->groupManager->get($group)->delete(); } - $this->folderHelper->removeFolder($folderId); + $this->spaceManager->remove($spaceId); return new JSONResponse([ 'http' => [ diff --git a/lib/Group/Admin/AdminGroup.php b/lib/Group/Admin/AdminGroup.php index d1a2e6cfd..591776801 100644 --- a/lib/Group/Admin/AdminGroup.php +++ b/lib/Group/Admin/AdminGroup.php @@ -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. @@ -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 { @@ -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(); + } } diff --git a/lib/Group/Admin/AdminUserGroup.php b/lib/Group/Admin/AdminUserGroup.php index a936cac92..1b41e3a4b 100644 --- a/lib/Group/Admin/AdminUserGroup.php +++ b/lib/Group/Admin/AdminUserGroup.php @@ -26,6 +26,7 @@ use OCP\IGroupManager; use OCP\IUser; +use Psr\Log\LoggerInterface; /** * This class gathers all users from the @@ -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 { @@ -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); + } } diff --git a/lib/Service/UserService.php b/lib/Service/UserService.php index a70dcc5f6..0b7f10eed 100644 --- a/lib/Service/UserService.php +++ b/lib/Service/UserService.php @@ -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.'); diff --git a/lib/Space/SpaceManager.php b/lib/Space/SpaceManager.php index fda7d4473..377dee0a9 100644 --- a/lib/Space/SpaceManager.php +++ b/lib/Space/SpaceManager.php @@ -145,4 +145,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); + } } From c1a2a4e19a151ce3617d6a6fc2732933b3474d04 Mon Sep 17 00:00:00 2001 From: zak39 Date: Thu, 7 Nov 2024 14:42:43 +0100 Subject: [PATCH 12/12] style: composer run cs:fix --- lib/Controller/WorkspaceController.php | 8 ++++---- lib/Group/Admin/AdminGroup.php | 16 ++++++++-------- lib/Group/Admin/AdminUserGroup.php | 10 +++++----- lib/Service/UserService.php | 8 ++++---- lib/Space/SpaceManager.php | 8 ++++---- 5 files changed, 25 insertions(+), 25 deletions(-) diff --git a/lib/Controller/WorkspaceController.php b/lib/Controller/WorkspaceController.php index 28681bcb6..eb37aa4a2 100644 --- a/lib/Controller/WorkspaceController.php +++ b/lib/Controller/WorkspaceController.php @@ -55,8 +55,8 @@ class WorkspaceController extends Controller { public function __construct( IRequest $request, - private AdminGroup $adminGroup, - private AdminUserGroup $adminUserGroup, + private AdminGroup $adminGroup, + private AdminUserGroup $adminUserGroup, private GroupfolderHelper $folderHelper, private IGroupManager $groupManager, private RootFolder $rootFolder, @@ -158,7 +158,7 @@ public function destroy(int $spaceId): JSONResponse { } // Removes all workspaces groups - $space = $this->spaceManager->get($spaceId); + $space = $this->spaceManager->get($spaceId); $groups = []; $this->logger->debug('Removing workspaces groups.'); foreach (array_keys($space['groups']) as $group) { @@ -166,7 +166,7 @@ public function destroy(int $spaceId): JSONResponse { $this->groupManager->get($group)->delete(); } - $this->spaceManager->remove($spaceId); + $this->spaceManager->remove($spaceId); return new JSONResponse([ 'http' => [ diff --git a/lib/Group/Admin/AdminGroup.php b/lib/Group/Admin/AdminGroup.php index 591776801..fd24df5a0 100644 --- a/lib/Group/Admin/AdminGroup.php +++ b/lib/Group/Admin/AdminGroup.php @@ -36,8 +36,8 @@ class AdminGroup { public function __construct(private AdminUserGroup $adminUserGroup, private AdminGroupManager $adminGroupManager, - private LoggerInterface $logger, - private IGroupManager $groupManager) { + private LoggerInterface $logger, + private IGroupManager $groupManager) { } public function addUser(IUser $user, string $gid): bool { @@ -48,11 +48,11 @@ public function addUser(IUser $user, string $gid): bool { return true; } - /** - * @return IUser[] - */ - public function getUsers(int $spaceId): array { + /** + * @return IUser[] + */ + public function getUsers(int $spaceId): array { $group = $this->groupManager->get(self::GID_PREFIX . $spaceId); - return $group->getUsers(); - } + return $group->getUsers(); + } } diff --git a/lib/Group/Admin/AdminUserGroup.php b/lib/Group/Admin/AdminUserGroup.php index 1b41e3a4b..450117f69 100644 --- a/lib/Group/Admin/AdminUserGroup.php +++ b/lib/Group/Admin/AdminUserGroup.php @@ -37,9 +37,9 @@ class AdminUserGroup { public const GID = 'WorkspacesManagers'; public function __construct( - private IGroupManager $groupManager, - private LoggerInterface $logger, - ) { + private IGroupManager $groupManager, + private LoggerInterface $logger, + ) { } public function addUser(IUser $user): bool { @@ -54,9 +54,9 @@ public function addUser(IUser $user): bool { return true; } - public function removeUser(IUser $user): void { + 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); - } + } } diff --git a/lib/Service/UserService.php b/lib/Service/UserService.php index 0b7f10eed..97aad039e 100644 --- a/lib/Service/UserService.php +++ b/lib/Service/UserService.php @@ -160,10 +160,10 @@ public function canRemoveWorkspaceManagers(IUser $user): bool { * * @param IUser $user * @return void - * - * @deprecated - * - * @uses OCA\Workspace\Group\Admin\AdminUserGroup::removeUser + * + * @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.'); diff --git a/lib/Space/SpaceManager.php b/lib/Space/SpaceManager.php index 377dee0a9..b9b19ecee 100644 --- a/lib/Space/SpaceManager.php +++ b/lib/Space/SpaceManager.php @@ -146,9 +146,9 @@ private function deleteBlankSpaceName(string $spaceName): string { return trim($spaceName); } - public function remove(string $spaceId): void { - $space = $this->get($spaceId); + public function remove(string $spaceId): void { + $space = $this->get($spaceId); $folderId = $space['groupfolder_id']; - $this->folderHelper->removeFolder($folderId); - } + $this->folderHelper->removeFolder($folderId); + } }