2018-06-11 11:03:28 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2018 Julius Härtl <jus@bitgrid.net>
|
|
|
|
*
|
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
namespace OCA\Deck\Db;
|
|
|
|
|
2018-06-19 19:41:14 +03:00
|
|
|
use OCP\AppFramework\Db\DoesNotExistException;
|
2018-06-11 11:03:28 +03:00
|
|
|
use OCP\AppFramework\Db\Entity;
|
2018-06-19 19:41:14 +03:00
|
|
|
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
|
2018-06-11 11:03:28 +03:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
|
|
|
use OCP\IDBConnection;
|
|
|
|
use OCP\IUserManager;
|
|
|
|
use PDO;
|
|
|
|
|
|
|
|
|
|
|
|
class AttachmentMapper extends DeckMapper implements IPermissionMapper {
|
|
|
|
|
|
|
|
private $cardMapper;
|
|
|
|
private $userManager;
|
2018-06-19 20:40:12 +03:00
|
|
|
private $qb;
|
2018-06-11 11:03:28 +03:00
|
|
|
|
2018-07-15 16:01:01 +03:00
|
|
|
/**
|
|
|
|
* AttachmentMapper constructor.
|
|
|
|
*
|
|
|
|
* @param IDBConnection $db
|
|
|
|
* @param CardMapper $cardMapper
|
|
|
|
* @param IUserManager $userManager
|
|
|
|
*/
|
2018-06-11 11:03:28 +03:00
|
|
|
public function __construct(IDBConnection $db, CardMapper $cardMapper, IUserManager $userManager) {
|
|
|
|
parent::__construct($db, 'deck_attachment', Attachment::class);
|
|
|
|
$this->cardMapper = $cardMapper;
|
|
|
|
$this->userManager = $userManager;
|
|
|
|
$this->qb = $this->db->getQueryBuilder();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $id
|
|
|
|
* @return Entity|Attachment
|
|
|
|
* @throws \OCP\AppFramework\Db\DoesNotExistException
|
|
|
|
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
|
|
|
|
*/
|
|
|
|
public function find($id) {
|
|
|
|
$qb = $this->db->getQueryBuilder();
|
|
|
|
$qb->select('*')
|
|
|
|
->from('deck_attachment')
|
2018-06-19 21:08:02 +03:00
|
|
|
->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
|
2018-06-11 11:03:28 +03:00
|
|
|
|
|
|
|
$cursor = $qb->execute();
|
|
|
|
$row = $cursor->fetch(PDO::FETCH_ASSOC);
|
2018-07-15 16:01:01 +03:00
|
|
|
if($row === false) {
|
|
|
|
$cursor->closeCursor();
|
|
|
|
throw new DoesNotExistException('Did expect one result but found none when executing' . $qb);
|
|
|
|
}
|
|
|
|
|
|
|
|
$row2 = $cursor->fetch();
|
2018-06-11 11:03:28 +03:00
|
|
|
$cursor->closeCursor();
|
2018-07-15 16:01:01 +03:00
|
|
|
if($row2 !== false ) {
|
|
|
|
throw new MultipleObjectsReturnedException('Did not expect more than one result when executing' . $query);
|
|
|
|
}
|
|
|
|
|
2018-06-11 11:03:28 +03:00
|
|
|
return $this->mapRowToEntity($row);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find all attachments for a card
|
|
|
|
*
|
|
|
|
* @param $cardId
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function findAll($cardId) {
|
|
|
|
$qb = $this->db->getQueryBuilder();
|
|
|
|
$qb->select('*')
|
|
|
|
->from('deck_attachment')
|
2018-06-19 21:08:02 +03:00
|
|
|
->where($qb->expr()->eq('card_id', $qb->createNamedParameter($cardId, IQueryBuilder::PARAM_INT)))
|
|
|
|
->andWhere($qb->expr()->eq('deleted_at', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)));
|
2018-06-12 18:14:54 +03:00
|
|
|
|
2018-06-11 11:03:28 +03:00
|
|
|
|
|
|
|
$entities = [];
|
|
|
|
$cursor = $qb->execute();
|
|
|
|
while($row = $cursor->fetch()){
|
|
|
|
$entities[] = $this->mapRowToEntity($row);
|
|
|
|
}
|
|
|
|
$cursor->closeCursor();
|
2018-06-12 16:33:06 +03:00
|
|
|
return $entities;
|
|
|
|
}
|
|
|
|
|
2018-07-15 16:01:01 +03:00
|
|
|
/**
|
|
|
|
* @param null $cardId
|
|
|
|
* @param bool $withOffset
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-06-12 18:14:54 +03:00
|
|
|
public function findToDelete($cardId = null, $withOffset = true) {
|
2018-06-12 16:33:06 +03:00
|
|
|
// add buffer of 5 min
|
|
|
|
$timeLimit = time() - (60 * 5);
|
|
|
|
$qb = $this->db->getQueryBuilder();
|
|
|
|
$qb->select('*')
|
|
|
|
->from('deck_attachment')
|
2018-06-19 21:08:02 +03:00
|
|
|
->where($qb->expr()->gt('deleted_at', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)));
|
2018-06-12 18:14:54 +03:00
|
|
|
if ($withOffset) {
|
|
|
|
$qb
|
2018-06-19 21:08:02 +03:00
|
|
|
->andWhere($qb->expr()->lt('deleted_at', $qb->createNamedParameter($timeLimit, IQueryBuilder::PARAM_INT)));
|
2018-06-12 18:14:54 +03:00
|
|
|
}
|
|
|
|
if ($cardId !== null) {
|
|
|
|
$qb
|
2018-06-19 21:08:02 +03:00
|
|
|
->andWhere($qb->expr()->eq('card_id', $qb->createNamedParameter($cardId, IQueryBuilder::PARAM_INT)));
|
2018-06-12 18:14:54 +03:00
|
|
|
}
|
2018-06-12 16:33:06 +03:00
|
|
|
|
|
|
|
$entities = [];
|
|
|
|
$cursor = $qb->execute();
|
|
|
|
while($row = $cursor->fetch()){
|
|
|
|
$entities[] = $this->mapRowToEntity($row);
|
|
|
|
}
|
|
|
|
$cursor->closeCursor();
|
2018-06-11 11:03:28 +03:00
|
|
|
return $entities;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if $userId is owner of Entity with $id
|
|
|
|
*
|
|
|
|
* @param $userId string userId
|
|
|
|
* @param $id int|string unique entity identifier
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isOwner($userId, $id) {
|
2018-06-19 19:41:14 +03:00
|
|
|
try {
|
|
|
|
$attachment = $this->find($id);
|
|
|
|
return $this->cardMapper->isOwner($userId, $attachment->getCardId());
|
|
|
|
} catch (DoesNotExistException $e) {
|
|
|
|
} catch (MultipleObjectsReturnedException $e) {
|
|
|
|
}
|
|
|
|
return false;
|
2018-06-11 11:03:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Query boardId for Entity of given $id
|
|
|
|
*
|
|
|
|
* @param $id int|string unique entity identifier
|
|
|
|
* @return int|null id of Board
|
|
|
|
*/
|
|
|
|
public function findBoardId($id) {
|
|
|
|
try {
|
|
|
|
$attachment = $this->find($id);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-06-19 19:41:14 +03:00
|
|
|
return $this->cardMapper->findBoardId($attachment->getCardId());
|
2018-06-11 11:03:28 +03:00
|
|
|
}
|
|
|
|
}
|