From 7e23aa63bed78a7d937180a5298f2592162195e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Thu, 24 Feb 2022 14:09:22 +0100 Subject: [PATCH] Migrate AclMapper to QBMapper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- lib/Db/AclMapper.php | 48 +++++++++++++++++++------ tests/unit/Service/BoardServiceTest.php | 4 +-- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/lib/Db/AclMapper.php b/lib/Db/AclMapper.php index 4eb6a59c6..36e4ade08 100644 --- a/lib/Db/AclMapper.php +++ b/lib/Db/AclMapper.php @@ -25,23 +25,46 @@ namespace OCA\Deck\Db; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\MultipleObjectsReturnedException; +use OCP\AppFramework\Db\QBMapper; use OCP\IDBConnection; -class AclMapper extends DeckMapper implements IPermissionMapper { +class AclMapper extends QBMapper implements IPermissionMapper { public function __construct(IDBConnection $db) { parent::__construct($db, 'deck_board_acl', Acl::class); } + public function find($id): Acl { + $query = $this->db->getQueryBuilder(); + $query->select('*') + ->from($this->getTableName()) + ->where($query->expr()->eq('id', $query->createNamedParameter($id))); + + return $this->findEntity($query); + } + public function findAll($boardId, $limit = null, $offset = null) { - $sql = 'SELECT id, board_id, type, participant, permission_edit, permission_share, permission_manage FROM `*PREFIX*deck_board_acl` WHERE `board_id` = ? '; - return $this->findEntities($sql, [$boardId], $limit, $offset); + $query = $this->db->getQueryBuilder(); + $query->select('*') + ->from($this->getTableName()) + ->where($query->expr()->eq('board_id', $query->createNamedParameter($boardId))) + ->setMaxResults($limit) + ->setFirstResult($offset); + + return $this->findEntities($query); } public function isOwner($userId, $aclId): bool { - $sql = 'SELECT owner FROM `*PREFIX*deck_boards` WHERE `id` IN (SELECT board_id FROM `*PREFIX*deck_board_acl` WHERE id = ?)'; - $stmt = $this->execute($sql, [$aclId]); - $row = $stmt->fetch(); - return ($row['owner'] === $userId); + $query = $this->db->getQueryBuilder(); + $query->select('owner') + ->from('deck_boards', 'b') + ->innerJoin('b', $this->getTableName(), 'a', $query->expr()->eq('b.id', 'a.board_id')) + ->where($query->expr()->eq('a.id', $query->createNamedParameter($aclId))); + + $cursor = $query->execute(); + $row = $cursor->fetch(); + $cursor->closeCursor(); + + return is_array($row) && $row['owner'] === $userId; } public function findBoardId($id): ?int { @@ -53,8 +76,13 @@ class AclMapper extends DeckMapper implements IPermissionMapper { return null; } - public function findByParticipant($type, $participant): array { - $sql = 'SELECT * from *PREFIX*deck_board_acl WHERE type = ? AND participant = ?'; - return $this->findEntities($sql, [$type, $participant]); + public function findByParticipant(int $type, string $participant): array { + $query = $this->db->getQueryBuilder(); + $query->select('*') + ->from($this->getTableName()) + ->where($query->expr()->eq('type', $query->createNamedParameter($type))) + ->andWhere($query->expr()->eq('type', $query->createNamedParameter($participant))); + + return $this->findEntities($query); } } diff --git a/tests/unit/Service/BoardServiceTest.php b/tests/unit/Service/BoardServiceTest.php index 7dba39b5d..b7c49a64f 100644 --- a/tests/unit/Service/BoardServiceTest.php +++ b/tests/unit/Service/BoardServiceTest.php @@ -391,7 +391,7 @@ class BoardServiceTest extends TestCase { $this->aclMapper->expects($this->once()) ->method('delete') ->with($acl) - ->willReturn(true); - $this->assertTrue($this->service->deleteAcl(123)); + ->willReturn($acl); + $this->assertEquals($acl, $this->service->deleteAcl(123)); } }