Fetch proper data for card resource provider
Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
@@ -27,6 +27,7 @@ namespace OCA\Deck\Collaboration\Resources;
|
||||
use OCA\Deck\Db\Acl;
|
||||
use OCA\Deck\Db\Board;
|
||||
use OCA\Deck\Db\BoardMapper;
|
||||
use OCA\Deck\Db\CardMapper;
|
||||
use OCA\Deck\Service\PermissionService;
|
||||
use OCP\AppFramework\Db\DoesNotExistException;
|
||||
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
|
||||
@@ -35,27 +36,38 @@ use OCP\Collaboration\Resources\IManager;
|
||||
use OCP\Collaboration\Resources\IProvider;
|
||||
use OCP\Collaboration\Resources\IResource;
|
||||
use OCP\Collaboration\Resources\ResourceException;
|
||||
use OCP\IURLGenerator;
|
||||
use OCP\IUser;
|
||||
|
||||
class ResourceProviderCard implements IProvider {
|
||||
|
||||
const RESOURCE_TYPE = 'deck-card';
|
||||
|
||||
/** @var CardMapper */
|
||||
private $cardMapper;
|
||||
|
||||
/** @var BoardMapper */
|
||||
private $boardMapper;
|
||||
|
||||
/** @var PermissionService */
|
||||
private $permissionService;
|
||||
|
||||
/** @var IURLGenerator */
|
||||
private $urlGenerator;
|
||||
|
||||
/** @var array */
|
||||
protected $nodes = [];
|
||||
|
||||
public function __construct(BoardMapper $boardMapper, PermissionService $permissionService) {
|
||||
public function __construct(CardMapper $cardMapper, BoardMapper $boardMapper, PermissionService $permissionService, IURLGenerator $urlGenerator) {
|
||||
$this->cardMapper = $cardMapper;
|
||||
$this->boardMapper = $boardMapper;
|
||||
$this->permissionService = $permissionService;
|
||||
$this->urlGenerator = $urlGenerator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the type of a resource
|
||||
*
|
||||
* @param IResource $resource
|
||||
* @return string
|
||||
* @since 15.0.0
|
||||
*/
|
||||
@@ -68,21 +80,26 @@ class ResourceProviderCard implements IProvider {
|
||||
*
|
||||
* @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();
|
||||
$link = \OC::$server->getURLGenerator()->linkToRoute('deck.page.index') ;
|
||||
try {
|
||||
$card = $this->cardMapper->find($resource->getId());
|
||||
$board = $this->getBoard($resource->getId());
|
||||
} catch (DoesNotExistException $e) {
|
||||
throw new ResourceException('No card found for resource');
|
||||
} catch (MultipleObjectsReturnedException $e) {
|
||||
throw new ResourceException('No unique card found for resource, this should never happen');
|
||||
}
|
||||
|
||||
$link = $this->urlGenerator->linkToRoute('deck.page.index') . '#/board/' . $board->getId() . '/cards/' . $resource->getId();
|
||||
|
||||
return [
|
||||
'type' => self::RESOURCE_TYPE,
|
||||
'id' => $resource->getId(),
|
||||
'name' => $board->getTitle(),
|
||||
'name' => $board->getTitle() . ': ' . $card->getTitle(),
|
||||
'link' => $link,
|
||||
'iconUrl' => \OC::$server->getURLGenerator()->imagePath('deck', 'deck-dark.svg')
|
||||
'iconUrl' => \OC::$server->getURLGenerator()->imagePath('core', 'actions/toggle-pictures.svg')
|
||||
];
|
||||
|
||||
}
|
||||
@@ -96,10 +113,18 @@ class ResourceProviderCard implements IProvider {
|
||||
* @since 16.0.0
|
||||
*/
|
||||
public function canAccessResource(IResource $resource, ?IUser $user): bool {
|
||||
if ($resource->getType() !== self::RESOURCE_TYPE || !$user instanceof IUser) {
|
||||
if (!$user instanceof IUser || $resource->getType() !== self::RESOURCE_TYPE) {
|
||||
return false;
|
||||
}
|
||||
$board = $this->getBoard($resource);
|
||||
try {
|
||||
$boardId = $this->cardMapper->findBoardId($resource->getId());
|
||||
$board = $this->getBoard($boardId);
|
||||
} catch (DoesNotExistException $e) {
|
||||
return false;
|
||||
} catch (MultipleObjectsReturnedException $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($board === null) {
|
||||
return false;
|
||||
}
|
||||
@@ -109,23 +134,25 @@ class ResourceProviderCard implements IProvider {
|
||||
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;
|
||||
}
|
||||
/**
|
||||
* @param $cardId
|
||||
* @return Board
|
||||
* @throws DoesNotExistException
|
||||
* @throws MultipleObjectsReturnedException
|
||||
*/
|
||||
private function getBoard($cardId) {
|
||||
$boardId = $this->cardMapper->findBoardId($cardId);
|
||||
return $this->boardMapper->find($boardId, false, true);
|
||||
}
|
||||
|
||||
public function invalidateAccessCache($boardId = null) {
|
||||
public function invalidateAccessCache($cardId = 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);
|
||||
if ($cardId !== null) {
|
||||
$resource = $resourceManager->getResourceForUser(self::RESOURCE_TYPE, (string)$cardId, null);
|
||||
$resourceManager->invalidateAccessCacheForResource($resource);
|
||||
} else {
|
||||
$resourceManager->invalidateAccessCacheForProvider($this);
|
||||
|
||||
Reference in New Issue
Block a user