diff --git a/lib/Controller/SnippetController.php b/lib/Controller/SnippetController.php new file mode 100644 index 0000000000..93b6ae38f8 --- /dev/null +++ b/lib/Controller/SnippetController.php @@ -0,0 +1,120 @@ +snippetService = $snippetService; + $this->uid = $userId; + } + + /** + * @NoAdminRequired + * + * @return JsonResponse + */ + #[TrapError] + public function getOwnSnippets(): JsonResponse { + $snippets = $this->snippetService->findAll($this->uid); + + return JsonResponse::success($snippets); + } + + /** + * @NoAdminRequired + * + * @return JsonResponse + */ + #[TrapError] + public function getSharedSnippets(): JsonResponse { + $snippets = $this->snippetService->findAllSharedWithMe($this->uid); + + return JsonResponse::success($snippets); + } + + /** + * @NoAdminRequired + * @param string $title + * @param string $content + * + * @return JsonResponse + */ + #[TrapError] + public function create(string $title, string $content): JsonResponse { + if ($this->uid === null) { + return JsonResponse::error('User not found', Http::STATUS_UNAUTHORIZED); + } + $snippet = $this->snippetService->create($this->uid, $title, $content); + + return JsonResponse::success($snippet, Http::STATUS_CREATED); + } + + /** + * @NoAdminRequired + * @param int $snippetId + * @param string $title + * @param string $content + * + * @return JsonResponse + */ + #[TrapError] + public function update(int $snippetId, string $title, string $content): JsonResponse { + + $snippet = $this->snippetService->find($snippetId, $this->uid); + + if ($snippet === null) { + return JsonResponse::error('Snippet not found', Http::STATUS_NOT_FOUND); + } + + $this->snippetService->update($snippetId, $this->uid, $title, $content); + + return JsonResponse::success($snippet, Http::STATUS_OK); + } + + + public function share(int $snippetId, string $shareWith, string $type): JsonResponse { + $snippet = $this->snippetService->find($snippetId, $this->uid); + + if ($snippet === null) { + return JsonResponse::error('Snippet not found', Http::STATUS_NOT_FOUND); + } + + switch ($type) { + case SnippetShare::TYPE_USER: + $this->snippetService->share($snippetId, $shareWith); + return JsonResponse::success(); + case SnippetShare::TYPE_GROUP: + $this->snippetService->shareWithGroup($snippetId, $shareWith); + return JsonResponse::success(); + default: + return JsonResponse::fail('Invalid share type', Http::STATUS_BAD_REQUEST); + } + + } + +} diff --git a/lib/Db/Snippet.php b/lib/Db/Snippet.php index 40590b4405..0b9f7fdeec 100644 --- a/lib/Db/Snippet.php +++ b/lib/Db/Snippet.php @@ -3,7 +3,7 @@ declare(strict_types=1); /** - * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later */ diff --git a/lib/Db/SnippetMapper.php b/lib/Db/SnippetMapper.php index 2103e5cfa9..5eff3a7bba 100644 --- a/lib/Db/SnippetMapper.php +++ b/lib/Db/SnippetMapper.php @@ -3,7 +3,7 @@ declare(strict_types=1); /** - * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later */ diff --git a/lib/Db/SnippetShare.php b/lib/Db/SnippetShare.php index 8db8cb66ec..990918e3b5 100644 --- a/lib/Db/SnippetShare.php +++ b/lib/Db/SnippetShare.php @@ -3,7 +3,7 @@ declare(strict_types=1); /** - * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later */ diff --git a/lib/Db/SnippetShareMapper.php b/lib/Db/SnippetShareMapper.php index da11c0c8ee..9af37c0741 100644 --- a/lib/Db/SnippetShareMapper.php +++ b/lib/Db/SnippetShareMapper.php @@ -3,7 +3,7 @@ declare(strict_types=1); /** - * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later */ @@ -42,7 +42,7 @@ public function find(int $id, string $owner): SnippetShare { return $this->findEntity($qb); } - public function shareExists(string $snippetId, string $shareWith): bool { + public function shareExists(int $snippetId, string $shareWith): bool { $qb = $this->db->getQueryBuilder(); $qb->select('*') ->from($this->getTableName()) diff --git a/lib/Service/SnippetService.php b/lib/Service/SnippetService.php index 3967d5128b..cc40ceccb5 100644 --- a/lib/Service/SnippetService.php +++ b/lib/Service/SnippetService.php @@ -3,9 +3,8 @@ declare(strict_types=1); /** - * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors - * SPDX-FileCopyrightText: 2016 ownCloud, Inc. - * SPDX-License-Identifier: AGPL-3.0-only + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Mail\Service; @@ -52,8 +51,8 @@ public function findAll(string $userId): array { * @param string */ public function findAllSharedWithMe(string $userId): array { - // TODO: add groupshares - return $this->snippetShareMapper->findAllSharedWith($userId); + $groups = $this->groupManager->getUserGroupIds($userId); + return $this->snippetShareMapper->findSharedWithMe($userId, $groups); } /** * @param int $snippetId @@ -103,17 +102,14 @@ public function delete(int $snippetId, string $userId): void { $snippet = $this->snippetMapper->find($snippetId, $userId); $this->snippetMapper->delete($snippet); } + //TODO: run owner check on controller level - public function share(int $snippetId, string $userId, string $shareWith): void { + public function share(int $snippetId, string $shareWith): void { $sharee = $this->userManager->get($shareWith); if ($sharee === null) { throw new DoesNotExistException('Sharee does not exist'); } - $snippet = $this->snippetMapper->find($snippetId, $userId); - if ($snippet === null) { - throw new DoesNotExistException('Snippet does not exist'); - } if ($this->snippetShareMapper->shareExists($snippetId, $shareWith)) { throw new NotPermittedException('Share already exists'); } @@ -124,22 +120,13 @@ public function share(int $snippetId, string $userId, string $shareWith): void { $this->snippetShareMapper->insert($share); } - //TODO: run owner check on controller level - - public function unshare(int $snippetId, string $shareWith): void { - $share = $this->snippetShareMapper->find($snippetId, $shareWith); - $this->snippetShareMapper->delete($share); - } - - //TODO: run owner check on controller level - public function shareWithGroup(int $snippetId, string $userId, string $groupId): void { - $snippet = $this->snippetMapper->find($snippetId, $userId); - if ($snippet === null) { - throw new DoesNotExistException('Snippet does not exist'); - } + public function shareWithGroup(int $snippetId, string $groupId): void { if (!$this->groupManager->groupExists($groupId)) { throw new DoesNotExistException('Group does not exist'); } + if ($this->snippetShareMapper->shareExists($snippetId, $groupId)) { + throw new NotPermittedException('Share already exists'); + } $share = new SnippetShare(); $share->setShareWith($groupId); $share->setSnippetId($snippetId); @@ -147,6 +134,10 @@ public function shareWithGroup(int $snippetId, string $userId, string $groupId): $this->snippetShareMapper->insert($share); } + public function unshare(int $snippetId, string $shareWith): void { + $share = $this->snippetShareMapper->find($snippetId, $shareWith); + $this->snippetShareMapper->delete($share); + } }