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

View File

@@ -26,9 +26,6 @@ return [
'routes' => [
['name' => 'page#index', 'url' => '/', 'verb' => 'GET'],
['name' => 'Config#get', 'url' => '/config', 'verb' => 'GET'],
['name' => 'Config#setValue', 'url' => '/config/{key}', 'verb' => 'POST'],
// boards
['name' => 'board#index', 'url' => '/boards', 'verb' => 'GET'],
['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#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' => '.+']],
],
'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#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#delete', 'url' => '/api/v1.0/cards/{cardId}/comments/{commentId}', 'verb' => 'DELETE'],
// dashboard
['name' => 'overview_api#upcomingCards', 'url' => '/api/v1.0/overview/upcoming', 'verb' => 'GET'],
]
];

View File

@@ -23,90 +23,42 @@
namespace OCA\Deck\Controller;
use OCA\Deck\Service\ConfigService;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\NotFoundResponse;
use OCP\IConfig;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\AppFramework\OCSController;
use OCP\IRequest;
use OCP\AppFramework\Controller;
class ConfigController extends Controller {
private $config;
private $userId;
private $groupManager;
class ConfigController extends OCSController {
private $configService;
public function __construct(
$AppName,
IRequest $request,
IConfig $config,
IGroupManager $groupManager,
$userId
ConfigService $configService
) {
parent::__construct($AppName, $request);
$this->userId = $userId;
$this->groupManager = $groupManager;
$this->config = $config;
$this->configService = $configService;
}
/**
* @NoCSRFRequired
* @NoAdminRequired
*/
public function get() {
$data = [
'groupLimit' => $this->getGroupLimit(),
];
return new DataResponse($data);
public function get(): DataResponse {
return new DataResponse($this->configService->getAll());
}
/**
* @NoCSRFRequired
* @NoAdminRequired
*/
public function setValue($key, $value) {
switch ($key) {
case 'groupLimit':
$result = $this->setGroupLimit($value);
break;
}
public function setValue(string $key, $value) {
$result = $this->configService->set($key, $value);
if ($result === null) {
return new NotFoundResponse();
}
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;
use OCA\Deck\AppInfo\Application;
use OCA\Deck\Service\ConfigService;
use OCA\Deck\Service\PermissionService;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\IInitialStateService;
use OCP\IRequest;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Controller;
use OCP\IL10N;
class PageController extends Controller {
private $permissionService;
private $userId;
private $l10n;
private $initialState;
private $configService;
public function __construct(
$AppName,
IRequest $request,
PermissionService $permissionService,
IInitialStateService $initialStateService,
IL10N $l10n,
$userId
ConfigService $configService
) {
parent::__construct($AppName, $request);
$this->userId = $userId;
$this->permissionService = $permissionService;
$this->initialState = $initialStateService;
$this->l10n = $l10n;
$this->configService = $configService;
}
/**
@@ -64,6 +63,7 @@ class PageController extends Controller {
public function index() {
$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, 'config', $this->configService->getAll());
$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;
use DateTime;
use DateTimeZone;
use Sabre\VObject\Component\VCalendar;
class Card extends RelationalEntity {
protected $title;
@@ -117,4 +119,40 @@ class Card extends RelationalEntity {
unset($json['descriptionPrev']);
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;
use Exception;
use OCP\AppFramework\Db\Entity;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
@@ -81,8 +83,10 @@ class CardMapper extends QBMapper implements IPermissionMapper {
// make sure we only reset the notification flag if the duedate changes
if (in_array('duedate', $entity->getUpdatedFields(), true)) {
try {
/** @var Card $existing */
$existing = $this->find($entity->getId());
if ($existing->getDuedate() !== $entity->getDuedate()) {
if ($existing && $entity->getDuedate() !== $existing->getDuedate()) {
$entity->setNotified(false);
}
// remove pending notifications
@@ -91,6 +95,8 @@ class CardMapper extends QBMapper implements IPermissionMapper {
->setApp('deck')
->setObject('card', $entity->getId());
$this->notificationManager->markProcessed($notification);
} catch (Exception $e) {
}
}
return parent::update($entity);
}
@@ -102,19 +108,13 @@ class CardMapper extends QBMapper implements IPermissionMapper {
return parent::update($cardUpdate);
}
/**
* @param $id
* @return RelationalEntity if not found
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
* @throws \OCP\AppFramework\Db\DoesNotExistException
*/
public function find($id): Entity {
public function find($id): Card {
$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)))
->orderBy('order')
->addOrderBy('id');
/** @var Card $card */
$card = $this->findEntity($qb);
$labels = $this->labelMapper->findAssignedLabelsForCard($card->id);
@@ -153,7 +153,6 @@ class CardMapper extends QBMapper implements IPermissionMapper {
->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)));
return $qb;
}
@@ -167,6 +166,19 @@ class CardMapper extends QBMapper implements IPermissionMapper {
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) {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
@@ -278,19 +290,21 @@ class CardMapper extends QBMapper implements IPermissionMapper {
}
public function assignLabel($card, $label) {
$sql = 'INSERT INTO `*PREFIX*deck_assigned_labels` (`label_id`,`card_id`) VALUES (?,?)';
$stmt = $this->db->prepare($sql);
$stmt->bindParam(1, $label, \PDO::PARAM_INT);
$stmt->bindParam(2, $card, \PDO::PARAM_INT);
$stmt->execute();
$qb = $this->db->getQueryBuilder();
$qb->insert('deck_assigned_labels')
->values([
'label_id' => $qb->createNamedParameter($label, IQueryBuilder::PARAM_INT),
'card_id' => $qb->createNamedParameter($card, IQueryBuilder::PARAM_INT),
]);
$qb->execute();
}
public function removeLabel($card, $label) {
$sql = 'DELETE FROM `*PREFIX*deck_assigned_labels` WHERE card_id = ? AND label_id = ?';
$stmt = $this->db->prepare($sql);
$stmt->bindParam(1, $card, \PDO::PARAM_INT);
$stmt->bindParam(2, $label, \PDO::PARAM_INT);
$stmt->execute();
$qb = $this->db->getQueryBuilder();
$qb->delete('deck_assigned_labels')
->where($qb->expr()->eq('card_id', $qb->createNamedParameter($card, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('label_id', $qb->createNamedParameter($label, IQueryBuilder::PARAM_INT)));
$qb->execute();
}
public function isOwner($userId, $cardId) {

View File

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

View File

@@ -23,6 +23,8 @@
namespace OCA\Deck\Db;
use Sabre\VObject\Component\VCalendar;
class Stack extends RelationalEntity {
protected $title;
protected $boardId;
@@ -50,4 +52,17 @@ class Stack extends RelationalEntity {
}
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;
}
$complete = $this->getUserBoards($since);
$result = [];
/** @var Board $item */
foreach ($complete as &$item) {
$this->boardMapper->mapOwner($item);
@@ -152,7 +153,7 @@ class BoardService {
]);
$result[$item->getId()] = $item;
}
$this->boardsCache = $complete;
$this->boardsCache = $result;
return array_values($result);
}
@@ -189,6 +190,7 @@ class BoardService {
'PERMISSION_SHARE' => $permissions[Acl::PERMISSION_SHARE] ?? false
]);
$this->enrichWithUsers($board);
$this->boardsCache[$board->getId()] = $board;
return $board;
}
@@ -348,7 +350,7 @@ class BoardService {
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);
if ($board->getDeletedAt() > 0) {
throw new BadRequestException('This board has already been deleted');
@@ -377,7 +379,7 @@ class BoardService {
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->setDeletedAt(0);
$board = $this->boardMapper->update($board);
@@ -404,7 +406,7 @@ class BoardService {
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);
$delete = $this->boardMapper->delete($board);

View File

@@ -144,6 +144,15 @@ class CardService {
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 $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;
}
public function findCalendarEntries($boardId) {
$this->permissionService->checkPermission(null, $boardId, Acl::PERMISSION_READ);
return $this->stackMapper->findAll($boardId);
}
public function fetchDeleted($boardId) {
$this->permissionService->checkPermission($this->boardMapper, $boardId, Acl::PERMISSION_READ);
$stacks = $this->stackMapper->findDeleted($boardId);

View File

@@ -51,6 +51,7 @@
</template>
<template #footer>
<AppNavigationSettings>
<div>
<div>
<input id="toggle-modal"
v-model="cardDetailsInModal"
@@ -59,7 +60,20 @@
<label for="toggle-modal">
{{ t('deck', 'Use modal card view') }}
</label>
<Multiselect v-model="groupLimit"
</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}"
open-direction="bottom"
:options="groups"
@@ -69,7 +83,9 @@
label="displayname"
track-by="id"
@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>
</AppNavigationSettings>
</template>
@@ -84,7 +100,8 @@ import { AppNavigation as AppNavigationVue, AppNavigationItem, AppNavigationSett
import AppNavigationAddBoard from './AppNavigationAddBoard'
import AppNavigationBoardCategory from './AppNavigationBoardCategory'
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')
@@ -123,8 +140,7 @@ export default {
'sharedBoards',
]),
isAdmin() {
// eslint-disable-next-line
return OC.isUserAdmin()
return !!getCurrentUser()?.isAdmin
},
cardDetailsInModal: {
get() {
@@ -134,15 +150,19 @@ export default {
this.$store.dispatch('setCardDetailsInModal', newValue)
},
},
configCalendar: {
get() {
return this.$store.getters.config('calendar')
},
set(newValue) {
this.$store.dispatch('setConfig', { calendar: newValue })
},
},
},
beforeMount() {
if (this.isAdmin) {
axios.get(generateUrl('apps/deck/config')).then((response) => {
this.groupLimit = response.data.groupLimit
this.groupLimit = this.$store.getters.config('groupLimit')
this.groupLimitDisabled = false
}, (error) => {
console.error('Error while loading groupLimit', error.response)
})
axios.get(generateOcsUrl('cloud', 2) + 'groups').then((response) => {
this.groups = response.data.ocs.data.groups.reduce((obj, item) => {
obj.push({
@@ -157,15 +177,9 @@ export default {
}
},
methods: {
updateConfig() {
this.groupLimitDisabled = true
axios.post(generateUrl('apps/deck/config/groupLimit'), {
value: this.groupLimit,
}).then(() => {
async updateConfig() {
await this.$store.dispatch('setConfig', { groupLimit: this.groupLimit })
this.groupLimitDisabled = false
}, (error) => {
console.error('Error while saving groupLimit', error.response)
})
},
},
}

View File

@@ -22,6 +22,7 @@
import 'url-search-params-polyfill'
import { loadState } from '@nextcloud/initial-state'
import Vue from 'vue'
import Vuex from 'vuex'
import axios from '@nextcloud/axios'
@@ -56,6 +57,7 @@ export default new Vuex.Store({
},
strict: debug,
state: {
config: loadState('deck', 'config', {}),
showArchived: false,
navShown: true,
compactMode: localStorage.getItem('deck.compactMode') === 'true',
@@ -73,6 +75,9 @@ export default new Vuex.Store({
filter: { tags: [], users: [], due: '' },
},
getters: {
config: state => (key) => {
return state.config[key]
},
cardDetailsInModal: state => {
return state.cardDetailsInModal
},
@@ -133,6 +138,9 @@ export default new Vuex.Store({
},
},
mutations: {
SET_CONFIG(state, { key, value }) {
Vue.set(state.config, key, value)
},
setSearchQuery(state, searchQuery) {
state.searchQuery = searchQuery
},
@@ -287,6 +295,19 @@ export default new Vuex.Store({
},
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) {
commit('SET_FILTER', filter)
},

View File

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