Implement collections integration

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl
2019-03-05 14:34:19 +01:00
parent 9149a01949
commit e2be00a18d
15 changed files with 982 additions and 158 deletions

View File

@@ -24,6 +24,7 @@
namespace OCA\Deck\AppInfo;
use OCA\Deck\Activity\CommentEventHandler;
use OCA\Deck\Collaboration\Resources\ResourceProvider;
use OCA\Deck\Db\Acl;
use OCA\Deck\Db\AclMapper;
use OCA\Deck\Db\AssignedUsersMapper;
@@ -32,6 +33,8 @@ use OCA\Deck\Middleware\ExceptionMiddleware;
use OCA\Deck\Notification\Notifier;
use OCP\AppFramework\App;
use OCA\Deck\Middleware\SharingMiddleware;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\Collaboration\Resources\IManager;
use OCP\Comments\CommentsEntityEvent;
use OCP\IGroup;
use OCP\IUser;
@@ -100,6 +103,8 @@ class Application extends App {
}
});
$this->registerCollaborationResources();
}
public function registerNavigationEntry() {
@@ -147,4 +152,16 @@ class Application extends App {
return $this->getContainer()->query(CommentEventHandler::class);
});
}
protected function registerCollaborationResources() {
/**
* Register Collaboration ResourceProvider
*/
/** @var IManager $resourceManager */
$resourceManager = $this->getContainer()->query(IManager::class);
$resourceManager->registerResourceProvider(ResourceProvider::class);
\OC::$server->getEventDispatcher()->addListener('\OCP\Collaboration\Resources::loadAdditionalScripts', function () {
\OCP\Util::addScript('deck', 'build/collections');
});
}
}

View File

@@ -0,0 +1,133 @@
<?php
/**
* @copyright Copyright (c) 2019 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\Collaboration\Resources;
use OCA\Deck\Db\Acl;
use OCA\Deck\Db\Board;
use OCA\Deck\Db\BoardMapper;
use OCA\Deck\Service\PermissionService;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\AppFramework\QueryException;
use OCP\Collaboration\Resources\IManager;
use OCP\Collaboration\Resources\IProvider;
use OCP\Collaboration\Resources\IResource;
use OCP\Collaboration\Resources\ResourceException;
use OCP\IUser;
class ResourceProvider implements IProvider {
const RESOURCE_TYPE = 'deck';
private $boardMapper;
private $permissionService;
/** @var array */
protected $nodes = [];
public function __construct(BoardMapper $boardMapper, PermissionService $permissionService) {
$this->boardMapper = $boardMapper;
$this->permissionService = $permissionService;
}
/**
* Get the type of a resource
*
* @param IResource $resource
* @return string
* @since 15.0.0
*/
public function getType(): string {
return self::RESOURCE_TYPE;
}
/**
* Get the rich object data of a resource
*
* @param IResource $resource
* @return array
* @throws \OCP\AppFramework\Db\DoesNotExistException
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
* @since 16.0.0
*/
public function getResourceRichObject(IResource $resource): array {
$board = $this->getBoard($resource);
$link = \OC::$server->getURLGenerator()->linkToRoute('deck.page.index') . '#!/board/' . $resource->getId();
return [
'type' => self::RESOURCE_TYPE,
'id' => $resource->getId(),
'name' => $board->getTitle(),
'link' => $link,
'iconUrl' => \OC::$server->getURLGenerator()->imagePath('deck', 'deck-dark.svg')
];
}
/**
* Can a user/guest access the collection
*
* @param IResource $resource
* @param IUser|null $user
* @return bool
* @since 16.0.0
*/
public function canAccessResource(IResource $resource, ?IUser $user): bool {
if ($resource->getType() !== self::RESOURCE_TYPE || !$user instanceof IUser) {
return false;
}
$board = $this->getBoard($resource);
if ($board === null) {
return false;
}
if ($board->getOwner() === $user->getUID()) {
return true;
}
return $this->permissionService->userCan($board->getAcl(), Acl::PERMISSION_READ, $user->getUID());
}
private function getBoard(IResource $resource) {
try {
return $this->boardMapper->find($resource->getId(), false, true);
} catch (DoesNotExistException $e) {
} catch (MultipleObjectsReturnedException $e) {
return null;
}
}
public function invalidateAccessCache($boardId = null) {
try {
/** @var IManager $resourceManager */
$resourceManager = \OC::$server->query(IManager::class);
} catch (QueryException $e) {
}
if ($boardId !== null) {
$resource = $resourceManager->getResourceForUser(self::RESOURCE_TYPE, (string)$boardId, null);
$resourceManager->invalidateAccessCacheForResource($resource);
} else {
$resourceManager->invalidateAccessCacheForProvider($this);
}
}
}

View File

@@ -25,6 +25,7 @@ namespace OCA\Deck\Service;
use OCA\Deck\Activity\ActivityManager;
use OCA\Deck\Activity\ChangeSet;
use OCA\Deck\Collaboration\Resources\ResourceProvider;
use OCA\Deck\Db\Acl;
use OCA\Deck\Db\AclMapper;
use OCA\Deck\Db\AssignedUsersMapper;
@@ -59,6 +60,7 @@ class BoardService {
private $userId;
private $activityManager;
private $changeHelper;
private $resourceProvider;
public function __construct(
BoardMapper $boardMapper,
@@ -73,6 +75,7 @@ class BoardService {
IGroupManager $groupManager,
ActivityManager $activityManager,
ChangeHelper $changeHelper,
ResourceProvider $resourceProvider,
$userId
) {
$this->boardMapper = $boardMapper;
@@ -87,6 +90,7 @@ class BoardService {
$this->groupManager = $groupManager;
$this->activityManager = $activityManager;
$this->changeHelper = $changeHelper;
$this->resourceProvider = $resourceProvider;
$this->userId = $userId;
}
@@ -459,6 +463,7 @@ class BoardService {
$this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_BOARD, $newAcl, ActivityManager::SUBJECT_BOARD_SHARE);
$this->boardMapper->mapAcl($newAcl);
$this->changeHelper->boardChanged($boardId);
$this->resourceProvider->invalidateAccessCache($boardId);
return $newAcl;
}
@@ -529,6 +534,7 @@ class BoardService {
}
$this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_BOARD, $acl, ActivityManager::SUBJECT_BOARD_UNSHARE);
$this->changeHelper->boardChanged($acl->getBoardId());
$this->resourceProvider->invalidateAccessCache($acl->getBoardId());
return $this->aclMapper->delete($acl);
}

View File

@@ -127,7 +127,7 @@ class PermissionService {
* @return bool
* @throws NoPermissionException
*/
public function checkPermission($mapper, $id, $permission) {
public function checkPermission($mapper, $id, $permission, $userId = null) {
$boardId = $id;
if ($mapper instanceof IPermissionMapper) {
$boardId = $mapper->findBoardId($id);
@@ -141,12 +141,12 @@ class PermissionService {
return false;
}
if ($this->userIsBoardOwner($boardId)) {
if ($this->userIsBoardOwner($boardId, $userId)) {
return true;
}
$acls = $this->aclMapper->findAll($boardId);
$result = $this->userCan($acls, $permission);
$result = $this->userCan($acls, $permission, $userId);
if ($result) {
return true;
}
@@ -159,10 +159,13 @@ class PermissionService {
* @param $boardId
* @return bool
*/
public function userIsBoardOwner($boardId) {
public function userIsBoardOwner($boardId, $userId = null) {
if ($userId === null) {
$userId = $this->userId;
}
try {
$board = $this->boardMapper->find($boardId);
return $board && $this->userId === $board->getOwner();
return $board && $userId === $board->getOwner();
} catch (DoesNotExistException $e) {
} catch (MultipleObjectsReturnedException $e) {
return false;
@@ -176,17 +179,20 @@ class PermissionService {
* @param $permission
* @return bool
*/
public function userCan(array $acls, $permission) {
public function userCan(array $acls, $permission, $userId = null) {
if ($userId === null) {
$userId = $this->userId;
}
// check for users
foreach ($acls as $acl) {
if ($acl->getType() === Acl::PERMISSION_TYPE_USER && $acl->getParticipant() === $this->userId) {
if ($acl->getType() === Acl::PERMISSION_TYPE_USER && $acl->getParticipant() === $userId) {
return $acl->getPermission($permission);
}
}
// check for groups
$hasGroupPermission = false;
foreach ($acls as $acl) {
if (!$hasGroupPermission && $acl->getType() === Acl::PERMISSION_TYPE_GROUP && $this->groupManager->isInGroup($this->userId, $acl->getParticipant())) {
if (!$hasGroupPermission && $acl->getType() === Acl::PERMISSION_TYPE_GROUP && $this->groupManager->isInGroup($userId, $acl->getParticipant())) {
$hasGroupPermission = $acl->getPermission($permission);
}
}