2121use OCA \Mail \Exception \ServiceException ;
2222use OCA \Mail \Http \TrapError ;
2323use OCA \Mail \Service \AccountService ;
24+ use OCA \Mail \Service \DelegationService ;
2425use OCA \Mail \Service \Sync \SyncService ;
2526use OCP \AppFramework \Controller ;
27+ use OCP \AppFramework \Db \DoesNotExistException ;
2628use OCP \AppFramework \Http ;
2729use OCP \AppFramework \Http \Attribute \NoAdminRequired ;
2830use OCP \AppFramework \Http \Attribute \OpenAPI ;
@@ -38,6 +40,7 @@ class MailboxesController extends Controller {
3840 private IMailManager $ mailManager ;
3941 private SyncService $ syncService ;
4042 private ?string $ currentUserId ;
43+ private DelegationService $ delegationService ;
4144
4245 public function __construct (
4346 string $ appName ,
@@ -48,13 +51,15 @@ public function __construct(
4851 SyncService $ syncService ,
4952 private readonly IConfig $ config ,
5053 private readonly ITimeFactory $ timeFactory ,
54+ DelegationService $ delegationService ,
5155 ) {
5256 parent ::__construct ($ appName , $ request );
5357
5458 $ this ->accountService = $ accountService ;
5559 $ this ->currentUserId = $ UserId ;
5660 $ this ->mailManager = $ mailManager ;
5761 $ this ->syncService = $ syncService ;
62+ $ this ->delegationService = $ delegationService ;
5863 }
5964
6065 /**
@@ -74,7 +79,12 @@ public function index(int $accountId, bool $forceSync = false): JSONResponse {
7479 return new JSONResponse ([], Http::STATUS_UNAUTHORIZED );
7580 }
7681
77- $ account = $ this ->accountService ->find ($ this ->currentUserId , $ accountId );
82+ try {
83+ $ effectiveUserId = $ this ->delegationService ->resolveAccountUserId ($ accountId , $ this ->currentUserId );
84+ } catch (DoesNotExistException $ e ) {
85+ return new JSONResponse ([], Http::STATUS_FORBIDDEN );
86+ }
87+ $ account = $ this ->accountService ->find ($ effectiveUserId , $ accountId );
7888
7989 $ mailboxes = $ this ->mailManager ->getMailboxes ($ account , $ forceSync );
8090 return new JSONResponse ([
@@ -102,8 +112,13 @@ public function patch(int $id,
102112 return new JSONResponse ([], Http::STATUS_UNAUTHORIZED );
103113 }
104114
105- $ mailbox = $ this ->mailManager ->getMailbox ($ this ->currentUserId , $ id );
106- $ account = $ this ->accountService ->find ($ this ->currentUserId , $ mailbox ->getAccountId ());
115+ try {
116+ $ effectiveUserId = $ this ->delegationService ->resolveMailboxUserId ($ id , $ this ->currentUserId );
117+ } catch (DoesNotExistException $ e ) {
118+ return new JSONResponse ([], Http::STATUS_FORBIDDEN );
119+ }
120+ $ mailbox = $ this ->mailManager ->getMailbox ($ effectiveUserId , $ id );
121+ $ account = $ this ->accountService ->find ($ effectiveUserId , $ mailbox ->getAccountId ());
107122
108123 if ($ name !== null ) {
109124 $ mailbox = $ this ->mailManager ->renameMailbox (
@@ -148,8 +163,13 @@ public function sync(int $id, array $ids = [], ?int $lastMessageTimestamp = null
148163 return new JSONResponse ([], Http::STATUS_UNAUTHORIZED );
149164 }
150165
151- $ mailbox = $ this ->mailManager ->getMailbox ($ this ->currentUserId , $ id );
152- $ account = $ this ->accountService ->find ($ this ->currentUserId , $ mailbox ->getAccountId ());
166+ try {
167+ $ effectiveUserId = $ this ->delegationService ->resolveMailboxUserId ($ id , $ this ->currentUserId );
168+ } catch (DoesNotExistException $ e ) {
169+ return new JSONResponse ([], Http::STATUS_FORBIDDEN );
170+ }
171+ $ mailbox = $ this ->mailManager ->getMailbox ($ effectiveUserId , $ id );
172+ $ account = $ this ->accountService ->find ($ effectiveUserId , $ mailbox ->getAccountId ());
153173 $ order = $ sortOrder === 'newest ' ? IMailSearch::ORDER_NEWEST_FIRST : IMailSearch::ORDER_OLDEST_FIRST ;
154174
155175 $ this ->config ->setUserValue (
@@ -194,8 +214,13 @@ public function clearCache(int $id): JSONResponse {
194214 return new JSONResponse ([], Http::STATUS_UNAUTHORIZED );
195215 }
196216
197- $ mailbox = $ this ->mailManager ->getMailbox ($ this ->currentUserId , $ id );
198- $ account = $ this ->accountService ->find ($ this ->currentUserId , $ mailbox ->getAccountId ());
217+ try {
218+ $ effectiveUserId = $ this ->delegationService ->resolveMailboxUserId ($ id , $ this ->currentUserId );
219+ } catch (DoesNotExistException $ e ) {
220+ return new JSONResponse ([], Http::STATUS_FORBIDDEN );
221+ }
222+ $ mailbox = $ this ->mailManager ->getMailbox ($ effectiveUserId , $ id );
223+ $ account = $ this ->accountService ->find ($ effectiveUserId , $ mailbox ->getAccountId ());
199224
200225 $ this ->syncService ->clearCache ($ account , $ mailbox );
201226 return new JSONResponse ([]);
@@ -216,8 +241,13 @@ public function markAllAsRead(int $id): JSONResponse {
216241 return new JSONResponse ([], Http::STATUS_UNAUTHORIZED );
217242 }
218243
219- $ mailbox = $ this ->mailManager ->getMailbox ($ this ->currentUserId , $ id );
220- $ account = $ this ->accountService ->find ($ this ->currentUserId , $ mailbox ->getAccountId ());
244+ try {
245+ $ effectiveUserId = $ this ->delegationService ->resolveMailboxUserId ($ id , $ this ->currentUserId );
246+ } catch (DoesNotExistException $ e ) {
247+ return new JSONResponse ([], Http::STATUS_FORBIDDEN );
248+ }
249+ $ mailbox = $ this ->mailManager ->getMailbox ($ effectiveUserId , $ id );
250+ $ account = $ this ->accountService ->find ($ effectiveUserId , $ mailbox ->getAccountId ());
221251
222252 $ this ->mailManager ->markFolderAsRead ($ account , $ mailbox );
223253
@@ -240,7 +270,12 @@ public function stats(int $id): JSONResponse {
240270 return new JSONResponse ([], Http::STATUS_UNAUTHORIZED );
241271 }
242272
243- $ mailbox = $ this ->mailManager ->getMailbox ($ this ->currentUserId , $ id );
273+ try {
274+ $ effectiveUserId = $ this ->delegationService ->resolveMailboxUserId ($ id , $ this ->currentUserId );
275+ } catch (DoesNotExistException $ e ) {
276+ return new JSONResponse ([], Http::STATUS_FORBIDDEN );
277+ }
278+ $ mailbox = $ this ->mailManager ->getMailbox ($ effectiveUserId , $ id );
244279 return new JSONResponse ($ mailbox ->getStats ());
245280 }
246281
@@ -280,7 +315,12 @@ public function create(int $accountId, string $name): JSONResponse {
280315 return new JSONResponse ([], Http::STATUS_UNAUTHORIZED );
281316 }
282317
283- $ account = $ this ->accountService ->find ($ this ->currentUserId , $ accountId );
318+ try {
319+ $ effectiveUserId = $ this ->delegationService ->resolveAccountUserId ($ accountId , $ this ->currentUserId );
320+ } catch (DoesNotExistException $ e ) {
321+ return new JSONResponse ([], Http::STATUS_FORBIDDEN );
322+ }
323+ $ account = $ this ->accountService ->find ($ effectiveUserId , $ accountId );
284324
285325 return new JSONResponse ($ this ->mailManager ->createMailbox ($ account , $ name ));
286326 }
@@ -300,8 +340,13 @@ public function destroy(int $id): JSONResponse {
300340 return new JSONResponse ([], Http::STATUS_UNAUTHORIZED );
301341 }
302342
303- $ mailbox = $ this ->mailManager ->getMailbox ($ this ->currentUserId , $ id );
304- $ account = $ this ->accountService ->find ($ this ->currentUserId , $ mailbox ->getAccountId ());
343+ try {
344+ $ effectiveUserId = $ this ->delegationService ->resolveMailboxUserId ($ id , $ this ->currentUserId );
345+ } catch (DoesNotExistException $ e ) {
346+ return new JSONResponse ([], Http::STATUS_FORBIDDEN );
347+ }
348+ $ mailbox = $ this ->mailManager ->getMailbox ($ effectiveUserId , $ id );
349+ $ account = $ this ->accountService ->find ($ effectiveUserId , $ mailbox ->getAccountId ());
305350
306351 $ this ->mailManager ->deleteMailbox ($ account , $ mailbox );
307352 return new JSONResponse ();
@@ -323,8 +368,13 @@ public function clearMailbox(int $id): JSONResponse {
323368 return new JSONResponse ([], Http::STATUS_UNAUTHORIZED );
324369 }
325370
326- $ mailbox = $ this ->mailManager ->getMailbox ($ this ->currentUserId , $ id );
327- $ account = $ this ->accountService ->find ($ this ->currentUserId , $ mailbox ->getAccountId ());
371+ try {
372+ $ effectiveUserId = $ this ->delegationService ->resolveMailboxUserId ($ id , $ this ->currentUserId );
373+ } catch (DoesNotExistException $ e ) {
374+ return new JSONResponse ([], Http::STATUS_FORBIDDEN );
375+ }
376+ $ mailbox = $ this ->mailManager ->getMailbox ($ effectiveUserId , $ id );
377+ $ account = $ this ->accountService ->find ($ effectiveUserId , $ mailbox ->getAccountId ());
328378
329379 $ this ->mailManager ->clearMailbox ($ account , $ mailbox );
330380 return new JSONResponse ();
@@ -341,8 +391,13 @@ public function repair(int $id): JSONResponse {
341391 return new JSONResponse ([], Http::STATUS_FORBIDDEN );
342392 }
343393
344- $ mailbox = $ this ->mailManager ->getMailbox ($ this ->currentUserId , $ id );
345- $ account = $ this ->accountService ->find ($ this ->currentUserId , $ mailbox ->getAccountId ());
394+ try {
395+ $ effectiveUserId = $ this ->delegationService ->resolveMailboxUserId ($ id , $ this ->currentUserId );
396+ } catch (DoesNotExistException $ e ) {
397+ return new JSONResponse ([], Http::STATUS_FORBIDDEN );
398+ }
399+ $ mailbox = $ this ->mailManager ->getMailbox ($ effectiveUserId , $ id );
400+ $ account = $ this ->accountService ->find ($ effectiveUserId , $ mailbox ->getAccountId ());
346401
347402 $ this ->syncService ->repairSync ($ account , $ mailbox );
348403 return new JsonResponse ();
0 commit comments