126 lines
4.5 KiB
PHP
126 lines
4.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* SPDX-FileCopyrightText: 2026 F7cloud / F7cloud GmbH and F7cloud contributors
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
|
|
namespace OCA\Mail\Db;
|
|
|
|
use OCP\AppFramework\Db\DoesNotExistException;
|
|
use OCP\AppFramework\Db\QBMapper;
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
|
use OCP\IDBConnection;
|
|
|
|
/**
|
|
* @template-extends QBMapper<MailboxShare>
|
|
*/
|
|
class MailboxShareMapper extends QBMapper {
|
|
|
|
public function __construct(IDBConnection $db) {
|
|
parent::__construct($db, 'mail_mailbox_shares');
|
|
}
|
|
|
|
/**
|
|
* Find a share by id.
|
|
*
|
|
* @throws DoesNotExistException
|
|
*/
|
|
public function find(int $id): MailboxShare {
|
|
$qb = $this->db->getQueryBuilder();
|
|
$qb->select('*')
|
|
->from($this->getTableName())
|
|
->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
|
|
return $this->findEntity($qb);
|
|
}
|
|
|
|
/**
|
|
* Find all shares created by the given owner (for listing "shared by me" or cleanup on account delete).
|
|
*
|
|
* @return MailboxShare[]
|
|
*/
|
|
public function findByOwner(string $ownerUserId): array {
|
|
$qb = $this->db->getQueryBuilder();
|
|
$qb->select('*')
|
|
->from($this->getTableName())
|
|
->where($qb->expr()->eq('owner_user_id', $qb->createNamedParameter($ownerUserId, IQueryBuilder::PARAM_STR)));
|
|
return $this->findEntities($qb);
|
|
}
|
|
|
|
/**
|
|
* Find all shares for a mailbox (owner view: who has access).
|
|
*
|
|
* @return MailboxShare[]
|
|
*/
|
|
public function findByMailbox(string $ownerUserId, int $accountId, int $mailboxId): array {
|
|
$qb = $this->db->getQueryBuilder();
|
|
$qb->select('*')
|
|
->from($this->getTableName())
|
|
->where($qb->expr()->eq('owner_user_id', $qb->createNamedParameter($ownerUserId, IQueryBuilder::PARAM_STR)))
|
|
->andWhere($qb->expr()->eq('account_id', $qb->createNamedParameter($accountId, IQueryBuilder::PARAM_INT)))
|
|
->andWhere($qb->expr()->eq('mailbox_id', $qb->createNamedParameter($mailboxId, IQueryBuilder::PARAM_INT)));
|
|
return $this->findEntities($qb);
|
|
}
|
|
|
|
/**
|
|
* Find all shares where the current user (or a group they belong to) is the recipient.
|
|
* Used for "shared with me" list. Caller must pass resolved user + group ids.
|
|
*
|
|
* @param string[] $userOrGroupIds List of current user id and their group ids (e.g. ['user1', 'group1', 'group2'])
|
|
* @return MailboxShare[]
|
|
*/
|
|
public function findSharedWith(array $userOrGroupIds): array {
|
|
if ($userOrGroupIds === []) {
|
|
return [];
|
|
}
|
|
$qb = $this->db->getQueryBuilder();
|
|
$qb->select('*')
|
|
->from($this->getTableName())
|
|
->where($qb->expr()->in('share_with', $qb->createNamedParameter($userOrGroupIds, IQueryBuilder::PARAM_STR_ARRAY)));
|
|
return $this->findEntities($qb);
|
|
}
|
|
|
|
/**
|
|
* Check if a share already exists (same owner, mailbox, share_type, share_with).
|
|
*/
|
|
public function shareExists(string $ownerUserId, int $accountId, int $mailboxId, string $shareType, string $shareWith): bool {
|
|
$qb = $this->db->getQueryBuilder();
|
|
$qb->select('id')
|
|
->from($this->getTableName())
|
|
->where($qb->expr()->eq('owner_user_id', $qb->createNamedParameter($ownerUserId, IQueryBuilder::PARAM_STR)))
|
|
->andWhere($qb->expr()->eq('account_id', $qb->createNamedParameter($accountId, IQueryBuilder::PARAM_INT)))
|
|
->andWhere($qb->expr()->eq('mailbox_id', $qb->createNamedParameter($mailboxId, IQueryBuilder::PARAM_INT)))
|
|
->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter($shareType, IQueryBuilder::PARAM_STR)))
|
|
->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($shareWith, IQueryBuilder::PARAM_STR)))
|
|
->setMaxResults(1);
|
|
$result = $qb->executeQuery();
|
|
$row = $result->fetch();
|
|
$result->closeCursor();
|
|
return $row !== false;
|
|
}
|
|
|
|
/**
|
|
* Delete share by id. Caller must ensure the current user is the owner.
|
|
*/
|
|
public function deleteById(int $id): void {
|
|
$qb = $this->db->getQueryBuilder();
|
|
$qb->delete($this->getTableName())
|
|
->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
|
|
$qb->executeStatement();
|
|
}
|
|
|
|
/**
|
|
* Delete all shares for a mailbox (e.g. when mailbox is deleted).
|
|
*/
|
|
public function deleteByMailbox(string $ownerUserId, int $accountId, int $mailboxId): void {
|
|
$qb = $this->db->getQueryBuilder();
|
|
$qb->delete($this->getTableName())
|
|
->where($qb->expr()->eq('owner_user_id', $qb->createNamedParameter($ownerUserId, IQueryBuilder::PARAM_STR)))
|
|
->andWhere($qb->expr()->eq('account_id', $qb->createNamedParameter($accountId, IQueryBuilder::PARAM_INT)))
|
|
->andWhere($qb->expr()->eq('mailbox_id', $qb->createNamedParameter($mailboxId, IQueryBuilder::PARAM_INT)));
|
|
$qb->executeStatement();
|
|
}
|
|
}
|