Skip to content

Commit 1431fc2

Browse files
committed
fixup! fixup! feat: add delegation backend
Signed-off-by: Hamza <hamzamahjoubi221@gmail.com>
1 parent 0f31e46 commit 1431fc2

9 files changed

Lines changed: 22 additions & 97 deletions

lib/Controller/AccountApiController.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,8 @@ public function list(): DataResponse {
7171
}, $accounts);
7272

7373
$delegatedAccounts = $this->accountService->findDelegatedAccounts($userId);
74-
foreach ($delegatedAccounts as $mailAccount) {
75-
$account = new Account($mailAccount);
76-
$ownerUserId = $mailAccount->getUserId();
74+
foreach ($delegatedAccounts as $account) {
75+
$ownerUserId = $account->getUserId();
7776
$aliases = $this->aliasesService->findAll($account->getId(), $ownerUserId);
7877
$result[] = [
7978
'id' => $account->getId(),

lib/Controller/AccountsController.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ public function index(): JSONResponse {
109109
$conf['aliases'] = $this->aliasesService->findAll($conf['accountId'], $delegatedAccount->getUserId());
110110
$json[] = $conf;
111111
}
112-
113112
return new JSONResponse($json);
114113
}
115114

lib/Controller/DelegationController.php

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,16 @@ public function __construct(
3737
}
3838

3939
/**
40-
* Get all users that have delegation for a given account
41-
*
4240
* @NoAdminRequired
4341
*
4442
* @param int $accountId
4543
* @return JSONResponse
4644
*/
4745
#[TrapError]
4846
public function getDelegatedUsers(int $accountId): JSONResponse {
49-
if ($this->currentUserId === null) {
50-
return new JSONResponse([], Http::STATUS_UNAUTHORIZED);
51-
}
52-
5347
$account = $this->accountService->findById($accountId);
5448
if ($account->getUserId() !== $this->currentUserId) {
55-
return new JSONResponse([], Http::STATUS_NOT_FOUND);
49+
return new JSONResponse([], Http::STATUS_UNAUTHORIZED);
5650
}
5751

5852
return new JSONResponse(
@@ -61,8 +55,6 @@ public function getDelegatedUsers(int $accountId): JSONResponse {
6155
}
6256

6357
/**
64-
* Delegate an account to a user
65-
*
6658
* @NoAdminRequired
6759
*
6860
* @param int $accountId
@@ -71,13 +63,10 @@ public function getDelegatedUsers(int $accountId): JSONResponse {
7163
*/
7264
#[TrapError]
7365
public function delegate(int $accountId, string $userId): JSONResponse {
74-
if ($this->currentUserId === null) {
75-
return new JSONResponse([], Http::STATUS_UNAUTHORIZED);
76-
}
7766

7867
$account = $this->accountService->findById($accountId);
7968
if ($account->getUserId() !== $this->currentUserId) {
80-
return new JSONResponse([], Http::STATUS_NOT_FOUND);
69+
return new JSONResponse([], Http::STATUS_UNAUTHORIZED);
8170
}
8271

8372
if ($userId === $this->currentUserId) {
@@ -98,8 +87,6 @@ public function delegate(int $accountId, string $userId): JSONResponse {
9887
}
9988

10089
/**
101-
* Revoke delegation of an account for a user
102-
*
10390
* @NoAdminRequired
10491
*
10592
* @param int $accountId
@@ -108,13 +95,9 @@ public function delegate(int $accountId, string $userId): JSONResponse {
10895
*/
10996
#[TrapError]
11097
public function unDelegate(int $accountId, string $userId): JSONResponse {
111-
if ($this->currentUserId === null) {
112-
return new JSONResponse([], Http::STATUS_UNAUTHORIZED);
113-
}
114-
11598
$account = $this->accountService->findById($accountId);
11699
if ($account->getUserId() !== $this->currentUserId) {
117-
return new JSONResponse([], Http::STATUS_NOT_FOUND);
100+
return new JSONResponse([], Http::STATUS_UNAUTHORIZED);
118101
}
119102

120103
$this->delegationService->unDelegate($accountId, $userId);

lib/Controller/FilterController.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use OCA\Mail\Service\DelegationService;
1515
use OCA\Mail\Service\FilterService;
1616
use OCP\AppFramework\Controller;
17+
use OCP\AppFramework\Http;
1718
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
1819
use OCP\AppFramework\Http\Attribute\Route;
1920
use OCP\AppFramework\Http\JSONResponse;
@@ -41,8 +42,11 @@ public function __construct(
4142
#[NoAdminRequired]
4243
public function getFilters(int $accountId): JSONResponse {
4344
$effectiveUserId = $this->delegationService->resolveAccountUserId($accountId, $this->currentUserId);
44-
$account = $this->accountService->find($effectiveUserId, $accountId);
45+
$account = $this->accountService->findById($accountId);
4546

47+
if ($account->getUserId() !== $effectiveUserId) {
48+
return new JSONResponse([], Http::STATUS_NOT_FOUND);
49+
}
4650
$result = $this->mailFilterService->parse($account->getMailAccount());
4751

4852
return new JSONResponse($result->getFilters());
@@ -55,7 +59,11 @@ public function getFilters(int $accountId): JSONResponse {
5559
#[NoAdminRequired]
5660
public function updateFilters(int $accountId, array $filters): JSONResponse {
5761
$effectiveUserId = $this->delegationService->resolveAccountUserId($accountId, $this->currentUserId);
58-
$account = $this->accountService->find($effectiveUserId, $accountId);
62+
$account = $this->accountService->findById($accountId);
63+
64+
if ($account->getUserId() !== $effectiveUserId) {
65+
return new JSONResponse([], Http::STATUS_NOT_FOUND);
66+
}
5967

6068
$this->mailFilterService->update($account->getMailAccount(), $filters);
6169

lib/Db/AliasMapper.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,24 +142,14 @@ public function deleteProvisionedAliasesByUid(string $uid): void {
142142
}
143143

144144
/**
145-
* Find the account ID for a given alias ID without ownership checks.
146-
*
147145
* @throws DoesNotExistException
148146
*/
149147
public function findAccountIdForAlias(int $aliasId): int {
150148
$qb = $this->db->getQueryBuilder();
151149
$qb->select('account_id')
152150
->from($this->getTableName())
153151
->where($qb->expr()->eq('id', $qb->createNamedParameter($aliasId)));
154-
155-
$result = $qb->executeQuery();
156-
$row = $result->fetch();
157-
$result->closeCursor();
158-
159-
if ($row === false) {
160-
throw new DoesNotExistException('Alias not found');
161-
}
162-
152+
$row = $this->findOneQuery($qb);
163153
return (int)$row['account_id'];
164154
}
165155

lib/Db/LocalMessageMapper.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,24 +105,15 @@ public function findById(int $id, string $userId, int $type): LocalMessage {
105105
}
106106

107107
/**
108-
* Find the account ID for a local message without ownership checks.
109-
*
110-
* @throws DoesNotExistException if the local message does not exist
108+
* @throws DoesNotExistException
111109
*/
112110
public function findAccountIdForLocalMessage(int $localMessageId): int {
113111
$qb = $this->db->getQueryBuilder();
114112
$qb->select('account_id')
115113
->from($this->getTableName())
116114
->where($qb->expr()->eq('id', $qb->createNamedParameter($localMessageId, IQueryBuilder::PARAM_INT)));
117115

118-
$result = $qb->executeQuery();
119-
$row = $result->fetch();
120-
$result->closeCursor();
121-
122-
if ($row === false) {
123-
throw new DoesNotExistException('Local message not found');
124-
}
125-
116+
$row = $this->findOneQuery($qb);
126117
return (int)$row['account_id'];
127118
}
128119

lib/Db/MailboxMapper.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -158,24 +158,15 @@ public function findByUid(int $id, string $uid): Mailbox {
158158
}
159159

160160
/**
161-
* Find the account ID for a mailbox without ownership checks.
162-
*
163-
* @throws DoesNotExistException if the mailbox does not exist
161+
* @throws DoesNotExistException
164162
*/
165163
public function findAccountIdForMailbox(int $mailboxId): int {
166164
$qb = $this->db->getQueryBuilder();
167165
$qb->select('account_id')
168166
->from($this->getTableName())
169167
->where($qb->expr()->eq('id', $qb->createNamedParameter($mailboxId, IQueryBuilder::PARAM_INT)));
170168

171-
$result = $qb->executeQuery();
172-
$row = $result->fetch();
173-
$result->closeCursor();
174-
175-
if ($row === false) {
176-
throw new DoesNotExistException("Mailbox $mailboxId does not exist");
177-
}
178-
169+
$row = $this->findOneQuery($qb);
179170
return (int)$row['account_id'];
180171
}
181172

lib/Db/MessageMapper.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,7 @@ public function findByUserId(string $userId, int $id): Message {
132132
}
133133

134134
/**
135-
* Find the account ID for a message without ownership checks.
136-
*
137-
* @throws DoesNotExistException if the message does not exist
135+
* @throws DoesNotExistException
138136
*/
139137
public function findAccountIdForMessage(int $messageId): int {
140138
$qb = $this->db->getQueryBuilder();
@@ -144,14 +142,7 @@ public function findAccountIdForMessage(int $messageId): int {
144142
$qb->expr()->eq('m.mailbox_id', 'mb.id', IQueryBuilder::PARAM_INT))
145143
->where($qb->expr()->eq('m.id', $qb->createNamedParameter($messageId, IQueryBuilder::PARAM_INT)));
146144

147-
$result = $qb->executeQuery();
148-
$row = $result->fetch();
149-
$result->closeCursor();
150-
151-
if ($row === false) {
152-
throw new DoesNotExistException("Message $messageId does not exist");
153-
}
154-
145+
$row = $this->findOneQuery($qb);
155146
return (int)$row['account_id'];
156147
}
157148

lib/Service/DelegationService.php

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,6 @@ public function unDelegate(int $accountId, string $userId): void {
5858
}
5959

6060
/**
61-
* Resolve the effective user ID for accessing an account.
62-
*
63-
* If the current user owns the account, returns the current user ID.
64-
* If the current user is a delegate, returns the account owner's user ID.
65-
* Throws DoesNotExistException if the user has no access.
66-
*
6761
* @throws DoesNotExistException
6862
*/
6963
public function resolveAccountUserId(int $accountId, string $currentUserId): string {
@@ -75,16 +69,10 @@ public function resolveAccountUserId(int $accountId, string $currentUserId): str
7569
// Not the owner — check delegation
7670
}
7771

78-
// Check if the current user is a delegate
7972
return $this->delegationMapper->findAccountOwnerForDelegatedUser($accountId, $currentUserId);
8073
}
8174

8275
/**
83-
* Resolve the effective user ID for accessing a mailbox.
84-
*
85-
* Looks up the account ID for the given mailbox, then delegates to
86-
* resolveAccountUserId to check ownership or delegation.
87-
*
8876
* @throws DoesNotExistException
8977
*/
9078
public function resolveMailboxUserId(int $mailboxId, string $currentUserId): string {
@@ -93,11 +81,6 @@ public function resolveMailboxUserId(int $mailboxId, string $currentUserId): str
9381
}
9482

9583
/**
96-
* Resolve the effective user ID for accessing a message.
97-
*
98-
* Looks up the account ID for the given message (via its mailbox), then
99-
* delegates to resolveAccountUserId to check ownership or delegation.
100-
*
10184
* @throws DoesNotExistException
10285
*/
10386
public function resolveMessageUserId(int $messageId, string $currentUserId): string {
@@ -106,11 +89,6 @@ public function resolveMessageUserId(int $messageId, string $currentUserId): str
10689
}
10790

10891
/**
109-
* Resolve the effective user ID for accessing an alias.
110-
*
111-
* Looks up the account ID for the given alias, then delegates to
112-
* resolveAccountUserId to check ownership or delegation.
113-
*
11492
* @throws DoesNotExistException
11593
*/
11694
public function resolveAliasUserId(int $aliasId, string $currentUserId): string {
@@ -119,11 +97,6 @@ public function resolveAliasUserId(int $aliasId, string $currentUserId): string
11997
}
12098

12199
/**
122-
* Resolve the effective user ID for accessing a local message (draft/outbox).
123-
*
124-
* Looks up the account ID for the given local message, then delegates to
125-
* resolveAccountUserId to check ownership or delegation.
126-
*
127100
* @throws DoesNotExistException
128101
*/
129102
public function resolveLocalMessageUserId(int $localMessageId, string $currentUserId): string {

0 commit comments

Comments
 (0)