|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Mail\Controller; |
| 11 | + |
| 12 | +use OCA\Mail\Exception\DelegationExistsException; |
| 13 | +use OCA\Mail\Http\TrapError; |
| 14 | +use OCA\Mail\Service\AccountService; |
| 15 | +use OCA\Mail\Service\DelegationService; |
| 16 | +use OCP\AppFramework\Controller; |
| 17 | +use OCP\AppFramework\Http; |
| 18 | +use OCP\AppFramework\Http\Attribute\OpenAPI; |
| 19 | +use OCP\AppFramework\Http\JSONResponse; |
| 20 | +use OCP\IRequest; |
| 21 | +use OCP\IUserManager; |
| 22 | + |
| 23 | +#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)] |
| 24 | +class DelegationController extends Controller { |
| 25 | + private ?string $currentUserId; |
| 26 | + |
| 27 | + public function __construct( |
| 28 | + string $appName, |
| 29 | + IRequest $request, |
| 30 | + private DelegationService $delegationService, |
| 31 | + private AccountService $accountService, |
| 32 | + private IUserManager $userManager, |
| 33 | + ?string $UserId, |
| 34 | + ) { |
| 35 | + parent::__construct($appName, $request); |
| 36 | + $this->currentUserId = $UserId; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Get all accounts delegated to the current user |
| 41 | + * |
| 42 | + * @NoAdminRequired |
| 43 | + * |
| 44 | + * @return JSONResponse |
| 45 | + */ |
| 46 | + #[TrapError] |
| 47 | + public function getDelegatedAccounts(): JSONResponse { |
| 48 | + if ($this->currentUserId === null) { |
| 49 | + return new JSONResponse([], Http::STATUS_UNAUTHORIZED); |
| 50 | + } |
| 51 | + |
| 52 | + return new JSONResponse( |
| 53 | + $this->delegationService->findDelegatedAccountForUser($this->currentUserId) |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Get all users that have delegation for a given account |
| 59 | + * |
| 60 | + * @NoAdminRequired |
| 61 | + * |
| 62 | + * @param int $accountId |
| 63 | + * @return JSONResponse |
| 64 | + */ |
| 65 | + #[TrapError] |
| 66 | + public function getDelegatedUsers(int $accountId): JSONResponse { |
| 67 | + if ($this->currentUserId === null) { |
| 68 | + return new JSONResponse([], Http::STATUS_UNAUTHORIZED); |
| 69 | + } |
| 70 | + |
| 71 | + $account = $this->accountService->findById($accountId); |
| 72 | + if ($account->getUserId() !== $this->currentUserId) { |
| 73 | + return new JSONResponse([], Http::STATUS_NOT_FOUND); |
| 74 | + } |
| 75 | + |
| 76 | + return new JSONResponse( |
| 77 | + $this->delegationService->findDelegatedToUsersForAccount($accountId) |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Delegate an account to a user |
| 83 | + * |
| 84 | + * @NoAdminRequired |
| 85 | + * |
| 86 | + * @param int $accountId |
| 87 | + * @param string $userId |
| 88 | + * @return JSONResponse |
| 89 | + */ |
| 90 | + #[TrapError] |
| 91 | + public function delegate(int $accountId, string $userId): JSONResponse { |
| 92 | + if ($this->currentUserId === null) { |
| 93 | + return new JSONResponse([], Http::STATUS_UNAUTHORIZED); |
| 94 | + } |
| 95 | + |
| 96 | + $account = $this->accountService->findById($accountId); |
| 97 | + if ($account->getUserId() !== $this->currentUserId) { |
| 98 | + return new JSONResponse([], Http::STATUS_NOT_FOUND); |
| 99 | + } |
| 100 | + |
| 101 | + if ($userId === $this->currentUserId) { |
| 102 | + return new JSONResponse(['message' => 'Cannot delegate to yourself'], Http::STATUS_BAD_REQUEST); |
| 103 | + } |
| 104 | + |
| 105 | + if (!$this->userManager->userExists($userId)) { |
| 106 | + return new JSONResponse([], Http::STATUS_NOT_FOUND); |
| 107 | + } |
| 108 | + |
| 109 | + try { |
| 110 | + $delegation = $this->delegationService->delegate($accountId, $userId); |
| 111 | + } catch (DelegationExistsException) { |
| 112 | + return new JSONResponse(['message' => 'Delegation already exists'], Http::STATUS_CONFLICT); |
| 113 | + } |
| 114 | + |
| 115 | + return new JSONResponse($delegation, Http::STATUS_CREATED); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Revoke delegation of an account for a user |
| 120 | + * |
| 121 | + * @NoAdminRequired |
| 122 | + * |
| 123 | + * @param int $accountId |
| 124 | + * @param string $userId |
| 125 | + * @return JSONResponse |
| 126 | + */ |
| 127 | + #[TrapError] |
| 128 | + public function unDelegate(int $accountId, string $userId): JSONResponse { |
| 129 | + if ($this->currentUserId === null) { |
| 130 | + return new JSONResponse([], Http::STATUS_UNAUTHORIZED); |
| 131 | + } |
| 132 | + |
| 133 | + $account = $this->accountService->findById($accountId); |
| 134 | + if ($account->getUserId() !== $this->currentUserId) { |
| 135 | + return new JSONResponse([], Http::STATUS_NOT_FOUND); |
| 136 | + } |
| 137 | + |
| 138 | + $this->delegationService->unDelegate($accountId, $userId); |
| 139 | + return new JSONResponse([], Http::STATUS_OK); |
| 140 | + } |
| 141 | +} |
0 commit comments