Merge pull request #1545 from nextcloud/enh/dav-calendars

This commit is contained in:
Julius Härtl
2020-09-15 11:12:52 +02:00
committed by GitHub
19 changed files with 832 additions and 146 deletions

View File

@@ -64,11 +64,9 @@
<provider>OCA\Deck\Activity\DeckProvider</provider> <provider>OCA\Deck\Activity\DeckProvider</provider>
</providers> </providers>
</activity> </activity>
<fulltextsearch> <fulltextsearch>
<provider min-version="16">OCA\Deck\Provider\DeckProvider</provider> <provider min-version="16">OCA\Deck\Provider\DeckProvider</provider>
</fulltextsearch> </fulltextsearch>
<navigations> <navigations>
<navigation> <navigation>
<name>Deck</name> <name>Deck</name>
@@ -77,5 +75,9 @@
<order>10</order> <order>10</order>
</navigation> </navigation>
</navigations> </navigations>
<sabre>
<calendar-plugins>
<plugin>OCA\Deck\DAV\CalendarPlugin</plugin>
</calendar-plugins>
</sabre>
</info> </info>

View File

@@ -26,9 +26,6 @@ return [
'routes' => [ 'routes' => [
['name' => 'page#index', 'url' => '/', 'verb' => 'GET'], ['name' => 'page#index', 'url' => '/', 'verb' => 'GET'],
['name' => 'Config#get', 'url' => '/config', 'verb' => 'GET'],
['name' => 'Config#setValue', 'url' => '/config/{key}', 'verb' => 'POST'],
// boards // boards
['name' => 'board#index', 'url' => '/boards', 'verb' => 'GET'], ['name' => 'board#index', 'url' => '/boards', 'verb' => 'GET'],
['name' => 'board#create', 'url' => '/boards', 'verb' => 'POST'], ['name' => 'board#create', 'url' => '/boards', 'verb' => 'POST'],
@@ -125,17 +122,17 @@ return [
['name' => 'attachment_api#delete', 'url' => '/api/v1.0/boards/{boardId}/stacks/{stackId}/cards/{cardId}/attachments/{attachmentId}', 'verb' => 'DELETE'], ['name' => 'attachment_api#delete', 'url' => '/api/v1.0/boards/{boardId}/stacks/{stackId}/cards/{cardId}/attachments/{attachmentId}', 'verb' => 'DELETE'],
['name' => 'attachment_api#restore', 'url' => '/api/v1.0/boards/{boardId}/stacks/{stackId}/cards/{cardId}/attachments/{attachmentId}/restore', 'verb' => 'PUT'], ['name' => 'attachment_api#restore', 'url' => '/api/v1.0/boards/{boardId}/stacks/{stackId}/cards/{cardId}/attachments/{attachmentId}/restore', 'verb' => 'PUT'],
['name' => 'board_api#preflighted_cors', 'url' => '/api/v1.0/{path}','verb' => 'OPTIONS', 'requirements' => ['path' => '.+']], ['name' => 'board_api#preflighted_cors', 'url' => '/api/v1.0/{path}','verb' => 'OPTIONS', 'requirements' => ['path' => '.+']],
], ],
'ocs' => [ 'ocs' => [
['name' => 'Config#get', 'url' => '/api/v1.0/config', 'verb' => 'GET'],
['name' => 'Config#setValue', 'url' => '/api/v1.0/config/{key}', 'verb' => 'POST'],
['name' => 'comments_api#list', 'url' => '/api/v1.0/cards/{cardId}/comments', 'verb' => 'GET'], ['name' => 'comments_api#list', 'url' => '/api/v1.0/cards/{cardId}/comments', 'verb' => 'GET'],
['name' => 'comments_api#create', 'url' => '/api/v1.0/cards/{cardId}/comments', 'verb' => 'POST'], ['name' => 'comments_api#create', 'url' => '/api/v1.0/cards/{cardId}/comments', 'verb' => 'POST'],
['name' => 'comments_api#update', 'url' => '/api/v1.0/cards/{cardId}/comments/{commentId}', 'verb' => 'PUT'], ['name' => 'comments_api#update', 'url' => '/api/v1.0/cards/{cardId}/comments/{commentId}', 'verb' => 'PUT'],
['name' => 'comments_api#delete', 'url' => '/api/v1.0/cards/{cardId}/comments/{commentId}', 'verb' => 'DELETE'], ['name' => 'comments_api#delete', 'url' => '/api/v1.0/cards/{cardId}/comments/{commentId}', 'verb' => 'DELETE'],
// dashboard
['name' => 'overview_api#upcomingCards', 'url' => '/api/v1.0/overview/upcoming', 'verb' => 'GET'], ['name' => 'overview_api#upcomingCards', 'url' => '/api/v1.0/overview/upcoming', 'verb' => 'GET'],
] ]
]; ];

View File

@@ -23,90 +23,42 @@
namespace OCA\Deck\Controller; namespace OCA\Deck\Controller;
use OCA\Deck\Service\ConfigService;
use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\NotFoundResponse; use OCP\AppFramework\Http\NotFoundResponse;
use OCP\IConfig; use OCP\AppFramework\OCSController;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IRequest; use OCP\IRequest;
use OCP\AppFramework\Controller;
class ConfigController extends Controller { class ConfigController extends OCSController {
private $config; private $configService;
private $userId;
private $groupManager;
public function __construct( public function __construct(
$AppName, $AppName,
IRequest $request, IRequest $request,
IConfig $config, ConfigService $configService
IGroupManager $groupManager,
$userId
) { ) {
parent::__construct($AppName, $request); parent::__construct($AppName, $request);
$this->userId = $userId; $this->configService = $configService;
$this->groupManager = $groupManager;
$this->config = $config;
} }
/** /**
* @NoCSRFRequired * @NoCSRFRequired
* @NoAdminRequired
*/ */
public function get() { public function get(): DataResponse {
$data = [ return new DataResponse($this->configService->getAll());
'groupLimit' => $this->getGroupLimit(),
];
return new DataResponse($data);
} }
/** /**
* @NoCSRFRequired * @NoCSRFRequired
* @NoAdminRequired
*/ */
public function setValue($key, $value) { public function setValue(string $key, $value) {
switch ($key) { $result = $this->configService->set($key, $value);
case 'groupLimit':
$result = $this->setGroupLimit($value);
break;
}
if ($result === null) { if ($result === null) {
return new NotFoundResponse(); return new NotFoundResponse();
} }
return new DataResponse($result); return new DataResponse($result);
} }
private function setGroupLimit($value) {
$groups = [];
foreach ($value as $group) {
$groups[] = $group['id'];
}
$data = implode(',', $groups);
$this->config->setAppValue($this->appName, 'groupLimit', $data);
return $groups;
}
private function getGroupLimitList() {
$value = $this->config->getAppValue($this->appName, 'groupLimit', '');
$groups = explode(',', $value);
if ($value === '') {
return [];
}
return $groups;
}
private function getGroupLimit() {
$groups = $this->getGroupLimitList();
$groups = array_map(function ($groupId) {
/** @var IGroup $groups */
$group = $this->groupManager->get($groupId);
if ($group === null) {
return null;
}
return [
'id' => $group->getGID(),
'displayname' => $group->getDisplayName(),
];
}, $groups);
return array_filter($groups);
}
} }

View File

@@ -24,34 +24,33 @@
namespace OCA\Deck\Controller; namespace OCA\Deck\Controller;
use OCA\Deck\AppInfo\Application; use OCA\Deck\AppInfo\Application;
use OCA\Deck\Service\ConfigService;
use OCA\Deck\Service\PermissionService; use OCA\Deck\Service\PermissionService;
use OCP\AppFramework\Http\ContentSecurityPolicy; use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\IInitialStateService; use OCP\IInitialStateService;
use OCP\IRequest; use OCP\IRequest;
use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Controller; use OCP\AppFramework\Controller;
use OCP\IL10N;
class PageController extends Controller { class PageController extends Controller {
private $permissionService; private $permissionService;
private $userId; private $userId;
private $l10n; private $l10n;
private $initialState; private $initialState;
private $configService;
public function __construct( public function __construct(
$AppName, $AppName,
IRequest $request, IRequest $request,
PermissionService $permissionService, PermissionService $permissionService,
IInitialStateService $initialStateService, IInitialStateService $initialStateService,
IL10N $l10n, ConfigService $configService
$userId
) { ) {
parent::__construct($AppName, $request); parent::__construct($AppName, $request);
$this->userId = $userId;
$this->permissionService = $permissionService; $this->permissionService = $permissionService;
$this->initialState = $initialStateService; $this->initialState = $initialStateService;
$this->l10n = $l10n; $this->configService = $configService;
} }
/** /**
@@ -64,6 +63,7 @@ class PageController extends Controller {
public function index() { public function index() {
$this->initialState->provideInitialState(Application::APP_ID, 'maxUploadSize', (int)\OCP\Util::uploadLimit()); $this->initialState->provideInitialState(Application::APP_ID, 'maxUploadSize', (int)\OCP\Util::uploadLimit());
$this->initialState->provideInitialState(Application::APP_ID, 'canCreate', $this->permissionService->canCreate()); $this->initialState->provideInitialState(Application::APP_ID, 'canCreate', $this->permissionService->canCreate());
$this->initialState->provideInitialState(Application::APP_ID, 'config', $this->configService->getAll());
$response = new TemplateResponse('deck', 'main'); $response = new TemplateResponse('deck', 'main');

211
lib/DAV/Calendar.php Normal file
View File

@@ -0,0 +1,211 @@
<?php
/**
* @copyright 2020, Georg Ehrke <oc.list@georgehrke.com>
*
* @author Georg Ehrke <oc.list@georgehrke.com>
*
* @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\DAV;
use OCA\DAV\CalDAV\Integration\ExternalCalendar;
use OCA\DAV\CalDAV\Plugin;
use OCA\Deck\Db\Acl;
use OCA\Deck\Db\Board;
use Sabre\CalDAV\CalendarQueryValidator;
use Sabre\CalDAV\Xml\Property\SupportedCalendarComponentSet;
use Sabre\DAV\Exception\Forbidden;
use Sabre\DAV\Exception\NotFound;
use Sabre\DAV\PropPatch;
use Sabre\VObject\InvalidDataException;
use Sabre\VObject\Reader;
class Calendar extends ExternalCalendar {
/** @var string */
private $principalUri;
/** @var string[] */
private $children;
/** @var DeckCalendarBackend */
private $backend;
/** @var Board */
private $board;
public function __construct(string $principalUri, string $calendarUri, Board $board, DeckCalendarBackend $backend) {
parent::__construct('deck', $calendarUri);
$this->backend = $backend;
$this->board = $board;
$this->principalUri = $principalUri;
if ($board) {
$this->children = $this->backend->getChildren($board->getId());
} else {
$this->children = [];
}
}
public function getOwner() {
return $this->principalUri;
}
public function getACL() {
$acl = [
[
'privilege' => '{DAV:}read',
'principal' => $this->getOwner(),
'protected' => true,
]
];
if ($this->backend->checkBoardPermission($this->board->getId(), Acl::PERMISSION_MANAGE)) {
$acl[] = [
'privilege' => '{DAV:}write-properties',
'principal' => $this->getOwner(),
'protected' => true,
];
}
return $acl;
}
public function setACL(array $acl) {
throw new Forbidden('Setting ACL is not supported on this node');
}
public function getSupportedPrivilegeSet() {
return null;
}
public function calendarQuery(array $filters) {
$result = [];
$objects = $this->getChildren();
foreach ($objects as $object) {
if ($this->validateFilterForObject($object, $filters)) {
$result[] = $object->getName();
}
}
return $result;
}
protected function validateFilterForObject($object, array $filters) {
$vObject = Reader::read($object->get());
$validator = new CalendarQueryValidator();
$result = $validator->validate($vObject, $filters);
// Destroy circular references so PHP will GC the object.
$vObject->destroy();
return $result;
}
public function createFile($name, $data = null) {
throw new Forbidden('Creating a new entry is not implemented');
}
public function getChild($name) {
if ($this->childExists($name)) {
$card = array_values(array_filter(
$this->children,
function ($card) use (&$name) {
return $card->getCalendarPrefix() . '-' . $card->getId() . '.ics' === $name;
}
));
if (count($card) > 0) {
return new CalendarObject($this, $name, $this->backend, $card[0]);
}
}
throw new NotFound('Node not found');
}
public function getChildren() {
$childNames = array_map(function ($card) {
return $card->getCalendarPrefix() . '-' . $card->getId() . '.ics';
}, $this->children);
$children = [];
foreach ($childNames as $name) {
$children[] = $this->getChild($name);
}
return $children;
}
public function childExists($name) {
return count(array_filter(
$this->children,
function ($card) use (&$name) {
return $card->getCalendarPrefix() . '-' . $card->getId() . '.ics' === $name;
}
)) > 0;
}
public function delete() {
throw new Forbidden('Deleting an entry is not implemented');
}
public function getLastModified() {
return $this->board->getLastModified();
}
public function getGroup() {
return [];
}
public function propPatch(PropPatch $propPatch) {
$properties = [
'{DAV:}displayname',
'{http://apple.com/ns/ical/}calendar-color'
];
$propPatch->handle($properties, function ($properties) {
foreach ($properties as $key => $value) {
switch ($key) {
case '{DAV:}displayname':
if (mb_strpos($value, 'Deck: ') === 0) {
$value = mb_substr($value, strlen('Deck: '));
}
$this->board->setTitle($value);
break;
case '{http://apple.com/ns/ical/}calendar-color':
$color = substr($value, 1, 6);
if (!preg_match('/[a-f0-9]{6}/i', $color)) {
throw new InvalidDataException('No valid color provided');
}
$this->board->setColor($color);
break;
}
}
return $this->backend->updateBoard($this->board);
});
// We can just return here and let oc_properties handle everything
}
/**
* @inheritDoc
*/
public function getProperties($properties) {
return [
'{DAV:}displayname' => 'Deck: ' . ($this->board ? $this->board->getTitle() : 'no board object provided'),
'{http://apple.com/ns/ical/}calendar-color' => '#' . $this->board->getColor(),
'{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO']),
];
}
}

110
lib/DAV/CalendarObject.php Normal file
View File

@@ -0,0 +1,110 @@
<?php
/**
* @copyright 2020, Georg Ehrke <oc.list@georgehrke.com>
*
* @author Georg Ehrke <oc.list@georgehrke.com>
*
* @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\DAV;
use OCA\Deck\Db\Card;
use OCA\Deck\Db\Stack;
use Sabre\CalDAV\ICalendarObject;
use Sabre\DAV\Exception\Forbidden;
use Sabre\DAVACL\IACL;
use Sabre\VObject\Component\VCalendar;
class CalendarObject implements ICalendarObject, IACL {
/** @var Calendar */
private $calendar;
/** @var string */
private $name;
/** @var Card|Stack */
private $sourceItem;
/** @var DeckCalendarBackend */
private $backend;
/** @var VCalendar */
private $calendarObject;
public function __construct(Calendar $calendar, string $name, DeckCalendarBackend $backend, $sourceItem) {
$this->calendar = $calendar;
$this->name = $name;
$this->sourceItem = $sourceItem;
$this->backend = $backend;
$this->calendarObject = $this->sourceItem->getCalendarObject();
}
public function getOwner() {
return null;
}
public function getGroup() {
return null;
}
public function getACL() {
return $this->calendar->getACL();
}
public function setACL(array $acl) {
throw new Forbidden('Setting ACL is not supported on this node');
}
public function getSupportedPrivilegeSet() {
return null;
}
public function put($data) {
throw new Forbidden('This calendar-object is read-only');
}
public function get() {
if ($this->sourceItem) {
return $this->calendarObject->serialize();
}
}
public function getContentType() {
return 'text/calendar; charset=utf-8';
}
public function getETag() {
return '"' . md5($this->sourceItem->getLastModified()) . '"';
}
public function getSize() {
return mb_strlen($this->calendarObject->serialize());
}
public function delete() {
throw new Forbidden('This calendar-object is read-only');
}
public function getName() {
return $this->name;
}
public function setName($name) {
throw new Forbidden('This calendar-object is read-only');
}
public function getLastModified() {
return $this->sourceItem->getLastModified();
}
}

View File

@@ -0,0 +1,84 @@
<?php
/**
* @copyright 2020, Georg Ehrke <oc.list@georgehrke.com>
*
* @author Georg Ehrke <oc.list@georgehrke.com>
*
* @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\DAV;
use OCA\DAV\CalDAV\Integration\ExternalCalendar;
use OCA\DAV\CalDAV\Integration\ICalendarProvider;
use OCA\Deck\Db\Board;
use OCA\Deck\Service\ConfigService;
use Sabre\DAV\Exception\NotFound;
class CalendarPlugin implements ICalendarProvider {
/** @var DeckCalendarBackend */
private $backend;
/** @var bool */
private $calendarIntegrationEnabled;
public function __construct(DeckCalendarBackend $backend, ConfigService $configService) {
$this->backend = $backend;
$this->calendarIntegrationEnabled = $configService->get('calendar');
}
public function getAppId(): string {
return 'deck';
}
public function fetchAllForCalendarHome(string $principalUri): array {
if (!$this->calendarIntegrationEnabled) {
return [];
}
return array_map(function (Board $board) use ($principalUri) {
return new Calendar($principalUri, 'board-' . $board->getId(), $board, $this->backend);
}, $this->backend->getBoards());
}
public function hasCalendarInCalendarHome(string $principalUri, string $calendarUri): bool {
if (!$this->calendarIntegrationEnabled) {
return false;
}
$boards = array_map(static function (Board $board) {
return 'board-' . $board->getId();
}, $this->backend->getBoards());
return in_array($calendarUri, $boards, true);
}
public function getCalendarInCalendarHome(string $principalUri, string $calendarUri): ?ExternalCalendar {
if (!$this->calendarIntegrationEnabled) {
return null;
}
if ($this->hasCalendarInCalendarHome($principalUri, $calendarUri)) {
try {
$board = $this->backend->getBoard((int)str_replace('board-', '', $calendarUri));
return new Calendar($principalUri, $calendarUri, $board, $this->backend);
} catch (NotFound $e) {
// We can just return null if we have no matching board
}
}
return null;
}
}

View File

@@ -0,0 +1,89 @@
<?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\DAV;
use OCA\Deck\Db\Board;
use OCA\Deck\Db\BoardMapper;
use OCA\Deck\Service\BoardService;
use OCA\Deck\Service\CardService;
use OCA\Deck\Service\PermissionService;
use OCA\Deck\Service\StackService;
use Sabre\DAV\Exception\NotFound;
class DeckCalendarBackend {
/** @var BoardService */
private $boardService;
/** @var StackService */
private $stackService;
/** @var CardService */
private $cardService;
/** @var PermissionService */
private $permissionService;
/** @var BoardMapper */
private $boardMapper;
public function __construct(
BoardService $boardService, StackService $stackService, CardService $cardService, PermissionService $permissionService,
BoardMapper $boardMapper
) {
$this->boardService = $boardService;
$this->stackService = $stackService;
$this->cardService = $cardService;
$this->permissionService = $permissionService;
$this->boardMapper = $boardMapper;
}
public function getBoards(): array {
return $this->boardService->findAll();
}
public function getBoard(int $id): Board {
try {
return $this->boardService->find($id);
} catch (\Exception $e) {
throw new NotFound('Board with id ' . $id . ' not found');
}
}
public function checkBoardPermission(int $id, int $permission): bool {
$permissions = $this->permissionService->getPermissions($id);
return isset($permissions[$permission]) ? $permissions[$permission] : false;
}
public function updateBoard(Board $board): bool {
$this->boardMapper->update($board);
return true;
}
public function getChildren(int $id): array {
return array_merge(
$this->cardService->findCalendarEntries($id),
$this->stackService->findCalendarEntries($id)
);
}
}

View File

@@ -24,6 +24,8 @@
namespace OCA\Deck\Db; namespace OCA\Deck\Db;
use DateTime; use DateTime;
use DateTimeZone;
use Sabre\VObject\Component\VCalendar;
class Card extends RelationalEntity { class Card extends RelationalEntity {
protected $title; protected $title;
@@ -117,4 +119,40 @@ class Card extends RelationalEntity {
unset($json['descriptionPrev']); unset($json['descriptionPrev']);
return $json; return $json;
} }
public function getCalendarObject(): VCalendar {
$calendar = new VCalendar();
$event = $calendar->createComponent('VTODO');
$event->UID = 'deck-card-' . $this->getId();
if ($this->getDuedate()) {
$creationDate = new DateTime();
$creationDate->setTimestamp($this->createdAt);
$event->DTSTAMP = $creationDate;
$event->DUE = new DateTime($this->getDuedate(true), new DateTimeZone('UTC'));
}
$event->add('RELATED-TO', 'deck-stack-' . $this->getStackId());
// FIXME: For write support: CANCELLED / IN-PROCESS handling
$event->STATUS = $this->getArchived() ? "COMPLETED" : "NEEDS-ACTION";
if ($this->getArchived()) {
$date = new DateTime();
$date->setTimestamp($this->getLastModified());
$event->COMPLETED = $date;
//$event->add('PERCENT-COMPLETE', 100);
}
if (count($this->getLabels()) > 0) {
$event->CATEGORIES = array_map(function ($label) {
return $label->getTitle();
}, $this->getLabels());
}
$event->SUMMARY = $this->getTitle();
$event->DESCRIPTION = $this->getDescription();
$calendar->add($event);
return $calendar;
}
public function getCalendarPrefix(): string {
return 'card';
}
} }

View File

@@ -23,7 +23,9 @@
namespace OCA\Deck\Db; namespace OCA\Deck\Db;
use Exception;
use OCP\AppFramework\Db\Entity; use OCP\AppFramework\Db\Entity;
use OCP\AppFramework\Db\QBMapper; use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection; use OCP\IDBConnection;
@@ -81,16 +83,20 @@ class CardMapper extends QBMapper implements IPermissionMapper {
// make sure we only reset the notification flag if the duedate changes // make sure we only reset the notification flag if the duedate changes
if (in_array('duedate', $entity->getUpdatedFields(), true)) { if (in_array('duedate', $entity->getUpdatedFields(), true)) {
$existing = $this->find($entity->getId()); try {
if ($existing->getDuedate() !== $entity->getDuedate()) { /** @var Card $existing */
$entity->setNotified(false); $existing = $this->find($entity->getId());
if ($existing && $entity->getDuedate() !== $existing->getDuedate()) {
$entity->setNotified(false);
}
// remove pending notifications
$notification = $this->notificationManager->createNotification();
$notification
->setApp('deck')
->setObject('card', $entity->getId());
$this->notificationManager->markProcessed($notification);
} catch (Exception $e) {
} }
// remove pending notifications
$notification = $this->notificationManager->createNotification();
$notification
->setApp('deck')
->setObject('card', $entity->getId());
$this->notificationManager->markProcessed($notification);
} }
return parent::update($entity); return parent::update($entity);
} }
@@ -102,19 +108,13 @@ class CardMapper extends QBMapper implements IPermissionMapper {
return parent::update($cardUpdate); return parent::update($cardUpdate);
} }
/** public function find($id): Card {
* @param $id
* @return RelationalEntity if not found
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
* @throws \OCP\AppFramework\Db\DoesNotExistException
*/
public function find($id): Entity {
$qb = $this->db->getQueryBuilder(); $qb = $this->db->getQueryBuilder();
$qb->select('*')->from('deck_cards') $qb->select('*')
->from('deck_cards')
->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))) ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)))
->orderBy('order') ->orderBy('order')
->addOrderBy('id'); ->addOrderBy('id');
/** @var Card $card */ /** @var Card $card */
$card = $this->findEntity($qb); $card = $this->findEntity($qb);
$labels = $this->labelMapper->findAssignedLabelsForCard($card->id); $labels = $this->labelMapper->findAssignedLabelsForCard($card->id);
@@ -153,7 +153,6 @@ class CardMapper extends QBMapper implements IPermissionMapper {
->from('deck_cards', 'c') ->from('deck_cards', 'c')
->innerJoin('c', 'deck_stacks', 's', $qb->expr()->eq('s.id', 'c.stack_id')) ->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))); ->andWhere($qb->expr()->in('s.board_id', $qb->createNamedParameter($boardIds, IQueryBuilder::PARAM_INT_ARRAY)));
return $qb; return $qb;
} }
@@ -167,6 +166,19 @@ class CardMapper extends QBMapper implements IPermissionMapper {
return $this->findEntities($qb); return $this->findEntities($qb);
} }
public function findCalendarEntries($boardId, $limit = null, $offset = null) {
$qb = $this->db->getQueryBuilder();
$qb->select('c.*')
->from('deck_cards', 'c')
->join('c', 'deck_stacks', 's', 's.id = c.stack_id')
->where($qb->expr()->eq('s.board_id', $qb->createNamedParameter($boardId)))
->andWhere($qb->expr()->eq('c.deleted_at', $qb->createNamedParameter('0')))
->orderBy('c.duedate')
->setMaxResults($limit)
->setFirstResult($offset);
return $this->findEntities($qb);
}
public function findAllArchived($stackId, $limit = null, $offset = null) { public function findAllArchived($stackId, $limit = null, $offset = null) {
$qb = $this->db->getQueryBuilder(); $qb = $this->db->getQueryBuilder();
$qb->select('*') $qb->select('*')
@@ -278,19 +290,21 @@ class CardMapper extends QBMapper implements IPermissionMapper {
} }
public function assignLabel($card, $label) { public function assignLabel($card, $label) {
$sql = 'INSERT INTO `*PREFIX*deck_assigned_labels` (`label_id`,`card_id`) VALUES (?,?)'; $qb = $this->db->getQueryBuilder();
$stmt = $this->db->prepare($sql); $qb->insert('deck_assigned_labels')
$stmt->bindParam(1, $label, \PDO::PARAM_INT); ->values([
$stmt->bindParam(2, $card, \PDO::PARAM_INT); 'label_id' => $qb->createNamedParameter($label, IQueryBuilder::PARAM_INT),
$stmt->execute(); 'card_id' => $qb->createNamedParameter($card, IQueryBuilder::PARAM_INT),
]);
$qb->execute();
} }
public function removeLabel($card, $label) { public function removeLabel($card, $label) {
$sql = 'DELETE FROM `*PREFIX*deck_assigned_labels` WHERE card_id = ? AND label_id = ?'; $qb = $this->db->getQueryBuilder();
$stmt = $this->db->prepare($sql); $qb->delete('deck_assigned_labels')
$stmt->bindParam(1, $card, \PDO::PARAM_INT); ->where($qb->expr()->eq('card_id', $qb->createNamedParameter($card, IQueryBuilder::PARAM_INT)))
$stmt->bindParam(2, $label, \PDO::PARAM_INT); ->andWhere($qb->expr()->eq('label_id', $qb->createNamedParameter($label, IQueryBuilder::PARAM_INT)));
$stmt->execute(); $qb->execute();
} }
public function isOwner($userId, $cardId) { public function isOwner($userId, $cardId) {

View File

@@ -29,10 +29,11 @@ use OCP\AppFramework\Db\Mapper;
* Class DeckMapper * Class DeckMapper
* *
* @package OCA\Deck\Db * @package OCA\Deck\Db
* @deprecated use QBMapper
* *
* TODO: Move to QBMapper once Nextcloud 14 is a minimum requirement * TODO: Move to QBMapper once Nextcloud 14 is a minimum requirement
*/ */
abstract class DeckMapper extends Mapper { class DeckMapper extends Mapper {
/** /**
* @param $id * @param $id

View File

@@ -23,6 +23,8 @@
namespace OCA\Deck\Db; namespace OCA\Deck\Db;
use Sabre\VObject\Component\VCalendar;
class Stack extends RelationalEntity { class Stack extends RelationalEntity {
protected $title; protected $title;
protected $boardId; protected $boardId;
@@ -50,4 +52,17 @@ class Stack extends RelationalEntity {
} }
return $json; return $json;
} }
public function getCalendarObject(): VCalendar {
$calendar = new VCalendar();
$event = $calendar->createComponent('VTODO');
$event->UID = 'deck-stack-' . $this->getId();
$event->SUMMARY = 'List : ' . $this->getTitle();
$calendar->add($event);
return $calendar;
}
public function getCalendarPrefix(): string {
return 'stack';
}
} }

View File

@@ -130,6 +130,7 @@ class BoardService {
return $this->boardsCache; return $this->boardsCache;
} }
$complete = $this->getUserBoards($since); $complete = $this->getUserBoards($since);
$result = [];
/** @var Board $item */ /** @var Board $item */
foreach ($complete as &$item) { foreach ($complete as &$item) {
$this->boardMapper->mapOwner($item); $this->boardMapper->mapOwner($item);
@@ -152,7 +153,7 @@ class BoardService {
]); ]);
$result[$item->getId()] = $item; $result[$item->getId()] = $item;
} }
$this->boardsCache = $complete; $this->boardsCache = $result;
return array_values($result); return array_values($result);
} }
@@ -189,6 +190,7 @@ class BoardService {
'PERMISSION_SHARE' => $permissions[Acl::PERMISSION_SHARE] ?? false 'PERMISSION_SHARE' => $permissions[Acl::PERMISSION_SHARE] ?? false
]); ]);
$this->enrichWithUsers($board); $this->enrichWithUsers($board);
$this->boardsCache[$board->getId()] = $board;
return $board; return $board;
} }
@@ -348,7 +350,7 @@ class BoardService {
throw new BadRequestException('board id must be a number'); throw new BadRequestException('board id must be a number');
} }
$this->permissionService->checkPermission($this->boardMapper, $id, Acl::PERMISSION_READ); $this->permissionService->checkPermission($this->boardMapper, $id, Acl::PERMISSION_MANAGE);
$board = $this->find($id); $board = $this->find($id);
if ($board->getDeletedAt() > 0) { if ($board->getDeletedAt() > 0) {
throw new BadRequestException('This board has already been deleted'); throw new BadRequestException('This board has already been deleted');
@@ -377,7 +379,7 @@ class BoardService {
throw new BadRequestException('board id must be a number'); throw new BadRequestException('board id must be a number');
} }
$this->permissionService->checkPermission($this->boardMapper, $id, Acl::PERMISSION_READ); $this->permissionService->checkPermission($this->boardMapper, $id, Acl::PERMISSION_MANAGE);
$board = $this->find($id); $board = $this->find($id);
$board->setDeletedAt(0); $board->setDeletedAt(0);
$board = $this->boardMapper->update($board); $board = $this->boardMapper->update($board);
@@ -404,7 +406,7 @@ class BoardService {
throw new BadRequestException('id must be a number'); throw new BadRequestException('id must be a number');
} }
$this->permissionService->checkPermission($this->boardMapper, $id, Acl::PERMISSION_READ); $this->permissionService->checkPermission($this->boardMapper, $id, Acl::PERMISSION_MANAGE);
$board = $this->find($id); $board = $this->find($id);
$delete = $this->boardMapper->delete($board); $delete = $this->boardMapper->delete($board);

View File

@@ -144,6 +144,15 @@ class CardService {
return $card; return $card;
} }
public function findCalendarEntries($boardId) {
$this->permissionService->checkPermission($this->boardMapper, $boardId, Acl::PERMISSION_READ);
$cards = $this->cardMapper->findCalendarEntries($boardId);
foreach ($cards as $card) {
$this->enrich($card);
}
return $cards;
}
/** /**
* @param $title * @param $title
* @param $stackId * @param $stackId

View File

@@ -0,0 +1,129 @@
<?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\Service;
use OCA\Deck\AppInfo\Application;
use OCA\Deck\NoPermissionException;
use OCP\IConfig;
use OCP\IGroup;
use OCP\IGroupManager;
class ConfigService {
private $config;
private $userId;
private $groupManager;
public function __construct(
IConfig $config,
IGroupManager $groupManager,
$userId
) {
$this->userId = $userId;
$this->groupManager = $groupManager;
$this->config = $config;
}
public function getAll(): array {
$data = [
'calendar' => $this->get('calendar')
];
if ($this->groupManager->isAdmin($this->userId)) {
$data = [
'groupLimit' => $this->get('groupLimit'),
];
}
return $data;
}
public function get($key) {
$result = null;
switch ($key) {
case 'groupLimit':
if (!$this->groupManager->isAdmin($this->userId)) {
throw new NoPermissionException('You must be admin to get the group limit');
}
$result = $this->getGroupLimit();
break;
case 'calendar':
$result = (bool)$this->config->getUserValue($this->userId, Application::APP_ID, 'calendar', true);
break;
}
return $result;
}
public function set($key, $value) {
$result = null;
switch ($key) {
case 'groupLimit':
if (!$this->groupManager->isAdmin($this->userId)) {
throw new NoPermissionException('You must be admin to set the group limit');
}
$result = $this->setGroupLimit($value);
break;
case 'calendar':
$this->config->setUserValue($this->userId, Application::APP_ID, 'calendar', (int)$value);
$result = $value;
break;
}
return $result;
}
private function setGroupLimit($value) {
$groups = [];
foreach ($value as $group) {
$groups[] = $group['id'];
}
$data = implode(',', $groups);
$this->config->setAppValue(Application::APP_ID, 'groupLimit', $data);
return $groups;
}
private function getGroupLimitList() {
$value = $this->config->getAppValue(Application::APP_ID, 'groupLimit', '');
$groups = explode(',', $value);
if ($value === '') {
return [];
}
return $groups;
}
private function getGroupLimit() {
$groups = $this->getGroupLimitList();
$groups = array_map(function ($groupId) {
/** @var IGroup $groups */
$group = $this->groupManager->get($groupId);
if ($group === null) {
return null;
}
return [
'id' => $group->getGID(),
'displayname' => $group->getDisplayName(),
];
}, $groups);
return array_filter($groups);
}
}

View File

@@ -146,6 +146,11 @@ class StackService {
return $stacks; return $stacks;
} }
public function findCalendarEntries($boardId) {
$this->permissionService->checkPermission(null, $boardId, Acl::PERMISSION_READ);
return $this->stackMapper->findAll($boardId);
}
public function fetchDeleted($boardId) { public function fetchDeleted($boardId) {
$this->permissionService->checkPermission($this->boardMapper, $boardId, Acl::PERMISSION_READ); $this->permissionService->checkPermission($this->boardMapper, $boardId, Acl::PERMISSION_READ);
$stacks = $this->stackMapper->findDeleted($boardId); $stacks = $this->stackMapper->findDeleted($boardId);

View File

@@ -52,14 +52,28 @@
<template #footer> <template #footer>
<AppNavigationSettings> <AppNavigationSettings>
<div> <div>
<input id="toggle-modal" <div>
v-model="cardDetailsInModal" <input id="toggle-modal"
type="checkbox" v-model="cardDetailsInModal"
class="checkbox"> type="checkbox"
<label for="toggle-modal"> class="checkbox">
{{ t('deck', 'Use modal card view') }} <label for="toggle-modal">
</label> {{ t('deck', 'Use modal card view') }}
<Multiselect v-model="groupLimit" </label>
</div>
<div>
<input id="toggle-calendar"
v-model="configCalendar"
type="checkbox"
class="checkbox">
<label for="toggle-calendar">
{{ t('deck', 'Show boards in calendar/tasks') }}
</label>
</div>
<Multiselect v-if="isAdmin"
v-model="groupLimit"
:class="{'icon-loading-small': groupLimitDisabled}" :class="{'icon-loading-small': groupLimitDisabled}"
open-direction="bottom" open-direction="bottom"
:options="groups" :options="groups"
@@ -69,7 +83,9 @@
label="displayname" label="displayname"
track-by="id" track-by="id"
@input="updateConfig" /> @input="updateConfig" />
<p>{{ t('deck', 'Limiting Deck will block users not part of those groups from creating their own boards. Users will still be able to work on boards that have been shared with them.') }}</p> <p v-if="isAdmin">
{{ t('deck', 'Limiting Deck will block users not part of those groups from creating their own boards. Users will still be able to work on boards that have been shared with them.') }}
</p>
</div> </div>
</AppNavigationSettings> </AppNavigationSettings>
</template> </template>
@@ -84,7 +100,8 @@ import { AppNavigation as AppNavigationVue, AppNavigationItem, AppNavigationSett
import AppNavigationAddBoard from './AppNavigationAddBoard' import AppNavigationAddBoard from './AppNavigationAddBoard'
import AppNavigationBoardCategory from './AppNavigationBoardCategory' import AppNavigationBoardCategory from './AppNavigationBoardCategory'
import { loadState } from '@nextcloud/initial-state' import { loadState } from '@nextcloud/initial-state'
import { generateUrl, generateOcsUrl } from '@nextcloud/router' import { generateOcsUrl } from '@nextcloud/router'
import { getCurrentUser } from '@nextcloud/auth'
const canCreateState = loadState('deck', 'canCreate') const canCreateState = loadState('deck', 'canCreate')
@@ -123,8 +140,7 @@ export default {
'sharedBoards', 'sharedBoards',
]), ]),
isAdmin() { isAdmin() {
// eslint-disable-next-line return !!getCurrentUser()?.isAdmin
return OC.isUserAdmin()
}, },
cardDetailsInModal: { cardDetailsInModal: {
get() { get() {
@@ -134,15 +150,19 @@ export default {
this.$store.dispatch('setCardDetailsInModal', newValue) this.$store.dispatch('setCardDetailsInModal', newValue)
}, },
}, },
configCalendar: {
get() {
return this.$store.getters.config('calendar')
},
set(newValue) {
this.$store.dispatch('setConfig', { calendar: newValue })
},
},
}, },
beforeMount() { beforeMount() {
if (this.isAdmin) { if (this.isAdmin) {
axios.get(generateUrl('apps/deck/config')).then((response) => { this.groupLimit = this.$store.getters.config('groupLimit')
this.groupLimit = response.data.groupLimit this.groupLimitDisabled = false
this.groupLimitDisabled = false
}, (error) => {
console.error('Error while loading groupLimit', error.response)
})
axios.get(generateOcsUrl('cloud', 2) + 'groups').then((response) => { axios.get(generateOcsUrl('cloud', 2) + 'groups').then((response) => {
this.groups = response.data.ocs.data.groups.reduce((obj, item) => { this.groups = response.data.ocs.data.groups.reduce((obj, item) => {
obj.push({ obj.push({
@@ -157,15 +177,9 @@ export default {
} }
}, },
methods: { methods: {
updateConfig() { async updateConfig() {
this.groupLimitDisabled = true await this.$store.dispatch('setConfig', { groupLimit: this.groupLimit })
axios.post(generateUrl('apps/deck/config/groupLimit'), { this.groupLimitDisabled = false
value: this.groupLimit,
}).then(() => {
this.groupLimitDisabled = false
}, (error) => {
console.error('Error while saving groupLimit', error.response)
})
}, },
}, },
} }

View File

@@ -22,6 +22,7 @@
import 'url-search-params-polyfill' import 'url-search-params-polyfill'
import { loadState } from '@nextcloud/initial-state'
import Vue from 'vue' import Vue from 'vue'
import Vuex from 'vuex' import Vuex from 'vuex'
import axios from '@nextcloud/axios' import axios from '@nextcloud/axios'
@@ -56,6 +57,7 @@ export default new Vuex.Store({
}, },
strict: debug, strict: debug,
state: { state: {
config: loadState('deck', 'config', {}),
showArchived: false, showArchived: false,
navShown: true, navShown: true,
compactMode: localStorage.getItem('deck.compactMode') === 'true', compactMode: localStorage.getItem('deck.compactMode') === 'true',
@@ -73,6 +75,9 @@ export default new Vuex.Store({
filter: { tags: [], users: [], due: '' }, filter: { tags: [], users: [], due: '' },
}, },
getters: { getters: {
config: state => (key) => {
return state.config[key]
},
cardDetailsInModal: state => { cardDetailsInModal: state => {
return state.cardDetailsInModal return state.cardDetailsInModal
}, },
@@ -133,6 +138,9 @@ export default new Vuex.Store({
}, },
}, },
mutations: { mutations: {
SET_CONFIG(state, { key, value }) {
Vue.set(state.config, key, value)
},
setSearchQuery(state, searchQuery) { setSearchQuery(state, searchQuery) {
state.searchQuery = searchQuery state.searchQuery = searchQuery
}, },
@@ -287,6 +295,19 @@ export default new Vuex.Store({
}, },
actions: { actions: {
async setConfig({ commit }, config) {
for (const key in config) {
try {
await axios.post(generateOcsUrl(`apps/deck/api/v1.0/config`) + key, {
value: config[key],
})
commit('SET_CONFIG', { key, value: config[key] })
} catch (e) {
console.error(`Error while saving ${key}`, e.response)
throw e
}
}
},
setFilter({ commit }, filter) { setFilter({ commit }, filter) {
commit('SET_FILTER', filter) commit('SET_FILTER', filter)
}, },

View File

@@ -24,40 +24,33 @@
namespace OCA\Deck\Controller; namespace OCA\Deck\Controller;
use OCA\Deck\Service\ConfigService;
use OCA\Deck\Service\PermissionService; use OCA\Deck\Service\PermissionService;
use OCP\IInitialStateService; use OCP\IInitialStateService;
use OCP\IL10N; use OCP\IL10N;
use OCP\IRequest; use OCP\IRequest;
use OCA\Deck\Db\Board;
use OCP\IConfig;
class PageControllerTest extends \Test\TestCase { class PageControllerTest extends \Test\TestCase {
private $controller; private $controller;
private $request; private $request;
private $l10n; private $l10n;
private $userId = 'john';
private $permissionService; private $permissionService;
private $initialState; private $initialState;
private $config; private $configService;
public function setUp(): void { public function setUp(): void {
$this->l10n = $this->createMock(IL10N::class); $this->l10n = $this->createMock(IL10N::class);
$this->request = $this->createMock(IRequest::class); $this->request = $this->createMock(IRequest::class);
$this->permissionService = $this->createMock(PermissionService::class); $this->permissionService = $this->createMock(PermissionService::class);
$this->config = $this->createMock(IConfig::class); $this->configService = $this->createMock(ConfigService::class);
$this->initialState = $this->createMock(IInitialStateService::class); $this->initialState = $this->createMock(IInitialStateService::class);
$this->controller = new PageController( $this->controller = new PageController(
'deck', $this->request, $this->permissionService, $this->initialState, $this->l10n, $this->userId 'deck', $this->request, $this->permissionService, $this->initialState, $this->configService
); );
} }
public function testIndex() { public function testIndex() {
$board = new Board();
$board->setTitle('Personal');
$board->setOwner($this->userId);
$board->setColor('317CCC');
$this->permissionService->expects($this->any()) $this->permissionService->expects($this->any())
->method('canCreate') ->method('canCreate')
->willReturn(true); ->willReturn(true);