Skip to content

Commit

Permalink
feat(vue,php): Create an API for attaching a group to a workspace
Browse files Browse the repository at this point in the history
Fixes the issue where "Password confirmation is required" by allowing groups to be attached without needing password confirmation.
  • Loading branch information
zak39 committed Oct 16, 2024
1 parent 3bf3f96 commit 5875025
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 8 deletions.
5 changes: 5 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}',
Expand Down
25 changes: 20 additions & 5 deletions lib/Controller/GroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
) {
}

Expand All @@ -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
*
*/
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion lib/Space/SpaceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions src/services/spaceService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
})
}
4 changes: 2 additions & 2 deletions src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit 5875025

Please sign in to comment.