diff --git a/appinfo/routes.php b/appinfo/routes.php index 755947410..31a6fae5a 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -55,7 +55,6 @@ ], [ 'name' => 'workspace#createWorkspace', - // TODO move this route to /api/spaces 'url' => '/spaces', 'verb' => 'POST' ], diff --git a/lib/Controller/WorkspaceController.php b/lib/Controller/WorkspaceController.php index f92d2946c..bfe6476eb 100644 --- a/lib/Controller/WorkspaceController.php +++ b/lib/Controller/WorkspaceController.php @@ -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; @@ -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; @@ -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); } @@ -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 ] + ) + ) + ; } /** @@ -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; } diff --git a/lib/Exceptions/AbstractNotification.php b/lib/Exceptions/AbstractNotification.php new file mode 100644 index 000000000..3cc39fb79 --- /dev/null +++ b/lib/Exceptions/AbstractNotification.php @@ -0,0 +1,39 @@ + + * + * @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 . + * + */ + +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; + } +} diff --git a/lib/Exceptions/WorkspaceNameExistException.php b/lib/Exceptions/WorkspaceNameExistException.php index b56ad93f7..8c0e51ad1 100644 --- a/lib/Exceptions/WorkspaceNameExistException.php +++ b/lib/Exceptions/WorkspaceNameExistException.php @@ -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); } } diff --git a/lib/Middleware/IsGeneralManagerMiddleware.php b/lib/Middleware/IsGeneralManagerMiddleware.php index 346899770..3ea7c76e9 100644 --- a/lib/Middleware/IsGeneralManagerMiddleware.php +++ b/lib/Middleware/IsGeneralManagerMiddleware.php @@ -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; @@ -38,7 +39,7 @@ class IsGeneralManagerMiddleware extends Middleware { public function __construct( private IControllerMethodReflector $reflector, private IRequest $request, - private UserService $userService + private UserService $userService, ) { } @@ -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()); } } diff --git a/lib/Middleware/IsSpaceAdminMiddleware.php b/lib/Middleware/IsSpaceAdminMiddleware.php index 269a04068..4759e778a 100644 --- a/lib/Middleware/IsSpaceAdminMiddleware.php +++ b/lib/Middleware/IsSpaceAdminMiddleware.php @@ -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; @@ -39,7 +40,7 @@ public function __construct( private IControllerMethodReflector $reflector, private IRequest $request, private UserService $userService, - private SpaceService $spaceService + private SpaceService $spaceService, ) { } @@ -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()); } } diff --git a/lib/Service/ColorCode.php b/lib/Service/ColorCode.php new file mode 100644 index 000000000..047ea1a8a --- /dev/null +++ b/lib/Service/ColorCode.php @@ -0,0 +1,32 @@ + + * + * @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 . + * + */ + +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); + } +} diff --git a/lib/Space/SpaceManager.php b/lib/Space/SpaceManager.php index fda7d4473..742763220 100644 --- a/lib/Space/SpaceManager.php +++ b/lib/Space/SpaceManager.php @@ -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; @@ -45,6 +46,7 @@ public function __construct( private UserGroup $userGroup, private SpaceMapper $spaceMapper, private WorkspaceManagerGroup $workspaceManagerGroup, + private ColorCode $colorCode, ) { } @@ -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); @@ -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); diff --git a/src/LeftSidebar.vue b/src/LeftSidebar.vue index e05fd1c52..575925169 100644 --- a/src/LeftSidebar.vue +++ b/src/LeftSidebar.vue @@ -56,8 +56,7 @@