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..eb37aa4a2 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; @@ -41,6 +43,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; @@ -52,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, @@ -64,7 +69,8 @@ public function __construct( private WorkspaceService $workspaceService, private UserGroup $userGroup, private WorkspaceManagerGroup $workspaceManagerGroup, - public $AppName + private SpaceManager $spaceManager, + public $AppName, ) { parent::__construct($AppName, $request); } @@ -140,36 +146,38 @@ 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'])); - 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' ] ]); @@ -206,7 +214,7 @@ public function findAll(): JSONResponse { $this->logger->warning( "Be careful, the $gid group is not exist in the oc_groups table." . " But, it's present in the oc_group_folders_groups table." - . 'It necessary to recreate it with the occ command.' + . 'It necessary to recreate it with the occ command.' ); continue; } diff --git a/lib/Group/Admin/AdminGroup.php b/lib/Group/Admin/AdminGroup.php index d1a2e6cfd..fd24df5a0 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..450117f69 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/Helper/GroupfolderHelper.php b/lib/Helper/GroupfolderHelper.php index d27b625e2..d71af1f17 100644 --- a/lib/Helper/GroupfolderHelper.php +++ b/lib/Helper/GroupfolderHelper.php @@ -60,7 +60,7 @@ 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.'); } } @@ -68,7 +68,7 @@ public function getFolder(int $folderId, int $rootStorageId) { 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.'); } } @@ -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..6715f6bba 100644 --- a/lib/Middleware/IsGeneralManagerMiddleware.php +++ b/lib/Middleware/IsGeneralManagerMiddleware.php @@ -38,7 +38,7 @@ class IsGeneralManagerMiddleware extends Middleware { public function __construct( private IControllerMethodReflector $reflector, private IRequest $request, - private UserService $userService + private UserService $userService, ) { } diff --git a/lib/Service/UserService.php b/lib/Service/UserService.php index a70dcc5f6..97aad039e 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..b9b19ecee 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); + } } diff --git a/src/SelectGroupfolders.vue b/src/SelectGroupfolders.vue deleted file mode 100644 index 73b421d4e..000000000 --- a/src/SelectGroupfolders.vue +++ /dev/null @@ -1,245 +0,0 @@ - - - - - - - - {{ t('workspace', 'Select groupfolders to convert in workspace') }} - - - - - - - {{ groupfolder.mount_point }} - - - - - - - {{ t('workspace', 'Convert in spaces') }} - - - - - - - - diff --git a/src/SpaceDetails.vue b/src/SpaceDetails.vue index 3e2cc5072..1ae5b9474 100644 --- a/src/SpaceDetails.vue +++ b/src/SpaceDetails.vue @@ -108,7 +108,8 @@ import NcModal from '@nextcloud/vue/dist/Components/NcModal.js' import SelectUsers from './SelectUsers.vue' import RemoveSpace from './RemoveSpace.vue' import UserTable from './UserTable.vue' -import { destroy, rename, checkGroupfolderNameExist } from './services/groupfoldersService.js' +import { 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..b0c4d4ef3 100644 --- a/src/services/groupfoldersService.js +++ b/src/services/groupfoldersService.js @@ -30,7 +30,6 @@ import BadGetError from '../Errors/BadGetError.js' import CheckGroupfolderNameExistError from '../Errors/Groupfolders/CheckGroupfolderNameError.js' import CreateGroupfolderError from '../Errors/Groupfolders/BadCreateError.js' import EnableAclGroupfolderError from '../Errors/Groupfolders/EnableAclGroupfolderError.js' -import GetGroupfolderError from '../Errors/Groupfolders/GetGroupfolderError.js' import showNotificationError from './Notifications/NotificationError.js' import RemoveGroupToManageACLForGroupfolderError from '../Errors/Groupfolders/RemoveGroupToManageACLForGroupfolderError.js' @@ -51,28 +50,6 @@ export function getAll() { return data } -/** - * - * @param {number} groupfolderId it's the id of a groupfolder - * @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) - }) -} - /** * * @param {object} space it's an object relative to space @@ -257,6 +234,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..abbe4ad4f 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.info(`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 +} diff --git a/src/tests/unit/groupfoldersService.test.js b/src/tests/unit/groupfoldersService.test.js index c5038b79c..f159ef4d4 100644 --- a/src/tests/unit/groupfoldersService.test.js +++ b/src/tests/unit/groupfoldersService.test.js @@ -20,7 +20,7 @@ * */ -import { getAll, get, formatGroups, formatUsers, checkGroupfolderNameExist, enableAcl, addGroupToGroupfolder, addGroupToManageACLForGroupfolder, removeGroupToManageACLForGroupfolder, createGroupfolder, destroy, rename } from '../../services/groupfoldersService.js' +import { getAll, formatGroups, formatUsers, checkGroupfolderNameExist, enableAcl, addGroupToGroupfolder, addGroupToManageACLForGroupfolder, removeGroupToManageACLForGroupfolder, createGroupfolder, destroy, rename } from '../../services/groupfoldersService.js' import axios from '@nextcloud/axios' jest.mock('axios') @@ -95,27 +95,6 @@ describe('getAll function', () => { }) }) -describe('get function', () => { - beforeEach(() => { - axios.mockClear() - }) - it('calls axios.get method', () => { - axios.get.mockResolvedValue(responseValue) - get(1) - expect(axios.get).toBeCalled() - }) - it('returns data property of the object if response status is ok', async () => { - axios.get.mockResolvedValue(responseValue) - const res = await get(1) - expect(res).toEqual(responseValue.data.ocs.data) - }) - it('throws exception if response status is not ok', async () => { - axios.get.mockResolvedValue(badResponseValue) - const promise = get(1) - await expect(promise).rejects.toThrow('Impossible to get the groupfolder') - }) -}) - describe('formatGroups function', () => { beforeEach(() => { axios.mockClear()