-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
Signed-off-by: Hamza Mahjoubi <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Mail\Controller; | ||
|
||
use OCA\Mail\AppInfo\Application; | ||
use OCA\Mail\Db\SnippetShare; | ||
use OCA\Mail\Http\JsonResponse; | ||
use OCA\Mail\Http\TrapError; | ||
use OCA\Mail\Service\SnippetService; | ||
use OCP\AppFramework\Controller; | ||
use OCP\AppFramework\Http; | ||
use OCP\AppFramework\Http\Attribute\NoAdminRequired; | ||
use OCP\IRequest; | ||
|
||
class SnippetController extends Controller { | ||
private ?string $uid; | ||
|
||
public function __construct( | ||
IRequest $request, | ||
?string $userId, | ||
private SnippetService $snippetService, | ||
) { | ||
parent::__construct(Application::APP_ID, $request); | ||
|
||
$this->snippetService = $snippetService; | ||
$this->uid = $userId; | ||
} | ||
|
||
/** | ||
* @NoAdminRequired | ||
* | ||
* @return JsonResponse | ||
*/ | ||
#[TrapError] | ||
public function getOwnSnippets(): JsonResponse { | ||
$snippets = $this->snippetService->findAll($this->uid); | ||
Check failure on line 43 in lib/Controller/SnippetController.php GitHub Actions / static-psalm-analysis dev-masterPossiblyNullArgument
|
||
|
||
return JsonResponse::success($snippets); | ||
} | ||
|
||
/** | ||
* @NoAdminRequired | ||
* | ||
* @return JsonResponse | ||
*/ | ||
#[TrapError] | ||
public function getSharedSnippets(): JsonResponse { | ||
$snippets = $this->snippetService->findAllSharedWithMe($this->uid); | ||
Check failure on line 55 in lib/Controller/SnippetController.php GitHub Actions / static-psalm-analysis dev-masterPossiblyNullArgument
|
||
|
||
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); | ||
Check failure on line 88 in lib/Controller/SnippetController.php GitHub Actions / static-psalm-analysis dev-masterPossiblyNullArgument
|
||
|
||
if ($snippet === null) { | ||
Check failure on line 90 in lib/Controller/SnippetController.php GitHub Actions / static-psalm-analysis dev-masterTypeDoesNotContainNull
|
||
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); | ||
Check failure on line 101 in lib/Controller/SnippetController.php GitHub Actions / static-psalm-analysis dev-masterPossiblyNullArgument
|
||
|
||
if ($snippet === null) { | ||
Check failure on line 103 in lib/Controller/SnippetController.php GitHub Actions / static-psalm-analysis dev-masterTypeDoesNotContainNull
|
||
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); | ||
} | ||
|
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
Check failure on line 53 in lib/Service/SnippetService.php GitHub Actions / static-psalm-analysis dev-masterInvalidDocblock
|
||
// TODO: add groupshares | ||
return $this->snippetShareMapper->findAllSharedWith($userId); | ||
$groups = $this->groupManager->getUserGroupIds($userId); | ||
Check failure on line 54 in lib/Service/SnippetService.php GitHub Actions / static-psalm-analysis dev-masterInvalidArgument
|
||
return $this->snippetShareMapper->findSharedWithMe($userId, $groups); | ||
Check failure on line 55 in lib/Service/SnippetService.php GitHub Actions / static-psalm-analysis dev-masterUndefinedMethod
|
||
} | ||
/** | ||
* @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,29 +120,24 @@ 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); | ||
$share->setType(SnippetShare::TYPE_GROUP); | ||
$this->snippetShareMapper->insert($share); | ||
} | ||
|
||
public function unshare(int $snippetId, string $shareWith): void { | ||
$share = $this->snippetShareMapper->find($snippetId, $shareWith); | ||
$this->snippetShareMapper->delete($share); | ||
} | ||
|
||
|
||
} |