f7cloud_client/apps/mail/lib/Controller/MailboxShareController.php
root 8b6a0139db f7cloud_client
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-17 22:59:26 +00:00

129 lines
3.5 KiB
PHP

<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 F7cloud GmbH and F7cloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Mail\Controller;
use OCA\Mail\AppInfo\Application;
use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\Http\JsonResponse;
use OCA\Mail\Http\TrapError;
use OCA\Mail\Service\MailboxShareService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\IRequest;
#[NoAdminRequired]
class MailboxShareController extends Controller {
public function __construct(
IRequest $request,
private ?string $userId,
private MailboxShareService $mailboxShareService,
private IMailManager $mailManager,
) {
parent::__construct(Application::APP_ID, $request);
}
/**
* Create a share for a mailbox (current user must own the mailbox).
*
* @return JsonResponse
*/
#[TrapError]
public function create(int $id, string $shareWith, string $shareType, string $permission): 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);
}
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
);
}
}
}