Implement share provider for deck

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl
2020-12-08 13:15:00 +01:00
parent 9e9515537b
commit e92d34bfcb
8 changed files with 1330 additions and 11 deletions

View File

@@ -26,6 +26,7 @@ namespace OCA\Deck\AppInfo;
use Closure;
use Exception;
use OC\EventDispatcher\SymfonyAdapter;
use OC\Share20\ProviderFactory;
use OCA\Deck\Activity\CommentEventHandler;
use OCA\Deck\Capabilities;
use OCA\Deck\Collaboration\Resources\ResourceProvider;
@@ -43,6 +44,8 @@ use OCA\Deck\Notification\Notifier;
use OCA\Deck\Search\DeckProvider;
use OCA\Deck\Service\FullTextSearchService;
use OCA\Deck\Service\PermissionService;
use OCA\Deck\Sharing\DeckShareProvider;
use OCA\Deck\Sharing\Listener;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
@@ -62,6 +65,8 @@ use OCP\IServerContainer;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Notification\IManager as NotificationManager;
use OCP\Share\IManager;
use OCP\Share\IProviderFactory;
use OCP\Util;
use Psr\Container\ContainerInterface;
@@ -92,6 +97,14 @@ class Application20 extends App implements IBootstrap {
$context->injectFn(Closure::fromCallable([$this, 'registerNotifications']));
$context->injectFn(Closure::fromCallable([$this, 'registerFullTextSearch']));
$context->injectFn(Closure::fromCallable([$this, 'registerCollaborationResources']));
$context->injectFn(function (IManager $shareManager) {
$shareManager->registerShareProvider(DeckShareProvider::class);
});
$context->injectFn(function (Listener $listener, IEventDispatcher $eventDispatcher) {
$listener->register($eventDispatcher);
});
}
public function register(IRegistrationContext $context): void {

View File

@@ -85,6 +85,16 @@ class BoardMapper extends DeckMapper implements IPermissionMapper {
return $board;
}
public function findAllForUser(string $userId, int $since = -1, $includeArchived = true): array {
$groups = $this->groupManager->getUserGroupIds(
$this->userManager->get($userId)
);
$userBoards = $this->findAllByUser($userId, null, null, $since, $includeArchived);
$groupBoards = $this->findAllByGroups($userId, $groups,null, null, $since, $includeArchived);
$circleBoards = $this->findAllByCircles($userId, null, null, $since, $includeArchived);
return array_unique(array_merge($userBoards, $groupBoards, $circleBoards));
}
/**
* Find all boards for a given user
*

View File

@@ -149,7 +149,8 @@ class CardMapper extends QBMapper implements IPermissionMapper {
public function queryCardsByBoards(array $boardIds): IQueryBuilder {
$qb = $this->db->getQueryBuilder();
$qb->select('c.*')
$qb->select('c.*', 's.board_id')
->selectAlias('s.title', 'stack_title')
->from('deck_cards', 'c')
->innerJoin('c', 'deck_stacks', 's', $qb->expr()->eq('s.id', 'c.stack_id'))
->andWhere($qb->expr()->in('s.board_id', $qb->createNamedParameter($boardIds, IQueryBuilder::PARAM_INT_ARRAY)));
@@ -279,6 +280,27 @@ class CardMapper extends QBMapper implements IPermissionMapper {
return $this->findEntities($qb);
}
public function searchRaw($boardIds, $term, $limit = null, $offset = null) {
$qb = $this->queryCardsByBoards($boardIds);
$qb->andWhere($qb->expr()->eq('c.deleted_at', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)));
$qb->andWhere(
$qb->expr()->orX(
$qb->expr()->iLike('c.title', $qb->createNamedParameter('%' . $this->db->escapeLikeParameter($term) . '%')),
$qb->expr()->iLike('c.description', $qb->createNamedParameter('%' . $this->db->escapeLikeParameter($term) . '%'))
)
);
if ($limit !== null) {
$qb->setMaxResults($limit);
}
if ($offset !== null) {
$qb->setFirstResult($offset);
}
$result = $qb->execute();
$all = $result->fetchAll();
$result->closeCursor();
return $all;
}
public function delete(Entity $entity): Entity {
// delete assigned labels
$this->labelMapper->deleteLabelAssignmentsForCard($entity->getId());

View File

@@ -113,13 +113,13 @@ class BoardService {
$this->userId = $userId;
}
public function getUserBoards(int $since = -1, $includeArchived = true): array {
$userInfo = $this->getBoardPrerequisites();
$userBoards = $this->boardMapper->findAllByUser($userInfo['user'], null, null, $since, $includeArchived);
$groupBoards = $this->boardMapper->findAllByGroups($userInfo['user'], $userInfo['groups'],null, null, $since, $includeArchived);
$circleBoards = $this->boardMapper->findAllByCircles($userInfo['user'], null, null, $since, $includeArchived);
return array_unique(array_merge($userBoards, $groupBoards, $circleBoards));
/**
* Get all boards that are shared with a user, their groups or circles
*/
public function getUserBoards(int $since = -1, bool $includeArchived = true): array {
return $this->boardMapper->findAllForUser($this->userId, $since, $includeArchived);
}
/**
* @return array
*/

View File

@@ -29,6 +29,7 @@ namespace OCA\Deck\Service;
use OCA\Deck\Activity\ActivityManager;
use OCA\Deck\Activity\ChangeSet;
use OCA\Deck\Db\AssignmentMapper;
use OCA\Deck\Db\Board;
use OCA\Deck\Db\Card;
use OCA\Deck\Db\CardMapper;
use OCA\Deck\Db\Acl;
@@ -116,9 +117,20 @@ class CardService {
return $cards;
}
public function search($boardIds, $term) {
$cards = $this->cardMapper->search($boardIds, $term);
return $cards;
public function search(string $term, int $limit = null, int $offset = null): array {
$boards = $this->boardService->getUserBoards();
$boardIds = array_map(static function (Board $board) {
return $board->getId();
}, $boards);
return $this->cardMapper->search($boardIds, $term, $limit, $offset);
}
public function searchRaw(string $term, int $limit = null, int $offset = null): array {
$boards = $this->boardService->getUserBoards();
$boardIds = array_map(static function (Board $board) {
return $board->getId();
}, $boards);
return $this->cardMapper->searchRaw($boardIds, $term, $limit, $offset);
}
/**

File diff suppressed because it is too large Load Diff

113
lib/Sharing/Listener.php Normal file
View File

@@ -0,0 +1,113 @@
<?php
/*
* @copyright Copyright (c) 2020 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/>.
*
*/
declare(strict_types=1);
namespace OCA\Deck\Sharing;
use OC\Files\Filesystem;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Share\Events\VerifyMountPointEvent;
use OCP\Share\IShare;
use Symfony\Component\EventDispatcher\GenericEvent;
class Listener {
public function __construct($userId) {
$this->userId = $userId;
}
public function register(IEventDispatcher $dispatcher): void {
/**
* @psalm-suppress UndefinedClass
*/
$dispatcher->addListener('OCP\Share::preShare', [self::class, 'listenPreShare'], 1000);
$dispatcher->addListener(VerifyMountPointEvent::class, [self::class, 'listenVerifyMountPointEvent'], 1000);
}
public static function listenPreShare(GenericEvent $event): void {
/** @var self $listener */
$listener = \OC::$server->query(self::class);
$listener->overwriteShareTarget($event);
}
public static function listenVerifyMountPointEvent(VerifyMountPointEvent $event): void {
/** @var self $listener */
$listener = \OC::$server->query(self::class);
$listener->overwriteMountPoint($event);
}
public function overwriteShareTarget(GenericEvent $event): void {
/** @var IShare $share */
$share = $event->getSubject();
if ($share->getShareType() !== IShare::TYPE_DECK
&& $share->getShareType() !== DeckShareProvider::SHARE_TYPE_DECK_USER) {
return;
}
$target = DeckShareProvider::DECK_FOLDER_PLACEHOLDER . '/' . $share->getNode()->getName();
$target = Filesystem::normalizePath($target);
$share->setTarget($target);
}
public function overwriteMountPoint(VerifyMountPointEvent $event): void {
$share = $event->getShare();
$view = $event->getView();
if ($share->getShareType() !== IShare::TYPE_DECK
&& $share->getShareType() !== DeckShareProvider::SHARE_TYPE_DECK_USER) {
return;
}
if ($event->getParent() === DeckShareProvider::DECK_FOLDER_PLACEHOLDER) {
try {
$userId = $view->getOwner('/');
} catch (\Exception $e) {
// If we fail to get the owner of the view from the cache,
// e.g. because the user never logged in but a cron job runs
// We fallback to calculating the owner from the root of the view:
if (substr_count($view->getRoot(), '/') >= 2) {
// /37c09aa0-1b92-4cf6-8c66-86d8cac8c1d0/files
[, $userId, ] = explode('/', $view->getRoot(), 3);
} else {
// Something weird is going on, we can't fallback more
// so for now we don't overwrite the share path ¯\_(ツ)_/¯
return;
}
}
$parent = $this->getAttachmentFolder();
$event->setParent($parent);
if (!$event->getView()->is_dir($parent)) {
$event->getView()->mkdir($parent);
}
}
}
private function getAttachmentFolder() {
return \OC::$server->getConfig()->getUserValue($this->userId, 'deck', 'attachment_folder', '/Deck');
}
}

View File

@@ -0,0 +1,112 @@
<?php
/*
* @copyright Copyright (c) 2020 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/>.
*
*/
declare(strict_types=1);
namespace OCA\Deck\Sharing;
use OCA\Deck\Db\CardMapper;
use OCP\IURLGenerator;
use OCP\Share\IShare;
class ShareAPIHelper {
private $urlGenerator;
private $cardMapper;
public function __construct(IURLGenerator $urlGenerator, CardMapper $cardMapper) {
$this->urlGenerator = $urlGenerator;
$this->cardMapper = $cardMapper;
}
public function formatShare(IShare $share): array {
$result = [];
$card = $this->cardMapper->find($share->getSharedWith());
$boardId = $this->cardMapper->findBoardId($card->getId());
$result['share_with'] = $share->getSharedWith();
$result['share_with_displayname'] = $card->getTitle();
$result['share_with_link'] = $this->urlGenerator->linkToRouteAbsolute('deck.page.index') . '#/board/' . $boardId . '/card/' . $card->getId();
return $result;
}
public function createShare(IShare $share, string $shareWith, int $permissions, $expireDate) {
$share->setSharedWith($shareWith);
$share->setPermissions($permissions);
if ($expireDate !== '') {
try {
$expireDate = $this->parseDate($expireDate);
$share->setExpirationDate($expireDate);
} catch (\Exception $e) {
throw new OCSNotFoundException($this->l->t('Invalid date, date format must be YYYY-MM-DD'));
}
}
}
/**
* Make sure that the passed date is valid ISO 8601
* So YYYY-MM-DD
* If not throw an exception
*
* Copied from \OCA\Files_Sharing\Controller\ShareAPIController::parseDate.
*
* @param string $expireDate
* @return \DateTime
* @throws \Exception
*/
private function parseDate(string $expireDate): \DateTime {
try {
$date = $this->timeFactory->getDateTime($expireDate);
} catch (\Exception $e) {
throw new \Exception('Invalid date. Format must be YYYY-MM-DD');
}
if ($date === false) {
throw new \Exception('Invalid date. Format must be YYYY-MM-DD');
}
$date->setTime(0, 0, 0);
return $date;
}
/**
* Returns whether the given user can access the given room share or not.
*
* A user can access a room share only if she is a participant of the room.
*
* @param IShare $share
* @param string $user
* @return bool
*/
public function canAccessShare(IShare $share, string $user): bool {
try {
$this->permissionService->checkPermission($this->cardMapper, $share->getSharedWith(), Acl::PERMISSION_READ, $user);
} catch (NoPermissionException $e) {
return false;
}
return true;
}
}