-
Notifications
You must be signed in to change notification settings - Fork 316
Expand file tree
/
Copy pathAccountApiController.php
More file actions
91 lines (82 loc) · 2.67 KB
/
AccountApiController.php
File metadata and controls
91 lines (82 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Mail\Controller;
use OCA\Mail\Account;
use OCA\Mail\Db\Alias;
use OCA\Mail\ResponseDefinitions;
use OCA\Mail\Service\AccountService;
use OCA\Mail\Service\AliasesService;
use OCA\Mail\Service\DelegationService;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\ApiRoute;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCSController;
use OCP\IRequest;
/**
* @psalm-import-type MailAccountListResponse from ResponseDefinitions
*/
class AccountApiController extends OCSController {
public function __construct(
string $appName,
IRequest $request,
private readonly ?string $userId,
private readonly AccountService $accountService,
private readonly AliasesService $aliasesService,
private readonly DelegationService $delegationService,
) {
parent::__construct($appName, $request);
}
/**
* List all email accounts and their aliases of the user which is currently logged-in
*
* @return DataResponse<Http::STATUS_OK, list<MailAccountListResponse>, array{}>|DataResponse<Http::STATUS_NOT_FOUND, array{}, array{}>
*
* 200: Account list
* 404: User was not logged in
*/
#[ApiRoute(verb: 'GET', url: '/account/list')]
#[NoAdminRequired]
#[NoCSRFRequired]
public function list(): DataResponse {
$userId = $this->userId;
if ($userId === null) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}
$accounts = $this->accountService->findByUserId($userId);
$result = array_map(function (Account $account) use ($userId) {
$aliases = $this->aliasesService->findAll($account->getId(), $userId);
return [
'id' => $account->getId(),
'email' => $account->getEmail(),
'isDelegated' => false,
'aliases' => array_map(static fn (Alias $alias) => [
'id' => $alias->getId(),
'email' => $alias->getAlias(),
'name' => $alias->getName(),
], $aliases),
];
}, $accounts);
$delegatedAccounts = $this->accountService->findDelegatedAccounts($userId);
foreach ($delegatedAccounts as $account) {
$ownerUserId = $account->getUserId();
$aliases = $this->aliasesService->findAll($account->getId(), $ownerUserId);
$result[] = [
'id' => $account->getId(),
'email' => $account->getEmail(),
'isDelegated' => true,
'aliases' => array_map(static fn (Alias $alias) => [
'id' => $alias->getId(),
'email' => $alias->getAlias(),
'name' => $alias->getName(),
], $aliases),
];
}
return new DataResponse($result);
}
}