From bbcfa5df573973fd4c7b14f6a2fcd72823970929 Mon Sep 17 00:00:00 2001 From: zak39 Date: Tue, 15 Oct 2024 11:43:57 +0200 Subject: [PATCH] feat(vue,php): Create an API for attaching a group to a workspace Fixes the issue where "Password confirmation is required" by allowing groups to be attached without needing password confirmation. --- appinfo/routes.php | 5 +++++ lib/Controller/GroupController.php | 15 +++++++++++++++ lib/Space/SpaceManager.php | 15 +++++++++++++++ src/services/spaceService.js | 23 +++++++++++++++++++++++ src/store/actions.js | 4 ++-- 5 files changed, 60 insertions(+), 2 deletions(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index cad3d8fa1..755947410 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -104,6 +104,11 @@ 'url' => '/api/group', 'verb' => 'POST', ], + [ + 'name' => 'group#attachGroupToSpace', + 'url' => '/spaces/{spaceId}/group-attach', + 'verb' => 'POST', + ], [ 'name' => 'group#delete', 'url' => '/api/group/{gid}', diff --git a/lib/Controller/GroupController.php b/lib/Controller/GroupController.php index f9493e8fe..a87b5aee1 100644 --- a/lib/Controller/GroupController.php +++ b/lib/Controller/GroupController.php @@ -34,6 +34,7 @@ use OCA\Workspace\Service\User\UserFormatter; use OCA\Workspace\Service\User\UserWorkspace; use OCA\Workspace\Service\UserService; +use OCA\Workspace\Space\SpaceManager; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\JSONResponse; @@ -52,6 +53,7 @@ public function __construct( private GroupsWorkspaceService $groupsWorkspace, private IGroupManager $groupManager, private LoggerInterface $logger, + private SpaceManager $spaceManager, private IUserManager $userManager, private UserFormatter $userFormatter, private UserService $userService, @@ -369,6 +371,19 @@ public function removeUser( ]); } + /** + * @NoAdminRequired + * @GeneralManagerRequired + */ + public function attachGroupToSpace(int $spaceId, string $gid) { + $workspace = $this->spaceManager->get($spaceId); + $this->spaceManager->attachGroup($workspace['groupfolder_id'], $gid); + + return new JSONResponse([ + 'message' => sprintf('The %s group is attached to the %s workspace (i.e groupfolder)', $gid, $workspace['name']), + ], Http::STATUS_ACCEPTED); + } + /** * @NoAdminRequired * @GeneralManagerRequired diff --git a/lib/Space/SpaceManager.php b/lib/Space/SpaceManager.php index df7a77d96..4e1365ef7 100644 --- a/lib/Space/SpaceManager.php +++ b/lib/Space/SpaceManager.php @@ -122,6 +122,21 @@ public function create(string $spacename): array { ]; } + public function get(int $spaceId): array { + + $space = $this->spaceMapper->find($spaceId)->jsonSerialize(); + $workspace = array_merge( + $this->folderHelper->getFolder($space['groupfolder_id'], $this->rootFolder->getRootFolderStorageId()), + $space + ); + + return $workspace; + } + + public function attachGroup(int $folderId, string $gid): void { + $this->folderHelper->addApplicableGroup($folderId, $gid); + } + /** * @param string $spaceName it's the space name * @return string whithout the blank to start and end of the space name diff --git a/src/services/spaceService.js b/src/services/spaceService.js index 0efb3f4c2..c49508817 100644 --- a/src/services/spaceService.js +++ b/src/services/spaceService.js @@ -101,3 +101,26 @@ export function isSpaceUsers(group) { const SPACE_USER_REGEX = new RegExp('^' + PREFIX_USER) return SPACE_USER_REGEX.test(group) } + +/** + * @param {number} spaceId of a workspace + * @param {string} gid it's an id (string format) of a group + * @return {Promise} + * @throws {AddGroupToGroupfolderError} + */ +export function addGroupToWorkspace(spaceId, gid) { + return axios.post(generateUrl(`/apps/workspace/spaces/${spaceId}/group-attach`), { + gid + }) + .then(resp => { + return resp.data + }) + .catch(error => { + showNotificationError( + 'Error groups', + `Impossible to attach the ${error} group to workspace. May be a problem with the connection ?`, + 5000) + console.error(`Impossible to attach the ${gid} group to workspace. May be a problem with the connection ?`, error) + throw new AddGroupToGroupfolderError('Error to add Space Manager group in the groupfolder') + }) +} diff --git a/src/store/actions.js b/src/store/actions.js index 501e81181..49a06f3ba 100644 --- a/src/store/actions.js +++ b/src/store/actions.js @@ -21,7 +21,7 @@ * */ -import { addGroupToGroupfolder } from '../services/groupfoldersService.js' +import { addGroupToWorkspace } from '../services/spaceService.js' import { generateUrl } from '@nextcloud/router' import { PREFIX_GID_SUBGROUP_SPACE, PREFIX_DISPLAYNAME_SUBGROUP_SPACE } from '../constants.js' import axios from '@nextcloud/axios' @@ -99,7 +99,7 @@ export default { spaceId: space.id }) .then((resp) => { - addGroupToGroupfolder(space.groupfolderId, resp.data.group.gid) + addGroupToWorkspace(space.id, resp.data.group.gid) // Navigates to the g roup's details page context.state.spaces[name].isOpen = true router.push({