Skip to content

Commit bd3d970

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

4 files changed

Lines changed: 213 additions & 0 deletions

File tree

lib/Db/Delegation.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\Mail\Db;
11+
12+
use JsonSerializable;
13+
use OCP\AppFramework\Db\Entity;
14+
use ReturnTypeWillChange;
15+
16+
/**
17+
* @method int getAccountId()
18+
* @method void setAccountId(int $accountId)
19+
* @method string getUserId()
20+
* @method void setUserId(string $userId)
21+
*/
22+
class Delegation extends Entity implements JsonSerializable {
23+
protected $accountId;
24+
protected $userId;
25+
26+
public function __construct() {
27+
$this->addType('userId', 'string');
28+
$this->addType('accountId', 'integer');
29+
}
30+
31+
#[ReturnTypeWillChange]
32+
public function jsonSerialize() {
33+
return [
34+
'id' => $this->getId(),
35+
'accountId' => $this->getAccountId(),
36+
'userId' => $this->getUserId(),
37+
];
38+
}
39+
}

lib/Db/DelegationMapper.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\Mail\Db;
11+
12+
use OCP\AppFramework\Db\DoesNotExistException;
13+
use OCP\AppFramework\Db\QBMapper;
14+
use OCP\IDBConnection;
15+
16+
/**
17+
* @template-extends QBMapper<Delegation>
18+
*/
19+
class DelegationMapper extends QBMapper {
20+
public function __construct(IDBConnection $db) {
21+
parent::__construct($db, 'mail_delegations');
22+
}
23+
24+
public function findDelegatedAccountsForUser(string $uid): array {
25+
$qb = $this->db->getQueryBuilder();
26+
27+
$select = $qb->select('account_id')
28+
->from($this->getTableName())
29+
->where(
30+
$qb->expr()->eq('user_id', $qb->createNamedParameter($uid))
31+
);
32+
33+
$rows = $this->findEntities($select);
34+
35+
return $rows;
36+
}
37+
38+
public function findDelegatedToUsers(string $accountId) : array {
39+
$qb = $this->db->getQueryBuilder();
40+
41+
$select = $qb->select('user_id')
42+
->from($this->getTableName())
43+
->where(
44+
$qb->expr()->eq('account_id', $qb->createNamedParameter($accountId))
45+
);
46+
47+
$rows = $this->findEntities($select);
48+
return $rows;
49+
}
50+
51+
/**
52+
* @throws DoesNotExistException
53+
*/
54+
public function find(int $accountId, string $uid): Actions {
55+
$qb = $this->db->getQueryBuilder();
56+
$qb->select('*')
57+
->from($this->getTableName())
58+
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($uid)))
59+
->andWhere($qb->expr()->eq('account_id', $qb->createNamedParameter($accountId)));
60+
return $this->findEntity($qb);
61+
}
62+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\Mail\Migration;
11+
12+
use Closure;
13+
use OCP\DB\ISchemaWrapper;
14+
use OCP\Migration\IOutput;
15+
use OCP\Migration\SimpleMigrationStep;
16+
use Override;
17+
18+
class Version5007Date20260312152753 extends SimpleMigrationStep {
19+
20+
/**
21+
* @param IOutput $output
22+
* @param Closure(): ISchemaWrapper $schemaClosure
23+
* @param array $options
24+
* @return null|ISchemaWrapper
25+
*/
26+
#[Override]
27+
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
28+
$schema = $schemaClosure();
29+
30+
if (!$schema->hasTable('mail_delegations')) {
31+
$table = $schema->createTable('mail_delegations');
32+
$table->addColumn('id', Types::INTEGER, [
33+
'autoincrement' => true,
34+
'notnull' => true,
35+
]);
36+
$table->addColumn('account_id', Types::INTEGER, [
37+
'notnull' => true,
38+
'length' => 20,
39+
]);
40+
$table->addColumn('user_id', Types::STRING, [
41+
'notnull' => true,
42+
'length' => 64,
43+
]);
44+
}
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+
}
56+
}
57+
58+
}

lib/Service/DelegationService.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-only
8+
*/
9+
10+
namespace OCA\Mail\Service;
11+
12+
use OCP\AppFramework\Db\DoesNotExistException;
13+
use Psr\Log\LoggerInterface;
14+
use OCA\Mail\Db\Delegation;
15+
use OCA\Mail\Db\DelegationMapper;
16+
17+
class DelegationService {
18+
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+
}

0 commit comments

Comments
 (0)