mirror of https://github.com/nextcloud/server.git
Allow apps to provide Calendars in user's calendarHome
Signed-off-by: Georg Ehrke <developer@georgehrke.com>pull/19196/head
parent
a1fc233fcb
commit
b46e5cb270
@ -0,0 +1,132 @@
|
||||
<?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\DAV\CalDAV\Integration;
|
||||
|
||||
use Sabre\CalDAV;
|
||||
use Sabre\DAV;
|
||||
|
||||
/**
|
||||
* Class ExternalCalendar
|
||||
*
|
||||
* @package OCA\DAV\CalDAV\Integration
|
||||
* @since 19.0.0
|
||||
*/
|
||||
abstract class ExternalCalendar implements CalDAV\ICalendar, DAV\IProperties {
|
||||
|
||||
/** @var string */
|
||||
private const PREFIX = 'app-generated';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* Double dash is a valid delimiter,
|
||||
* because it will always split the calendarURIs correctly:
|
||||
* - our prefix contains only one dash and won't be split
|
||||
* - appIds are not allowed to contain dashes as per spec:
|
||||
* > must contain only lowercase ASCII characters and underscore
|
||||
* - explode has a limit of three, so even if the app-generated
|
||||
* calendar uri has double dashes, it won't be split
|
||||
*/
|
||||
private const DELIMITER = '--';
|
||||
|
||||
/** @var string */
|
||||
private $appId;
|
||||
|
||||
/** @var string */
|
||||
private $calendarUri;
|
||||
|
||||
/**
|
||||
* ExternalCalendar constructor.
|
||||
*
|
||||
* @param string $appId
|
||||
* @param string $calendarUri
|
||||
*/
|
||||
public function __construct(string $appId, string $calendarUri) {
|
||||
$this->appId = $appId;
|
||||
$this->calendarUri = $calendarUri;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
final public function getName() {
|
||||
return implode(self::DELIMITER, [
|
||||
self::PREFIX,
|
||||
$this->appId,
|
||||
$this->calendarUri,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
final public function setName($name) {
|
||||
throw new DAV\Exception\MethodNotAllowed('Renaming calendars is not yet supported');
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
final public function createDirectory($name) {
|
||||
throw new DAV\Exception\MethodNotAllowed('Creating collections in calendar objects is not allowed');
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the calendar uri is app-generated
|
||||
*
|
||||
* @param string $calendarUri
|
||||
* @return bool
|
||||
*/
|
||||
public static function isAppGeneratedCalendar(string $calendarUri):bool {
|
||||
return strpos($calendarUri, self::PREFIX) === 0 && substr_count($calendarUri, self::DELIMITER) >= 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits an app-generated calendar-uri into appId and calendarUri
|
||||
*
|
||||
* @param string $calendarUri
|
||||
* @return array
|
||||
*/
|
||||
public static function splitAppGeneratedCalendarUri(string $calendarUri):array {
|
||||
$array = array_slice(explode(self::DELIMITER, $calendarUri, 3), 1);
|
||||
// Check the array has expected amount of elements
|
||||
// and none of them is an empty string
|
||||
if (\count($array) !== 2 || \in_array('', $array, true)) {
|
||||
throw new \InvalidArgumentException('Provided calendar uri was not app-generated');
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a calendar-name, the user wants to create, violates
|
||||
* the reserved name for calendar uris
|
||||
*
|
||||
* @param string $calendarUri
|
||||
* @return bool
|
||||
*/
|
||||
public static function doesViolateReservedName(string $calendarUri):bool {
|
||||
return strpos($calendarUri, self::PREFIX) === 0;
|
||||
}
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
<?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\DAV\CalDAV\Integration;
|
||||
|
||||
use Sabre\CalDAV\ICalendar;
|
||||
|
||||
/**
|
||||
* Interface ICalendarProvider
|
||||
*
|
||||
* @package OCA\DAV\CalDAV\Integration
|
||||
* @since 19.0.0
|
||||
*/
|
||||
interface ICalendarProvider {
|
||||
|
||||
/**
|
||||
* Provides the appId of the plugin
|
||||
*
|
||||
* @since 19.0.0
|
||||
* @return string AppId
|
||||
*/
|
||||
public function getAppId(): string;
|
||||
|
||||
/**
|
||||
* Fetches all calendars for a given principal uri
|
||||
*
|
||||
* @since 19.0.0
|
||||
* @param string $principalUri E.g. principals/users/user1
|
||||
* @return ExternalCalendar[] Array of all calendars
|
||||
*/
|
||||
public function fetchAllForCalendarHome(string $principalUri): array;
|
||||
|
||||
/**
|
||||
* Checks whether plugin has a calendar for a given principalUri and calendarUri
|
||||
*
|
||||
* @since 19.0.0
|
||||
* @param string $principalUri E.g. principals/users/user1
|
||||
* @param string $calendarUri E.g. personal
|
||||
* @return bool True if calendar for principalUri and calendarUri exists, false otherwise
|
||||
*/
|
||||
public function hasCalendarInCalendarHome(string $principalUri, string $calendarUri): bool;
|
||||
|
||||
/**
|
||||
* Fetches a calendar for a given principalUri and calendarUri
|
||||
* Returns null if calendar does not exist
|
||||
*
|
||||
* @since 19.0.0
|
||||
* @param string $principalUri E.g. principals/users/user1
|
||||
* @param string $calendarUri E.g. personal
|
||||
* @return ExternalCalendar|null Calendar if it exists, null otherwise
|
||||
*/
|
||||
public function getCalendarInCalendarHome(string $principalUri, string $calendarUri): ?ExternalCalendar;
|
||||
|
||||
}
|
@ -0,0 +1,119 @@
|
||||
<?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\DAV\Tests\unit\CalDAV\Integration;
|
||||
|
||||
use OCA\DAV\CalDAV\Integration\ExternalCalendar;
|
||||
use Test\TestCase;
|
||||
|
||||
class ExternalCalendarTest extends TestCase {
|
||||
|
||||
private $abstractExternalCalendar;
|
||||
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
$this->abstractExternalCalendar =
|
||||
$this->getMockForAbstractClass(ExternalCalendar::class, ['example-app-id', 'calendar-uri-in-backend']);
|
||||
}
|
||||
|
||||
public function testGetName():void {
|
||||
// Check that the correct name is returned
|
||||
$this->assertEquals('app-generated--example-app-id--calendar-uri-in-backend',
|
||||
$this->abstractExternalCalendar->getName());
|
||||
|
||||
// Check that the method is final and can't be overriden by other classes
|
||||
$reflectionMethod = new \ReflectionMethod(ExternalCalendar::class, 'getName');
|
||||
$this->assertTrue($reflectionMethod->isFinal());
|
||||
}
|
||||
|
||||
public function testSetName():void {
|
||||
// Check that the method is final and can't be overriden by other classes
|
||||
$reflectionMethod = new \ReflectionMethod(ExternalCalendar::class, 'setName');
|
||||
$this->assertTrue($reflectionMethod->isFinal());
|
||||
|
||||
$this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class);
|
||||
$this->expectExceptionMessage('Renaming calendars is not yet supported');
|
||||
|
||||
$this->abstractExternalCalendar->setName('other-name');
|
||||
}
|
||||
|
||||
public function createDirectory():void {
|
||||
// Check that the method is final and can't be overriden by other classes
|
||||
$reflectionMethod = new \ReflectionMethod(ExternalCalendar::class, 'createDirectory');
|
||||
$this->assertTrue($reflectionMethod->isFinal());
|
||||
|
||||
$this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class);
|
||||
$this->expectExceptionMessage('Creating collections in calendar objects is not allowed');
|
||||
|
||||
$this->abstractExternalCalendar->createDirectory('other-name');
|
||||
}
|
||||
|
||||
public function testIsAppGeneratedCalendar():void {
|
||||
$this->assertFalse(ExternalCalendar::isAppGeneratedCalendar('personal'));
|
||||
$this->assertFalse(ExternalCalendar::isAppGeneratedCalendar('work'));
|
||||
$this->assertFalse(ExternalCalendar::isAppGeneratedCalendar('contact_birthdays'));
|
||||
$this->assertFalse(ExternalCalendar::isAppGeneratedCalendar('company'));
|
||||
$this->assertFalse(ExternalCalendar::isAppGeneratedCalendar('app-generated'));
|
||||
$this->assertFalse(ExternalCalendar::isAppGeneratedCalendar('app-generated--example'));
|
||||
|
||||
$this->assertTrue(ExternalCalendar::isAppGeneratedCalendar('app-generated--deck--board-1'));
|
||||
$this->assertTrue(ExternalCalendar::isAppGeneratedCalendar('app-generated--example--foo-2'));
|
||||
$this->assertTrue(ExternalCalendar::isAppGeneratedCalendar('app-generated--example--foo--2'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider splitAppGeneratedCalendarUriDataProvider
|
||||
*/
|
||||
public function testSplitAppGeneratedCalendarUriInvalid(string $name):void {
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage('Provided calendar uri was not app-generated');
|
||||
|
||||
ExternalCalendar::splitAppGeneratedCalendarUri($name);
|
||||
}
|
||||
|
||||
public function splitAppGeneratedCalendarUriDataProvider():array {
|
||||
return [
|
||||
['personal'],
|
||||
['foo_shared_by_admin'],
|
||||
['contact_birthdays'],
|
||||
];
|
||||
}
|
||||
|
||||
public function testSplitAppGeneratedCalendarUri():void {
|
||||
$this->assertEquals(['deck', 'board-1'], ExternalCalendar::splitAppGeneratedCalendarUri('app-generated--deck--board-1'));
|
||||
$this->assertEquals(['example', 'foo-2'], ExternalCalendar::splitAppGeneratedCalendarUri('app-generated--example--foo-2'));
|
||||
$this->assertEquals(['example', 'foo--2'], ExternalCalendar::splitAppGeneratedCalendarUri('app-generated--example--foo--2'));
|
||||
}
|
||||
|
||||
public function testDoesViolateReservedName():void {
|
||||
$this->assertFalse(ExternalCalendar::doesViolateReservedName('personal'));
|
||||
$this->assertFalse(ExternalCalendar::doesViolateReservedName('work'));
|
||||
$this->assertFalse(ExternalCalendar::doesViolateReservedName('contact_birthdays'));
|
||||
$this->assertFalse(ExternalCalendar::doesViolateReservedName('company'));
|
||||
|
||||
$this->assertTrue(ExternalCalendar::doesViolateReservedName('app-generated'));
|
||||
$this->assertTrue(ExternalCalendar::doesViolateReservedName('app-generated-calendar'));
|
||||
$this->assertTrue(ExternalCalendar::doesViolateReservedName('app-generated--deck-123'));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue