Skip to content

Commit

Permalink
Merge pull request #1080 from arawa/refactor/create-group-folders-bac…
Browse files Browse the repository at this point in the history
…kend-side

Create group folders backend side
  • Loading branch information
zak39 authored Nov 7, 2024
2 parents b438446 + 5b0a141 commit 43b0c06
Show file tree
Hide file tree
Showing 15 changed files with 616 additions and 90 deletions.
1 change: 0 additions & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
],
[
'name' => 'workspace#createWorkspace',
// TODO move this route to /api/spaces
'url' => '/spaces',
'verb' => 'POST'
],
Expand Down
65 changes: 13 additions & 52 deletions lib/Controller/WorkspaceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
use OCA\Workspace\Db\Space;
use OCA\Workspace\Db\SpaceMapper;
use OCA\Workspace\Exceptions\BadRequestException;
use OCA\Workspace\Exceptions\CreateGroupException;
use OCA\Workspace\Exceptions\CreateWorkspaceException;
use OCA\Workspace\Exceptions\WorkspaceNameExistException;
use OCA\Workspace\Folder\RootFolder;
use OCA\Workspace\Helper\GroupfolderHelper;
use OCA\Workspace\Service\Group\GroupFormatter;
Expand All @@ -41,6 +38,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;
Expand All @@ -64,7 +62,8 @@ public function __construct(
private WorkspaceService $workspaceService,
private UserGroup $userGroup,
private WorkspaceManagerGroup $workspaceManagerGroup,
public $AppName
private SpaceManager $spaceManager,
public $AppName,
) {
parent::__construct($AppName, $request);
}
Expand All @@ -82,56 +81,18 @@ private function deleteBlankSpaceName(string $spaceName): string {
* @NoAdminRequired
* @GeneralManagerRequired
* @param string $spaceName
* @param int $folderId
* @throws BadRequestException
* @throws CreateWorkspaceException
* @throws CreateGroupException
*/
public function createWorkspace(string $spaceName,
int $folderId): JSONResponse {
if ($spaceName === false ||
$spaceName === null ||
$spaceName === ''
) {
throw new BadRequestException('spaceName must be provided');
}

if ($this->workspaceCheck->containSpecialChar($spaceName)) {
throw new BadRequestException('Your Workspace name must not contain the following characters: ' . implode(' ', str_split(WorkspaceCheckService::CHARACTERS_SPECIAL)));
}

if ($this->workspaceCheck->isExist($spaceName)) {
throw new WorkspaceNameExistException("The $spaceName space name already exist", Http::STATUS_CONFLICT);
}
public function createWorkspace(string $spaceName): JSONResponse {

$spaceName = $this->deleteBlankSpaceName($spaceName);
$workspace = $this->spaceManager->create($spaceName);

$space = new Space();
$space->setSpaceName($spaceName);
$space->setGroupfolderId($folderId);
$space->setColorCode('#' . substr(md5(mt_rand()), 0, 6)); // mt_rand() (MT - Mersenne Twister) is taller efficient than rand() function.
$this->spaceMapper->insert($space);

if (is_null($space)) {
throw new CreateWorkspaceException('Error to create a space.', Http::STATUS_CONFLICT);
}

// #2 create groups
$newSpaceManagerGroup = $this->workspaceManagerGroup->create($space);
$newSpaceUsersGroup = $this->userGroup->create($space);

// #3 Returns result
return new JSONResponse([
'name' => $space->getSpaceName(),
'id_space' => $space->getId(),
'folder_id' => $space->getGroupfolderId(),
'color' => $space->getColorCode(),
'groups' => GroupFormatter::formatGroups([
$newSpaceManagerGroup,
$newSpaceUsersGroup
]),
'statuscode' => Http::STATUS_CREATED,
]);
return new JSONResponse(
array_merge(
$workspace,
[ 'statuscode' => Http::STATUS_CREATED ]
)
)
;
}

/**
Expand Down Expand Up @@ -206,7 +167,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;
}
Expand Down
39 changes: 39 additions & 0 deletions lib/Exceptions/AbstractNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* @copyright Copyright (c) 2024 Arawa
*
* @author 2024 Baptiste Fotia <baptiste.fotia@arawa.fr>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Workspace\Exceptions;

abstract class AbstractNotification extends \Exception {
public function __construct(
string $message,
int $code,
protected string $title = 'Error',
) {
parent::__construct($message, $code);
}

public function getTitle(): string {
return $this->title;
}
}
8 changes: 5 additions & 3 deletions lib/Exceptions/WorkspaceNameExistException.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@

namespace OCA\Workspace\Exceptions;

class WorkspaceNameExistException extends \Exception {
public function __construct($message, $code = 0) {
parent::__construct($message, $code);
use OCP\AppFramework\Http;

class WorkspaceNameExistException extends AbstractNotification {
public function __construct($title, $message, $code = Http::STATUS_CONFLICT) {
parent::__construct(title: $title, message: $message, code: $code);
}
}
17 changes: 16 additions & 1 deletion lib/Middleware/IsGeneralManagerMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
namespace OCA\Workspace\Middleware;

use Exception;
use OCA\Workspace\Exceptions\AbstractNotification;
use OCA\Workspace\Middleware\Exceptions\AccessDeniedException;
use OCA\Workspace\Service\UserService;
use OCP\AppFramework\Http;
Expand All @@ -38,7 +39,7 @@ class IsGeneralManagerMiddleware extends Middleware {
public function __construct(
private IControllerMethodReflector $reflector,
private IRequest $request,
private UserService $userService
private UserService $userService,
) {
}

Expand All @@ -59,5 +60,19 @@ public function afterException($controller, $methodName, Exception $exception):
'msg' => 'You are not allowed to perform this action'
], Http::STATUS_FORBIDDEN);
}

if ($exception instanceof AbstractNotification) {
return new JSONResponse([
'title' => $exception->getTitle(),
'statuscode' => $exception->getCode(),
'message' => $exception->getMessage()
], $exception->getCode());
}

return new JSONResponse([
'statuscode' => $exception->getCode(),
'message' => $exception->getMessage(),
'trace' => $exception->getTrace()
], $exception->getCode());
}
}
17 changes: 15 additions & 2 deletions lib/Middleware/IsSpaceAdminMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

namespace OCA\Workspace\Middleware;

use OCA\Workspace\Exceptions\AbstractNotification;
use OCA\Workspace\Middleware\Exceptions\AccessDeniedException;
use OCA\Workspace\Service\SpaceService;
use OCA\Workspace\Service\UserService;
Expand All @@ -39,7 +40,7 @@ public function __construct(
private IControllerMethodReflector $reflector,
private IRequest $request,
private UserService $userService,
private SpaceService $spaceService
private SpaceService $spaceService,
) {
}

Expand All @@ -61,6 +62,18 @@ public function afterException($controller, $methodName, \Exception $exception):
], Http::STATUS_FORBIDDEN);
}

return new JSONResponse([]);
if ($exception instanceof AbstractNotification) {
return new JSONResponse([
'title' => $exception->getTitle(),
'statuscode' => $exception->getCode(),
'message' => $exception->getMessage()
], $exception->getCode());
}

return new JSONResponse([
'statuscode' => $exception->getCode(),
'message' => $exception->getMessage(),
'trace' => $exception->getTrace()
], $exception->getCode());
}
}
32 changes: 32 additions & 0 deletions lib/Service/ColorCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* @copyright Copyright (c) 2024 Arawa
*
* @author 2024 Baptiste Fotia <baptiste.fotia@arawa.fr>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Workspace\Service;

class ColorCode {
public function generate(): string {
// mt_rand() (MT - Mersenne Twister) is taller efficient than rand() function.
return '#' . substr(md5(mt_rand()), 0, 6);
}
}
9 changes: 7 additions & 2 deletions lib/Space/SpaceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use OCA\Workspace\Exceptions\WorkspaceNameExistException;
use OCA\Workspace\Folder\RootFolder;
use OCA\Workspace\Helper\GroupfolderHelper;
use OCA\Workspace\Service\ColorCode;
use OCA\Workspace\Service\Group\GroupFormatter;
use OCA\Workspace\Service\Group\UserGroup;
use OCA\Workspace\Service\Group\WorkspaceManagerGroup;
Expand All @@ -45,6 +46,7 @@ public function __construct(
private UserGroup $userGroup,
private SpaceMapper $spaceMapper,
private WorkspaceManagerGroup $workspaceManagerGroup,
private ColorCode $colorCode,
) {
}

Expand All @@ -61,7 +63,10 @@ public function create(string $spacename): array {
}

if ($this->workspaceCheck->isExist($spacename)) {
throw new WorkspaceNameExistException("The $spacename space name already exist", Http::STATUS_CONFLICT);
throw new WorkspaceNameExistException(
title: 'Error - Duplicate space name',
message: "This space or groupfolder already exist. Please, input another space.\nIf \"toto\" space exist, you cannot create the \"tOTo\" space.\nMake sure you the groupfolder doesn't exist."
);
}

$spacename = $this->deleteBlankSpaceName($spacename);
Expand All @@ -71,7 +76,7 @@ public function create(string $spacename): array {
$space = new Space();
$space->setSpaceName($spacename);
$space->setGroupfolderId($folderId);
$space->setColorCode('#' . substr(md5(mt_rand()), 0, 6)); // mt_rand() (MT - Mersenne Twister) is taller efficient than rand() function.
$space->setColorCode($this->colorCode->generate());
$this->spaceMapper->insert($space);


Expand Down
23 changes: 3 additions & 20 deletions src/LeftSidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@
</template>

<script>
import { createGroupfolder, checkGroupfolderNameExist, enableAcl, addGroupToGroupfolder, addGroupToManageACLForGroupfolder } from './services/groupfoldersService.js'
import { createSpace, deleteBlankSpacename, isSpaceManagers, isSpaceUsers } from './services/spaceService.js'
import { createSpace } from './services/spaceService.js'
import { PATTERN_CHECK_NOTHING_SPECIAL_CHARACTER } from './constants.js'
import BadCreateError from './Errors/BadCreateError.js'
import NcAppNavigation from '@nextcloud/vue/dist/Components/NcAppNavigation.js'
Expand All @@ -81,7 +80,6 @@ export default {
showNotificationError('Error', 'Please specify a name.', 3000)
return
}
name = deleteBlankSpacename(name)

const REGEX_CHECK_NOTHING_SPECIAL_CHARACTER = new RegExp(PATTERN_CHECK_NOTHING_SPECIAL_CHARACTER)

Expand All @@ -92,29 +90,14 @@ export default {
)
}

await checkGroupfolderNameExist(name)

const groupfolderId = await createGroupfolder(name)

await enableAcl(groupfolderId.data.id)

const workspace = await createSpace(name, groupfolderId.data.id)

const GROUPS_WORKSPACE = Object.keys(workspace.groups)
const workspaceManagerGid = GROUPS_WORKSPACE.find(isSpaceManagers)
const workspaceUserGid = GROUPS_WORKSPACE.find(isSpaceUsers)

await addGroupToGroupfolder(workspace.folder_id, workspaceManagerGid)
await addGroupToGroupfolder(workspace.folder_id, workspaceUserGid)

await addGroupToManageACLForGroupfolder(workspace.folder_id, workspaceManagerGid)
const workspace = await createSpace(name, this)

this.$store.commit('addSpace', {
color: workspace.color,
groups: workspace.groups,
isOpen: false,
id: workspace.id_space,
groupfolderId: groupfolderId.data.id,
groupfolderId: workspace.folder_id,
name,
quota: t('workspace', 'unlimited'),
users: {},
Expand Down
Loading

0 comments on commit 43b0c06

Please sign in to comment.