From 5875025707c9d9c3a58ee4919e4ef4d34a90a858 Mon Sep 17 00:00:00 2001 From: zak39 Date: Tue, 15 Oct 2024 14:32:12 +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 | 25 ++++++++++++++++++++----- lib/Space/SpaceManager.php | 17 ++++++++++++++++- src/services/spaceService.js | 24 ++++++++++++++++++++++++ src/store/actions.js | 4 ++-- 5 files changed, 67 insertions(+), 8 deletions(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index be9dfdc79..c9c8dad4c 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..1fee10738 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,10 +53,11 @@ public function __construct( private GroupsWorkspaceService $groupsWorkspace, private IGroupManager $groupManager, private LoggerInterface $logger, + private SpaceManager $spaceManager, private IUserManager $userManager, private UserFormatter $userFormatter, private UserService $userService, - private UserWorkspace $userWorkspace + private UserWorkspace $userWorkspace, ) { } @@ -67,9 +69,9 @@ public function __construct( * NB: This function could probably be abused by space managers to create arbitrary group. But, do we really care? * * @var array $data [ - * "gid" => 'Space01', - * "displayName" => 'Space01' - * ] + * "gid" => 'Space01', + * "displayName" => 'Space01' + * ] * @var string $spaceId for Middleware * */ @@ -237,7 +239,7 @@ public function addUser(string $spaceId, string $gid, string $user): JSONRespons public function removeUserFromWorkspace( array|string $space, string $gid, - string $user + string $user, ): JSONResponse { if (gettype($space) === 'string') { $space = json_decode($space, true); @@ -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..fda7d4473 100644 --- a/lib/Space/SpaceManager.php +++ b/lib/Space/SpaceManager.php @@ -57,7 +57,7 @@ public function create(string $spacename): array { } if ($this->workspaceCheck->containSpecialChar($spacename)) { - throw new BadRequestException('Your Workspace name must not contain the following characters: ' . implode(" ", str_split(WorkspaceCheckService::CHARACTERS_SPECIAL))); + throw new BadRequestException('Your Workspace name must not contain the following characters: ' . implode(' ', str_split(WorkspaceCheckService::CHARACTERS_SPECIAL))); } if ($this->workspaceCheck->isExist($spacename)) { @@ -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..f7495137c 100644 --- a/src/services/spaceService.js +++ b/src/services/spaceService.js @@ -26,6 +26,7 @@ import { PREFIX_MANAGER, PREFIX_USER } from '../constants.js' import { generateUrl } from '@nextcloud/router' import BadCreateError from '../Errors/BadCreateError.js' import showNotificationError from './Notifications/NotificationError.js' +import AddGroupToGroupfolderError from '../Errors/Groupfolders/AddGroupToGroupfolderError.js' /** * @param {string} spaceName it's a name for the space to create @@ -101,3 +102,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 group to workspace. May be a problem with the connection ?', gid, 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 cadcb655a..da06bdb0a 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({