Skip to content

Commit 88c13a5

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

3 files changed

Lines changed: 55 additions & 60 deletions

File tree

lib/Db/DelegationMapper.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,25 @@ public function findDelegatedAccountsForUser(string $uid): array {
3030
$qb->expr()->eq('user_id', $qb->createNamedParameter($uid))
3131
);
3232

33-
$rows = $this->findEntities($select);
34-
35-
return $rows;
33+
return $this->findEntities($select);
3634
}
3735

38-
public function findDelegatedToUsers(string $accountId) : array {
39-
$qb = $this->db->getQueryBuilder();
36+
public function findDelegatedToUsers(string $accountId): array {
37+
$qb = $this->db->getQueryBuilder();
4038

4139
$select = $qb->select('user_id')
4240
->from($this->getTableName())
4341
->where(
4442
$qb->expr()->eq('account_id', $qb->createNamedParameter($accountId))
4543
);
4644

47-
$rows = $this->findEntities($select);
48-
return $rows;
49-
}
45+
return $this->findEntities($select);
46+
}
5047

51-
/**
48+
/**
5249
* @throws DoesNotExistException
5350
*/
54-
public function find(int $accountId, string $uid): Actions {
51+
public function find(int $accountId, string $uid): Delegation {
5552
$qb = $this->db->getQueryBuilder();
5653
$qb->select('*')
5754
->from($this->getTableName())

lib/Migration/Version5007Date20260312152753.php

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

1212
use Closure;
1313
use OCP\DB\ISchemaWrapper;
14+
use OCP\DB\Types;
1415
use OCP\Migration\IOutput;
1516
use OCP\Migration\SimpleMigrationStep;
1617
use Override;
@@ -41,18 +42,20 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
4142
'notnull' => true,
4243
'length' => 64,
4344
]);
45+
$table->setPrimaryKey(['id']);
46+
if ($schema->hasTable('mail_accounts')) {
47+
$table->addForeignKeyConstraint(
48+
$schema->getTable('mail_accounts'),
49+
['account_id'],
50+
['id'],
51+
[
52+
'onDelete' => 'CASCADE',
53+
]
54+
);
55+
}
4456
}
45-
$table->setPrimaryKey(['id']);
46-
if ($schema->hasTable('mail_accounts')) {
47-
$table->addForeignKeyConstraint(
48-
$schema->getTable('mail_accounts'),
49-
['account_id'],
50-
['id'],
51-
[
52-
'onDelete' => 'CASCADE',
53-
]
54-
);
55-
}
57+
58+
return $schema;
5659
}
5760

5861
}

lib/Service/DelegationService.php

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,46 @@
44

55
/**
66
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
7-
* SPDX-License-Identifier: AGPL-3.0-only
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
88
*/
99

1010
namespace OCA\Mail\Service;
1111

12+
use OCA\Mail\Db\Delegation;
13+
use OCA\Mail\Db\DelegationMapper;
1214
use OCP\AppFramework\Db\DoesNotExistException;
1315
use Psr\Log\LoggerInterface;
14-
use OCA\Mail\Db\Delegation;
15-
use OCA\Mail\Db\DelegationMapper;
1616

1717
class DelegationService {
1818

19-
20-
public function __construct(
21-
private DelegationMapper $delegationMapper,
22-
private LoggerInterface $logger
23-
){}
24-
25-
public function delegate(int $accountId, string $userId){
26-
$delegation = new Delegation();
27-
$delegation->setAccountId($accountId);
28-
$delegation->setUserId($userId);
29-
$this->delegationMapper->insert($delegation);
30-
}
31-
32-
public function findDelgatedAccountForUser(string $userId): array {
33-
return $this->delegationMapper->findDelegatedAccountsForUser($userId);
34-
}
35-
36-
public function findDelegatedToUsersForAccount(string $accountId): array {
37-
return $this->delegationMapper->findDelegatedToUsers($accountId);
38-
}
39-
40-
public function unDelegate(int $accountId, string $userId) {
41-
try {
42-
$delegation = $this->delegationMapper->find($accountId,$userId);
43-
44-
} catch (DoesNotExistException $e) {
45-
$this->logger->warning("Delegation of account:$accountId not found for user: $userId", ['exception' => $e]);
46-
}
47-
if( !$delegation ){
48-
return;
49-
}
50-
$this->delegationMapper->delete($delegation);
51-
52-
}
53-
54-
}
19+
public function __construct(
20+
private DelegationMapper $delegationMapper,
21+
private LoggerInterface $logger,
22+
) {
23+
}
24+
25+
public function delegate(int $accountId, string $userId): void {
26+
$delegation = new Delegation();
27+
$delegation->setAccountId($accountId);
28+
$delegation->setUserId($userId);
29+
$this->delegationMapper->insert($delegation);
30+
}
31+
32+
public function findDelegatedAccountForUser(string $userId): array {
33+
return $this->delegationMapper->findDelegatedAccountsForUser($userId);
34+
}
35+
36+
public function findDelegatedToUsersForAccount(string $accountId): array {
37+
return $this->delegationMapper->findDelegatedToUsers($accountId);
38+
}
39+
40+
public function unDelegate(int $accountId, string $userId): void {
41+
try {
42+
$delegation = $this->delegationMapper->find($accountId, $userId);
43+
} catch (DoesNotExistException $e) {
44+
$this->logger->warning("Delegation of account:$accountId not found for user: $userId", ['exception' => $e]);
45+
return;
46+
}
47+
$this->delegationMapper->delete($delegation);
48+
}
49+
}

0 commit comments

Comments
 (0)