Skip to content

Commit b1b7f65

Browse files
committed
fixup! feat: add delegation backend
Signed-off-by: Hamza <hamzamahjoubi221@gmail.com>
1 parent 242c9b7 commit b1b7f65

10 files changed

Lines changed: 149 additions & 63 deletions

lib/Controller/AccountApiController.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use OCA\Mail\ResponseDefinitions;
1515
use OCA\Mail\Service\AccountService;
1616
use OCA\Mail\Service\AliasesService;
17+
use OCA\Mail\Service\DelegationService;
1718
use OCP\AppFramework\Http;
1819
use OCP\AppFramework\Http\Attribute\ApiRoute;
1920
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
@@ -32,6 +33,7 @@ public function __construct(
3233
private readonly ?string $userId,
3334
private readonly AccountService $accountService,
3435
private readonly AliasesService $aliasesService,
36+
private readonly DelegationService $delegationService,
3537
) {
3638
parent::__construct($appName, $request);
3739
}
@@ -54,17 +56,37 @@ public function list(): DataResponse {
5456
}
5557

5658
$accounts = $this->accountService->findByUserId($userId);
57-
return new DataResponse(array_map(function (Account $account) use ($userId) {
59+
$result = array_map(function (Account $account) use ($userId) {
5860
$aliases = $this->aliasesService->findAll($account->getId(), $userId);
5961
return [
6062
'id' => $account->getId(),
6163
'email' => $account->getEmail(),
64+
'isDelegated' => false,
6265
'aliases' => array_map(static fn (Alias $alias) => [
6366
'id' => $alias->getId(),
6467
'email' => $alias->getAlias(),
6568
'name' => $alias->getName(),
6669
], $aliases),
6770
];
68-
}, $accounts));
71+
}, $accounts);
72+
73+
$delegatedAccounts = $this->accountService->findDelegatedAccounts($userId);
74+
foreach ($delegatedAccounts as $mailAccount) {
75+
$account = new Account($mailAccount);
76+
$ownerUserId = $mailAccount->getUserId();
77+
$aliases = $this->aliasesService->findAll($account->getId(), $ownerUserId);
78+
$result[] = [
79+
'id' => $account->getId(),
80+
'email' => $account->getEmail(),
81+
'isDelegated' => true,
82+
'aliases' => array_map(static fn (Alias $alias) => [
83+
'id' => $alias->getId(),
84+
'email' => $alias->getAlias(),
85+
'name' => $alias->getName(),
86+
], $aliases),
87+
];
88+
}
89+
90+
return new DataResponse($result);
6991
}
7092
}

lib/Controller/AliasesController.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use OCA\Mail\Exception\NotImplemented;
1313
use OCA\Mail\Http\TrapError;
1414
use OCA\Mail\Service\AliasesService;
15+
use OCA\Mail\Service\DelegationService;
1516
use OCP\AppFramework\Controller;
1617
use OCP\AppFramework\Db\DoesNotExistException;
1718
use OCP\AppFramework\Http;
@@ -23,14 +24,17 @@
2324
class AliasesController extends Controller {
2425
private AliasesService $aliasService;
2526
private string $currentUserId;
27+
private DelegationService $delegationService;
2628

2729
public function __construct(string $appName,
2830
IRequest $request,
2931
AliasesService $aliasesService,
30-
string $UserId) {
32+
string $UserId,
33+
DelegationService $delegationService) {
3134
parent::__construct($appName, $request);
3235
$this->aliasService = $aliasesService;
3336
$this->currentUserId = $UserId;
37+
$this->delegationService = $delegationService;
3438
}
3539

3640
/**
@@ -42,7 +46,8 @@ public function __construct(string $appName,
4246
*/
4347
#[TrapError]
4448
public function index(int $accountId): JSONResponse {
45-
return new JSONResponse($this->aliasService->findAll($accountId, $this->currentUserId));
49+
$effectiveUserId = $this->delegationService->resolveAccountUserId($accountId, $this->currentUserId);
50+
return new JSONResponse($this->aliasService->findAll($accountId, $effectiveUserId));
4651
}
4752

4853
/**
@@ -64,9 +69,10 @@ public function update(int $id,
6469
string $alias,
6570
string $aliasName,
6671
?int $smimeCertificateId = null): JSONResponse {
72+
$effectiveUserId = $this->delegationService->resolveAliasUserId($id, $this->currentUserId);
6773
return new JSONResponse(
6874
$this->aliasService->update(
69-
$this->currentUserId,
75+
$effectiveUserId,
7076
$id,
7177
$alias,
7278
$aliasName,
@@ -83,7 +89,8 @@ public function update(int $id,
8389
*/
8490
#[TrapError]
8591
public function destroy(int $id): JSONResponse {
86-
return new JSONResponse($this->aliasService->delete($this->currentUserId, $id));
92+
$effectiveUserId = $this->delegationService->resolveAliasUserId($id, $this->currentUserId);
93+
return new JSONResponse($this->aliasService->delete($effectiveUserId, $id));
8794
}
8895

8996
/**
@@ -98,8 +105,9 @@ public function destroy(int $id): JSONResponse {
98105
*/
99106
#[TrapError]
100107
public function create(int $accountId, string $alias, string $aliasName): JSONResponse {
108+
$effectiveUserId = $this->delegationService->resolveAccountUserId($accountId, $this->currentUserId);
101109
return new JSONResponse(
102-
$this->aliasService->create($this->currentUserId, $accountId, $alias, $aliasName),
110+
$this->aliasService->create($effectiveUserId, $accountId, $alias, $aliasName),
103111
Http::STATUS_CREATED
104112
);
105113
}
@@ -115,6 +123,7 @@ public function create(int $accountId, string $alias, string $aliasName): JSONRe
115123
*/
116124
#[TrapError]
117125
public function updateSignature(int $id, ?string $signature = null): JSONResponse {
118-
return new JSONResponse($this->aliasService->updateSignature($this->currentUserId, $id, $signature));
126+
$effectiveUserId = $this->delegationService->resolveAliasUserId($id, $this->currentUserId);
127+
return new JSONResponse($this->aliasService->updateSignature($effectiveUserId, $id, $signature));
119128
}
120129
}

lib/Controller/DelegationController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function __construct(
3030
private DelegationService $delegationService,
3131
private AccountService $accountService,
3232
private IUserManager $userManager,
33-
?string $UserId
33+
?string $UserId,
3434
) {
3535
parent::__construct($appName, $request);
3636
$this->currentUserId = $UserId;

lib/Controller/FilterController.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
use OCA\Mail\AppInfo\Application;
1313
use OCA\Mail\Service\AccountService;
14+
use OCA\Mail\Service\DelegationService;
1415
use OCA\Mail\Service\FilterService;
1516
use OCP\AppFramework\Controller;
16-
use OCP\AppFramework\Http;
1717
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
1818
use OCP\AppFramework\Http\Attribute\Route;
1919
use OCP\AppFramework\Http\JSONResponse;
@@ -27,6 +27,7 @@ public function __construct(
2727
string $userId,
2828
private FilterService $mailFilterService,
2929
private AccountService $accountService,
30+
private DelegationService $delegationService,
3031
) {
3132
parent::__construct(Application::APP_ID, $request);
3233
$this->currentUserId = $userId;
@@ -39,11 +40,8 @@ public function __construct(
3940
#[Route(Route::TYPE_FRONTPAGE, verb: 'GET', url: '/api/filter/{accountId}', requirements: ['accountId' => '[\d]+'])]
4041
#[NoAdminRequired]
4142
public function getFilters(int $accountId): JSONResponse {
42-
$account = $this->accountService->findById($accountId);
43-
44-
if ($account->getUserId() !== $this->currentUserId) {
45-
return new JSONResponse([], Http::STATUS_NOT_FOUND);
46-
}
43+
$effectiveUserId = $this->delegationService->resolveAccountUserId($accountId, $this->currentUserId);
44+
$account = $this->accountService->find($effectiveUserId, $accountId);
4745

4846
$result = $this->mailFilterService->parse($account->getMailAccount());
4947

@@ -56,11 +54,8 @@ public function getFilters(int $accountId): JSONResponse {
5654
#[Route(Route::TYPE_FRONTPAGE, verb: 'PUT', url: '/api/filter/{accountId}', requirements: ['accountId' => '[\d]+'])]
5755
#[NoAdminRequired]
5856
public function updateFilters(int $accountId, array $filters): JSONResponse {
59-
$account = $this->accountService->findById($accountId);
60-
61-
if ($account->getUserId() !== $this->currentUserId) {
62-
return new JSONResponse([], Http::STATUS_NOT_FOUND);
63-
}
57+
$effectiveUserId = $this->delegationService->resolveAccountUserId($accountId, $this->currentUserId);
58+
$account = $this->accountService->find($effectiveUserId, $accountId);
6459

6560
$this->mailFilterService->update($account->getMailAccount(), $filters);
6661

lib/Controller/ListController.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use OCA\Mail\Http\JsonResponse;
1616
use OCA\Mail\IMAP\IMAPClientFactory;
1717
use OCA\Mail\Service\AccountService;
18+
use OCA\Mail\Service\DelegationService;
1819
use OCP\AppFramework\Controller;
1920
use OCP\AppFramework\Db\DoesNotExistException;
2021
use OCP\AppFramework\Http;
@@ -31,14 +32,16 @@ class ListController extends Controller {
3132
private IClientService $httpClientService;
3233
private LoggerInterface $logger;
3334
private ?string $currentUserId;
35+
private DelegationService $delegationService;
3436

3537
public function __construct(IRequest $request,
3638
IMailManager $mailManager,
3739
AccountService $accountService,
3840
IMAPClientFactory $clientFactory,
3941
IClientService $httpClientService,
4042
LoggerInterface $logger,
41-
?string $userId) {
43+
?string $userId,
44+
DelegationService $delegationService) {
4245
parent::__construct(Application::APP_ID, $request);
4346
$this->mailManager = $mailManager;
4447
$this->accountService = $accountService;
@@ -47,6 +50,7 @@ public function __construct(IRequest $request,
4750
$this->httpClientService = $httpClientService;
4851
$this->logger = $logger;
4952
$this->currentUserId = $userId;
53+
$this->delegationService = $delegationService;
5054
}
5155

5256
/**
@@ -59,9 +63,10 @@ public function unsubscribe(int $id): JsonResponse {
5963
}
6064

6165
try {
62-
$message = $this->mailManager->getMessage($this->currentUserId, $id);
63-
$mailbox = $this->mailManager->getMailbox($this->currentUserId, $message->getMailboxId());
64-
$account = $this->accountService->find($this->currentUserId, $mailbox->getAccountId());
66+
$effectiveUserId = $this->delegationService->resolveMessageUserId($id, $this->currentUserId);
67+
$message = $this->mailManager->getMessage($effectiveUserId, $id);
68+
$mailbox = $this->mailManager->getMailbox($effectiveUserId, $message->getMailboxId());
69+
$account = $this->accountService->find($effectiveUserId, $mailbox->getAccountId());
6570
} catch (DoesNotExistException $e) {
6671
return JsonResponse::fail(null, Http::STATUS_NOT_FOUND);
6772
}

lib/Controller/MailboxesApiController.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use OCA\Mail\Contracts\IMailSearch;
1414
use OCA\Mail\ResponseDefinitions;
1515
use OCA\Mail\Service\AccountService;
16+
use OCA\Mail\Service\DelegationService;
1617
use OCP\AppFramework\Db\DoesNotExistException;
1718
use OCP\AppFramework\Http;
1819
use OCP\AppFramework\Http\Attribute\ApiRoute;
@@ -33,6 +34,7 @@ public function __construct(
3334
private IMailManager $mailManager,
3435
private readonly AccountService $accountService,
3536
private IMailSearch $mailSearch,
37+
private DelegationService $delegationService,
3638
) {
3739
parent::__construct($appName, $request);
3840
}
@@ -56,7 +58,8 @@ public function list(int $accountId): DataResponse {
5658
}
5759

5860
try {
59-
$account = $this->accountService->find($userId, $accountId);
61+
$effectiveUserId = $this->delegationService->resolveAccountUserId($accountId, $userId);
62+
$account = $this->accountService->find($effectiveUserId, $accountId);
6063
} catch (DoesNotExistException $e) {
6164
return new DataResponse([], Http::STATUS_NOT_FOUND);
6265
}
@@ -97,8 +100,9 @@ public function listMessages(int $mailboxId,
97100
return new DataResponse([], Http::STATUS_NOT_FOUND);
98101
}
99102
try {
100-
$mailbox = $this->mailManager->getMailbox($userId, $mailboxId);
101-
$account = $this->accountService->find($userId, $mailbox->getAccountId());
103+
$effectiveUserId = $this->delegationService->resolveMailboxUserId($mailboxId, $userId);
104+
$mailbox = $this->mailManager->getMailbox($effectiveUserId, $mailboxId);
105+
$account = $this->accountService->find($effectiveUserId, $mailbox->getAccountId());
102106
} catch (DoesNotExistException $e) {
103107
return new DataResponse([], Http::STATUS_FORBIDDEN);
104108
}

lib/Controller/MessageApiController.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use OCA\Mail\Service\AliasesService;
2222
use OCA\Mail\Service\Attachment\AttachmentService;
2323
use OCA\Mail\Service\Attachment\UploadedFile;
24+
use OCA\Mail\Service\DelegationService;
2425
use OCA\Mail\Service\ItineraryService;
2526
use OCA\Mail\Service\MailManager;
2627
use OCA\Mail\Service\OutboxService;
@@ -66,6 +67,7 @@ public function __construct(
6667
private IDkimService $dkimService,
6768
private ItineraryService $itineraryService,
6869
private TrustedSenderService $trustedSenderService,
70+
private DelegationService $delegationService,
6971
) {
7072
parent::__construct($appName, $request);
7173
$this->userId = $userId;
@@ -120,15 +122,16 @@ public function send(
120122
}
121123

122124
try {
123-
$mailAccount = $this->accountService->find($this->userId, $accountId);
125+
$effectiveUserId = $this->delegationService->resolveAccountUserId($accountId, $this->userId);
126+
$mailAccount = $this->accountService->find($effectiveUserId, $accountId);
124127
} catch (ClientException $e) {
125128
$this->logger->error("Mail account #$accountId not found", ['exception' => $e]);
126129
return new DataResponse('Account not found.', Http::STATUS_NOT_FOUND);
127130
}
128131

129132
if ($fromEmail !== $mailAccount->getEmail()) {
130133
try {
131-
$alias = $this->aliasesService->findByAliasAndUserId($fromEmail, $this->userId);
134+
$alias = $this->aliasesService->findByAliasAndUserId($fromEmail, $effectiveUserId);
132135
} catch (DoesNotExistException $e) {
133136
$this->logger->error("Alias $fromEmail for mail account $accountId not found", ['exception' => $e]);
134137
// Cannot send from this email as it is not configured as an alias
@@ -234,9 +237,10 @@ public function get(int $id): DataResponse {
234237
}
235238

236239
try {
237-
$message = $this->mailManager->getMessage($this->userId, $id);
238-
$mailbox = $this->mailManager->getMailbox($this->userId, $message->getMailboxId());
239-
$account = $this->accountService->find($this->userId, $mailbox->getAccountId());
240+
$effectiveUserId = $this->delegationService->resolveMessageUserId($id, $this->userId);
241+
$message = $this->mailManager->getMessage($effectiveUserId, $id);
242+
$mailbox = $this->mailManager->getMailbox($effectiveUserId, $message->getMailboxId());
243+
$account = $this->accountService->find($effectiveUserId, $mailbox->getAccountId());
240244
} catch (ClientException|DoesNotExistException $e) {
241245
$this->logger->error('Message, Account or Mailbox not found', ['exception' => $e->getMessage()]);
242246
return new DataResponse('Account not found.', Http::STATUS_NOT_FOUND);
@@ -322,9 +326,10 @@ public function getRaw(int $id): DataResponse {
322326
}
323327

324328
try {
325-
$message = $this->mailManager->getMessage($this->userId, $id);
326-
$mailbox = $this->mailManager->getMailbox($this->userId, $message->getMailboxId());
327-
$account = $this->accountService->find($this->userId, $mailbox->getAccountId());
329+
$effectiveUserId = $this->delegationService->resolveMessageUserId($id, $this->userId);
330+
$message = $this->mailManager->getMessage($effectiveUserId, $id);
331+
$mailbox = $this->mailManager->getMailbox($effectiveUserId, $message->getMailboxId());
332+
$account = $this->accountService->find($effectiveUserId, $mailbox->getAccountId());
328333
} catch (ClientException|DoesNotExistException $e) {
329334
$this->logger->error('Message, Account or Mailbox not found', ['exception' => $e->getMessage()]);
330335
return new DataResponse('Message, Account or Mailbox not found', Http::STATUS_NOT_FOUND);
@@ -383,9 +388,10 @@ private function enrichDownloadUrl(int $id, array $attachment): array {
383388
#[TrapError]
384389
public function getAttachment(int $id, string $attachmentId): DataResponse {
385390
try {
386-
$message = $this->mailManager->getMessage($this->userId, $id);
387-
$mailbox = $this->mailManager->getMailbox($this->userId, $message->getMailboxId());
388-
$account = $this->accountService->find($this->userId, $mailbox->getAccountId());
391+
$effectiveUserId = $this->delegationService->resolveMessageUserId($id, $this->userId);
392+
$message = $this->mailManager->getMessage($effectiveUserId, $id);
393+
$mailbox = $this->mailManager->getMailbox($effectiveUserId, $message->getMailboxId());
394+
$account = $this->accountService->find($effectiveUserId, $mailbox->getAccountId());
389395
} catch (DoesNotExistException|ClientException $e) {
390396
return new DataResponse('Message, Account or Mailbox not found', Http::STATUS_NOT_FOUND);
391397
}

0 commit comments

Comments
 (0)