Some more restructuring

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl
2020-08-20 21:37:48 +02:00
parent f4201392f6
commit 42640e2428
6 changed files with 192 additions and 217 deletions

View File

@@ -24,72 +24,44 @@ namespace OCA\Deck\DAV;
use OCA\DAV\CalDAV\Integration\ExternalCalendar; use OCA\DAV\CalDAV\Integration\ExternalCalendar;
use OCA\DAV\CalDAV\Plugin; use OCA\DAV\CalDAV\Plugin;
use OCA\DAV\DAV\Sharing\IShareable; use OCA\Deck\Db\Acl;
use OCA\Deck\Db\Board; use OCA\Deck\Db\Board;
use OCA\Deck\Db\Card;
use OCA\Deck\Db\Stack;
use OCA\Deck\Service\CardService;
use OCA\Deck\Service\StackService;
use Sabre\CalDAV\Xml\Property\SupportedCalendarComponentSet; use Sabre\CalDAV\Xml\Property\SupportedCalendarComponentSet;
use Sabre\DAV\Exception\Forbidden;
use Sabre\DAV\PropPatch; use Sabre\DAV\PropPatch;
class Calendar extends ExternalCalendar implements IShareable { class Calendar extends ExternalCalendar {
/** @var string */ /** @var string */
private $principalUri; private $principalUri;
/** @var string */
private $calendarUri;
/** @var string[] */ /** @var string[] */
private $children; private $children;
/** /** @var DeckCalendarBackend */
* @var \stdClass private $backend;
*/ /** @var Board */
private $cardService; private $board;
/** public function __construct(string $principalUri, string $calendarUri, Board $board, DeckCalendarBackend $backend) {
* Calendar constructor.
*
* @param string $principalUri
* @param string $calendarUri
*/
public function __construct(string $principalUri, string $calendarUri, Board $board = null) {
parent::__construct('deck', $calendarUri); parent::__construct('deck', $calendarUri);
$this->backend = $backend;
$this->board = $board; $this->board = $board;
$this->principalUri = $principalUri; $this->principalUri = $principalUri;
$this->calendarUri = $calendarUri;
if ($board) { if ($board) {
/** @var CardService $cardService */ $this->children = $this->backend->getChildren($board->getId());
$cardService = \OC::$server->query(CardService::class);
/** @var StackService $stackService */
$stackService = \OC::$server->query(StackService::class);
$this->children = array_merge(
$cardService->findCalendarEntries($board->getId()),
$stackService->findCalendarEntries($board->getId())
);
} else { } else {
$this->children = []; $this->children = [];
} }
} }
public function getOwner() {
/**
* @inheritDoc
*/
function getOwner() {
return $this->principalUri; return $this->principalUri;
} }
/** public function getACL() {
* @inheritDoc $acl = [
*/
function getACL() {
return [
[ [
'privilege' => '{DAV:}read', 'privilege' => '{DAV:}read',
'principal' => $this->getOwner(), 'principal' => $this->getOwner(),
@@ -106,43 +78,41 @@ class Calendar extends ExternalCalendar implements IShareable {
'protected' => true, 'protected' => true,
], ],
]; ];
if ($this->backend->checkBoardPermission($this->board->getId(), Acl::PERMISSION_EDIT)) {
$acl[] = [
'privilege' => '{DAV:}write',
'principal' => $this->getOwner(),
'protected' => true,
];
$acl[] = [
'privilege' => '{DAV:}write-properties',
'principal' => $this->getOwner(),
'protected' => true,
];
}
return $acl;
} }
/** public function setACL(array $acl) {
* @inheritDoc throw new Forbidden('Setting ACL is not supported on this node');
*/
function setACL(array $acl) {
throw new \Sabre\DAV\Exception\Forbidden('Setting ACL is not supported on this node');
} }
/** public function getSupportedPrivilegeSet() {
* @inheritDoc
*/
function getSupportedPrivilegeSet() {
return null; return null;
} }
/** public function calendarQuery(array $filters) {
* @inheritDoc // FIXME: In a real implementation this should actually filter
*/
function calendarQuery(array $filters) {
// In a real implementation this should actually filter
return array_map(function ($card) { return array_map(function ($card) {
return $card->getCalendarPrefix() . '-' . $card->getId() . '.ics'; return $card->getCalendarPrefix() . '-' . $card->getId() . '.ics';
}, $this->children); }, $this->children);
} }
/** public function createFile($name, $data = null) {
* @inheritDoc throw new \Sabre\DAV\Exception\Forbidden('Creating a new entry is not implemented');
*/
function createFile($name, $data = null) {
return null;
} }
/** public function getChild($name) {
* @inheritDoc
*/
function getChild($name) {
if ($this->childExists($name)) { if ($this->childExists($name)) {
$card = array_values(array_filter( $card = array_values(array_filter(
$this->children, $this->children,
@@ -151,15 +121,12 @@ class Calendar extends ExternalCalendar implements IShareable {
} }
)); ));
if (count($card) > 0) { if (count($card) > 0) {
return new CalendarObject($this, $name, $card[0]); return new CalendarObject($this, $name, $card[0], $this->backend);
} }
} }
} }
/** public function getChildren() {
* @inheritDoc
*/
function getChildren() {
$childNames = array_map(function ($card) { $childNames = array_map(function ($card) {
return $card->getCalendarPrefix() . '-' . $card->getId() . '.ics'; return $card->getCalendarPrefix() . '-' . $card->getId() . '.ics';
}, $this->children); }, $this->children);
@@ -173,10 +140,7 @@ class Calendar extends ExternalCalendar implements IShareable {
return $children; return $children;
} }
/** public function childExists($name) {
* @inheritDoc
*/
function childExists($name) {
return count(array_filter( return count(array_filter(
$this->children, $this->children,
function ($card) use (&$name) { function ($card) use (&$name) {
@@ -185,64 +149,51 @@ class Calendar extends ExternalCalendar implements IShareable {
)) > 0; )) > 0;
} }
/**
* @inheritDoc public function delete() {
*/
function delete() {
return null; return null;
} }
/** public function getLastModified() {
* @inheritDoc
*/
function getLastModified() {
return $this->board->getLastModified(); return $this->board->getLastModified();
} }
/** public function getGroup() {
* @inheritDoc
*/
function getGroup() {
return []; return [];
} }
/** public function propPatch(PropPatch $propPatch) {
* @inheritDoc $properties = [
*/ '{DAV:}displayname',
function propPatch(PropPatch $propPatch) { '{http://apple.com/ns/ical/}calendar-color'
];
$propPatch->handle($properties, function ($properties) {
foreach ($properties as $key => $value) {
switch ($key) {
case '{DAV:}displayname':
if (mb_substr($value, 0, strlen('Deck: '))) {
$value = mb_substr($value, strlen('Deck: '));
}
$this->board->setTitle($value);
break;
case '{http://apple.com/ns/ical/}calendar-color':
$this->board->setColor(substr($value, 1));
break;
}
}
return $this->backend->updateBoard($this->board);
});
// We can just return here and let oc_properties handle everything // We can just return here and let oc_properties handle everything
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */
function getProperties($properties) { public function getProperties($properties) {
// A backend should provide at least minimum properties
return [ return [
'{DAV:}displayname' => 'Deck: ' . ($this->board ? $this->board->getTitle() : 'no board object provided'), '{DAV:}displayname' => 'Deck: ' . ($this->board ? $this->board->getTitle() : 'no board object provided'),
'{http://apple.com/ns/ical/}calendar-color' => '#' . $this->board->getColor(), '{http://apple.com/ns/ical/}calendar-color' => '#' . $this->board->getColor(),
'{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO']),
]; ];
} }
/**
* @inheritDoc
*/
function updateShares(array $add, array $remove) {
// TODO: Implement updateShares() method.
}
/**
* @inheritDoc
*/
function getShares() {
return [];
}
/**
* @inheritDoc
*/
public function getResourceId() {
// TODO: Implement getResourceId() method.
}
} }

View File

@@ -23,130 +23,88 @@
namespace OCA\Deck\DAV; namespace OCA\Deck\DAV;
use OCA\Deck\Db\Card; use OCA\Deck\Db\Card;
use OCA\Deck\Service\CardService; use OCA\Deck\Db\Stack;
use Sabre\CalDAV\ICalendarObject;
use Sabre\DAV\Exception\Forbidden;
use Sabre\DAVACL\IACL;
use Sabre\VObject\Component\VCalendar; use Sabre\VObject\Component\VCalendar;
class CalendarObject implements \Sabre\CalDAV\ICalendarObject, \Sabre\DAVACL\IACL { class CalendarObject implements ICalendarObject, IACL {
/** @var Calendar */ /** @var Calendar */
private $calendar; private $calendar;
/** @var string */ /** @var string */
private $name; private $name;
/** /** @var Card|Stack */
* @var Card
*/
private $sourceItem; private $sourceItem;
/** @var DeckCalendarBackend */
private $backend;
/** @var VCalendar */
private $calendarObject;
/** public function __construct(Calendar $calendar, string $name, $sourceItem = null, DeckCalendarBackend $backend) {
* CalendarObject constructor.
*
* @param Calendar $calendar
* @param string $name
*/
public function __construct(Calendar $calendar, string $name, $sourceItem = null) {
$this->calendar = $calendar; $this->calendar = $calendar;
$this->name = $name; $this->name = $name;
$this->sourceItem = $sourceItem; $this->sourceItem = $sourceItem;
$this->backend = $backend;
$this->calendarObject = $this->sourceItem->getCalendarObject();
} }
/** public function getOwner() {
* @inheritDoc
*/
function getOwner() {
return null; return null;
} }
/** public function getGroup() {
* @inheritDoc
*/
function getGroup() {
return null; return null;
} }
/** public function getACL() {
* @inheritDoc
*/
function getACL() {
return $this->calendar->getACL(); return $this->calendar->getACL();
} }
/** public function setACL(array $acl) {
* @inheritDoc throw new Forbidden('Setting ACL is not supported on this node');
*/
function setACL(array $acl) {
throw new \Sabre\DAV\Exception\Forbidden('Setting ACL is not supported on this node');
} }
/** public function getSupportedPrivilegeSet() {
* @inheritDoc
*/
function getSupportedPrivilegeSet() {
return null; return null;
} }
/** public function put($data) {
* @inheritDoc throw new Forbidden('This calendar-object is read-only');
*/
function put($data) {
throw new \Sabre\DAV\Exception\Forbidden('This calendar-object is read-only');
} }
/** public function get() {
* @inheritDoc
*/
function get() {
if ($this->sourceItem) { if ($this->sourceItem) {
return $this->sourceItem->getCalendarObject()->serialize(); return $this->calendarObject->serialize();
} }
} }
/** public function getContentType() {
* @inheritDoc
*/
function getContentType() {
return 'text/calendar; charset=utf-8'; return 'text/calendar; charset=utf-8';
} }
/** public function getETag() {
* @inheritDoc return '"' . md5($this->sourceItem->getLastModified()) . '"';
*/
function getETag() {
return '"' . md5($this->get()) . '"';
} }
/** public function getSize() {
* @inheritDoc return mb_strlen($this->calendarObject->serialize());
*/
function getSize() {
return strlen($this->get());
} }
/** public function delete() {
* @inheritDoc throw new Forbidden('This calendar-object is read-only');
*/
function delete() {
throw new \Sabre\DAV\Exception\Forbidden('This calendar-object is read-only');
} }
/** public function getName() {
* @inheritDoc
*/
function getName() {
return $this->name; return $this->name;
} }
/** public function setName($name) {
* @inheritDoc throw new Forbidden('This calendar-object is read-only');
*/
function setName($name) {
throw new \Sabre\DAV\Exception\Forbidden('This calendar-object is read-only');
} }
/** public function getLastModified() {
* @inheritDoc
*/
function getLastModified() {
return $this->sourceItem->getLastModified(); return $this->sourceItem->getLastModified();
} }
} }

View File

@@ -26,53 +26,37 @@ namespace OCA\Deck\DAV;
use OCA\DAV\CalDAV\Integration\ExternalCalendar; use OCA\DAV\CalDAV\Integration\ExternalCalendar;
use OCA\DAV\CalDAV\Integration\ICalendarProvider; use OCA\DAV\CalDAV\Integration\ICalendarProvider;
use OCA\Deck\Db\Board; use OCA\Deck\Db\Board;
use OCA\Deck\Service\BoardService;
class CalendarPlugin implements ICalendarProvider { class CalendarPlugin implements ICalendarProvider {
/** /** @var DeckCalendarBackend */
* @var BoardService private $backend;
*/
private $boardService;
public function __construct(BoardService $boardService) { public function __construct(DeckCalendarBackend $backend) {
$this->boardService = $boardService; $this->backend = $backend;
} }
/**
* @inheritDoc
*/
public function getAppId(): string { public function getAppId(): string {
return 'deck'; return 'deck';
} }
/**
* @inheritDoc
*/
public function fetchAllForCalendarHome(string $principalUri): array { public function fetchAllForCalendarHome(string $principalUri): array {
$boards = $this->boardService->findAll();
return array_map(function (Board $board) use ($principalUri) { return array_map(function (Board $board) use ($principalUri) {
return new Calendar($principalUri, 'board-' . $board->getId(), $board); return new Calendar($principalUri, 'board-' . $board->getId(), $board, $this->backend);
}, $boards); }, $this->backend->getBoards());
} }
/**
* @inheritDoc
*/
public function hasCalendarInCalendarHome(string $principalUri, string $calendarUri): bool { public function hasCalendarInCalendarHome(string $principalUri, string $calendarUri): bool {
$boards = array_map(function(Board $board) { $boards = array_map(static function (Board $board) {
return 'board-' . $board->getId(); return 'board-' . $board->getId();
}, $this->boardService->findAll()); }, $this->backend->getBoards());
return in_array($calendarUri, $boards, true); return in_array($calendarUri, $boards, true);
} }
/**
* @inheritDoc
*/
public function getCalendarInCalendarHome(string $principalUri, string $calendarUri): ?ExternalCalendar { public function getCalendarInCalendarHome(string $principalUri, string $calendarUri): ?ExternalCalendar {
if ($this->hasCalendarInCalendarHome($principalUri, $calendarUri)) { if ($this->hasCalendarInCalendarHome($principalUri, $calendarUri)) {
$board = $this->boardService->find(str_replace('board-', '', $calendarUri)); $board = $this->backend->getBoard((int)str_replace('board-', '', $calendarUri));
return new Calendar($principalUri, $calendarUri, $board); return new Calendar($principalUri, $calendarUri, $board, $this->backend);
} }
return null; return null;

View File

@@ -0,0 +1,84 @@
<?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;
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 {
return $this->boardService->find($id);
}
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

@@ -159,5 +159,4 @@ class Card extends RelationalEntity {
public function getCalendarPrefix(): string { public function getCalendarPrefix(): string {
return 'card'; return 'card';
} }
} }

View File

@@ -65,5 +65,4 @@ class Stack extends RelationalEntity {
public function getCalendarPrefix(): string { public function getCalendarPrefix(): string {
return 'stack'; return 'stack';
} }
} }