userId === null) { return JsonResponse::error('User not found', Http::STATUS_UNAUTHORIZED); } try { $mailbox = $this->mailManager->getMailbox($this->userId, $id); } catch (\Throwable $e) { return JsonResponse::error('Mailbox not found', Http::STATUS_NOT_FOUND); } try { $share = $this->mailboxShareService->createShare( $this->userId, $mailbox->getAccountId(), (int)$mailbox->getId(), $shareType, $shareWith, $permission, ); } catch (\InvalidArgumentException $e) { return JsonResponse::fail($e->getMessage(), Http::STATUS_BAD_REQUEST); } return JsonResponse::success($share); } /** * Delete a share (current user must be the owner). * * @return JsonResponse */ #[TrapError] public function destroy(int $shareId): JsonResponse { if ($this->userId === null) { return JsonResponse::error('User not found', Http::STATUS_UNAUTHORIZED); } try { $this->mailboxShareService->deleteShare($shareId, $this->userId); } catch (DoesNotExistException $e) { return JsonResponse::error('Share not found', Http::STATUS_NOT_FOUND); } catch (\InvalidArgumentException $e) { return JsonResponse::error($e->getMessage(), Http::STATUS_FORBIDDEN); } return JsonResponse::success(); } /** * List shares for a mailbox (current user must own the mailbox). * * @return JsonResponse */ #[TrapError] public function index(int $id): JsonResponse { if ($this->userId === null) { return JsonResponse::error('User not found', Http::STATUS_UNAUTHORIZED); } try { $mailbox = $this->mailManager->getMailbox($this->userId, $id); } catch (\Throwable $e) { return JsonResponse::error('Mailbox not found', Http::STATUS_NOT_FOUND); } $shares = $this->mailboxShareService->getSharesForMailbox( $this->userId, $mailbox->getAccountId(), (int)$mailbox->getId(), ); return JsonResponse::success($shares); } /** * List mailboxes shared with the current user. * * @return JsonResponse */ #[NoAdminRequired] #[TrapError] public function sharedWithMe(): JsonResponse { if ($this->userId === null) { return JsonResponse::error('User not found', Http::STATUS_UNAUTHORIZED); } try { $data = $this->mailboxShareService->getSharedWithMe($this->userId); return JsonResponse::success($data); } catch (\Throwable $e) { return JsonResponse::fail( ['message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()], Http::STATUS_INTERNAL_SERVER_ERROR ); } } }