Compare commits

...

2 commits

Author SHA1 Message Date
Christoph Wurst
9c302de971
Add address book plugins
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
2020-03-05 16:31:52 +01:00
Christoph Wurst
b803fd5333
Add a public event for contacts interaction
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
2020-03-05 16:31:33 +01:00
12 changed files with 431 additions and 17 deletions

View file

@ -27,6 +27,7 @@
*/
// Backends
use OCA\DAV\AppInfo\PluginManager;
use OCA\DAV\CardDAV\AddressBookRoot;
use OCA\DAV\CardDAV\CardDavBackend;
use OCA\DAV\Connector\LegacyDAVACL;
@ -34,6 +35,7 @@ use OCA\DAV\Connector\Sabre\Auth;
use OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin;
use OCA\DAV\Connector\Sabre\MaintenancePlugin;
use OCA\DAV\Connector\Sabre\Principal;
use OCP\App\IAppManager;
use Sabre\CardDAV\Plugin;
$authBackend = new Auth(
@ -63,7 +65,8 @@ $debugging = \OC::$server->getConfig()->getSystemValue('debug', false);
$principalCollection = new \Sabre\CalDAV\Principal\Collection($principalBackend);
$principalCollection->disableListing = !$debugging; // Disable listing
$addressBookRoot = new AddressBookRoot($principalBackend, $cardDavBackend);
$pluginManager = new PluginManager(\OC::$server, \OC::$server->query(IAppManager::class));
$addressBookRoot = new AddressBookRoot($principalBackend, $cardDavBackend, $pluginManager);
$addressBookRoot->disableListing = !$debugging; // Disable listing
$nodes = array(

View file

@ -91,6 +91,8 @@ return array(
'OCA\\DAV\\CardDAV\\Converter' => $baseDir . '/../lib/CardDAV/Converter.php',
'OCA\\DAV\\CardDAV\\HasPhotoPlugin' => $baseDir . '/../lib/CardDAV/HasPhotoPlugin.php',
'OCA\\DAV\\CardDAV\\ImageExportPlugin' => $baseDir . '/../lib/CardDAV/ImageExportPlugin.php',
'OCA\\DAV\\CardDAV\\Integration\\ExternalAddressBook' => $baseDir . '/../lib/CardDAV/Integration/ExternalAddressBook.php',
'OCA\\DAV\\CardDAV\\Integration\\IAddressBookProvider' => $baseDir . '/../lib/CardDAV/Integration/IAddressBookProvider.php',
'OCA\\DAV\\CardDAV\\MultiGetExportPlugin' => $baseDir . '/../lib/CardDAV/MultiGetExportPlugin.php',
'OCA\\DAV\\CardDAV\\PhotoCache' => $baseDir . '/../lib/CardDAV/PhotoCache.php',
'OCA\\DAV\\CardDAV\\Plugin' => $baseDir . '/../lib/CardDAV/Plugin.php',

View file

@ -106,6 +106,8 @@ class ComposerStaticInitDAV
'OCA\\DAV\\CardDAV\\Converter' => __DIR__ . '/..' . '/../lib/CardDAV/Converter.php',
'OCA\\DAV\\CardDAV\\HasPhotoPlugin' => __DIR__ . '/..' . '/../lib/CardDAV/HasPhotoPlugin.php',
'OCA\\DAV\\CardDAV\\ImageExportPlugin' => __DIR__ . '/..' . '/../lib/CardDAV/ImageExportPlugin.php',
'OCA\\DAV\\CardDAV\\Integration\\ExternalAddressBook' => __DIR__ . '/..' . '/../lib/CardDAV/Integration/ExternalAddressBook.php',
'OCA\\DAV\\CardDAV\\Integration\\IAddressBookProvider' => __DIR__ . '/..' . '/../lib/CardDAV/Integration/IAddressBookProvider.php',
'OCA\\DAV\\CardDAV\\MultiGetExportPlugin' => __DIR__ . '/..' . '/../lib/CardDAV/MultiGetExportPlugin.php',
'OCA\\DAV\\CardDAV\\PhotoCache' => __DIR__ . '/..' . '/../lib/CardDAV/PhotoCache.php',
'OCA\\DAV\\CardDAV\\Plugin' => __DIR__ . '/..' . '/../lib/CardDAV/Plugin.php',

View file

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud GmbH.
*
@ -26,8 +29,12 @@ namespace OCA\DAV\AppInfo;
use OC\ServerContainer;
use OCA\DAV\CalDAV\Integration\ICalendarProvider;
use OCA\DAV\CardDAV\Integration\IAddressBookProvider;
use OCP\App\IAppManager;
use OCP\AppFramework\QueryException;
use function array_map;
use function class_exists;
use function is_array;
/**
* Manager for DAV plugins from apps, used to register them
@ -59,6 +66,13 @@ class PluginManager {
*/
private $collections = null;
/**
* Address book plugins
*
* @var IAddressBookProvider[]|null
*/
private $addressBookPlugins = null;
/**
* Calendar plugins
*
@ -101,6 +115,16 @@ class PluginManager {
return $this->collections;
}
/**
* @return IAddressBookProvider[]
*/
public function getAddressBookPlugins(): array {
if ($this->addressBookPlugins === null) {
$this->populate();
}
return $this->addressBookPlugins;
}
/**
* Returns an array of app-registered calendar plugins
*
@ -118,6 +142,7 @@ class PluginManager {
*/
private function populate() {
$this->plugins = [];
$this->addressBookPlugins = [];
$this->calendarPlugins = [];
$this->collections = [];
foreach ($this->appManager->getInstalledApps() as $app) {
@ -128,6 +153,7 @@ class PluginManager {
}
$this->loadSabrePluginsFromInfoXml($this->extractPluginList($info));
$this->loadSabreCollectionsFromInfoXml($this->extractCollectionList($info));
$this->loadSabreAddressBookPluginsFromInfoXml($this->extractAddressBookPluginList($info));
$this->loadSabreCalendarPluginsFromInfoXml($this->extractCalendarPluginList($info));
}
}
@ -162,6 +188,29 @@ class PluginManager {
return [];
}
/**
* @param array $array
*
* @return string[]
*/
private function extractAddressBookPluginList(array $array): array {
if (!isset($array['sabre']) || !is_array($array['sabre'])) {
return [];
}
if (!isset($array['sabre']['address-book-plugins']) || !is_array($array['sabre']['address-book-plugins'])) {
return [];
}
if (!isset($array['sabre']['address-book-plugins']['plugin'])) {
return [];
}
$items = $array['sabre']['address-book-plugins']['plugin'];
if (!is_array($items)) {
$items = [$items];
}
return $items;
}
private function extractCalendarPluginList(array $array):array {
if (isset($array['sabre']) && is_array($array['sabre'])) {
if (isset($array['sabre']['calendar-plugins']) && is_array($array['sabre']['calendar-plugins'])) {
@ -205,6 +254,34 @@ class PluginManager {
}
}
private function createPluginInstance(string $className) {
try {
return $this->container->query($className);
} catch (QueryException $e) {
if (class_exists($className)) {
return new $className();
}
}
throw new \Exception("Sabre plugin class '$className' is unknown and could not be loaded");
}
/**
* @param string[] $plugin
*/
private function loadSabreAddressBookPluginsFromInfoXml(array $plugins): void {
$providers = array_map(function(string $className): IAddressBookProvider {
$instance = $this->createPluginInstance($className);
if (!($instance instanceof IAddressBookProvider)) {
throw new \Exception("Sabre address book plugin class '$className' does not implement the \OCA\DAV\CardDAV\Integration\IAddressBookProvider interface");
}
return $instance;
}, $plugins);
foreach ($providers as $provider) {
$this->addressBookPlugins[] = $provider;
}
}
private function loadSabreCalendarPluginsFromInfoXml(array $calendarPlugins):void {
foreach ($calendarPlugins as $calendarPlugin) {
try {

View file

@ -24,6 +24,7 @@
namespace OCA\DAV\CardDAV;
use OCA\DAV\AppInfo\PluginManager;
use OCP\IL10N;
class AddressBookRoot extends \Sabre\CardDAV\AddressBookRoot {
@ -31,14 +32,20 @@ class AddressBookRoot extends \Sabre\CardDAV\AddressBookRoot {
/** @var IL10N */
protected $l10n;
/** @var PluginManager */
private $pluginManager;
/**
* @param \Sabre\DAVACL\PrincipalBackend\BackendInterface $principalBackend
* @param \Sabre\CardDAV\Backend\BackendInterface $carddavBackend
* @param string $principalPrefix
*/
public function __construct(\Sabre\DAVACL\PrincipalBackend\BackendInterface $principalBackend, \Sabre\CardDAV\Backend\BackendInterface $carddavBackend, $principalPrefix = 'principals') {
public function __construct(\Sabre\DAVACL\PrincipalBackend\BackendInterface $principalBackend,
\Sabre\CardDAV\Backend\BackendInterface $carddavBackend,
PluginManager $pluginManager,
$principalPrefix = 'principals') {
parent::__construct($principalBackend, $carddavBackend, $principalPrefix);
$this->l10n = \OC::$server->getL10N('dav');
$this->pluginManager = $pluginManager;
}
/**
@ -49,12 +56,11 @@ class AddressBookRoot extends \Sabre\CardDAV\AddressBookRoot {
* supplied by the authentication backend.
*
* @param array $principal
*
* @return \Sabre\DAV\INode
*/
function getChildForPrincipal(array $principal) {
return new UserAddressBooks($this->carddavBackend, $principal['uri'], $this->l10n);
return new UserAddressBooks($this->carddavBackend, $principal['uri'], $this->pluginManager);
}
function getName() {

View file

@ -0,0 +1,113 @@
<?php
declare(strict_types=1);
namespace OCA\DAV\CardDAV\Integration;
use Sabre\CardDAV\IAddressBook;
use Sabre\DAV;
/**
* @since 19.0.0
*/
abstract class ExternalAddressBook implements IAddressBook, DAV\IProperties {
/** @var string */
private const PREFIX = 'app-generated';
/**
* @var string
*
* Double dash is a valid delimiter,
* because it will always split the URIs 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
* URI has double dashes, it won't be split
*/
private const DELIMITER = '--';
/** @var string */
private $appId;
/** @var string */
private $uri;
/**
* @param string $appId
* @param string $uri
*/
public function __construct(string $appId, string $uri) {
$this->appId = $appId;
$this->uri = $uri;
}
/**
* @inheritDoc
*/
final public function getName() {
return implode(self::DELIMITER, [
self::PREFIX,
$this->appId,
$this->uri,
]);
}
/**
* @inheritDoc
*/
final public function setName($name) {
throw new DAV\Exception\MethodNotAllowed('Renaming address books is not yet supported');
}
/**
* @inheritDoc
*/
final public function createDirectory($name) {
throw new DAV\Exception\MethodNotAllowed('Creating collections in address book objects is not allowed');
}
/**
* Checks whether the address book uri is app-generated
*
* @param string $uri
*
* @return bool
*/
public static function isAppGeneratedAddressBook(string $uri): bool {
return strpos($uri, self::PREFIX) === 0 && substr_count($uri, self::DELIMITER) >= 2;
}
/**
* Splits an app-generated uri into appId and uri
*
* @param string $uri
*
* @return array
*/
public static function splitAppGeneratedAddressBookUri(string $uri): array {
$array = array_slice(explode(self::DELIMITER, $uri, 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 address book uri was not app-generated');
}
return $array;
}
/**
* Checks whether a address book name the user wants to create violates
* the reserved name for URIs
*
* @param string $uri
*
* @return bool
*/
public static function doesViolateReservedName(string $uri): bool {
return strpos($uri, self::PREFIX) === 0;
}
}

View file

@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
namespace OCA\DAV\CardDAV\Integration;
use Sabre\CardDAV\IAddressBook;
/**
* @since 19.0.0
*/
interface IAddressBookProvider {
/**
* Provides the appId of the plugin
*
* @since 19.0.0
* @return string AppId
*/
public function getAppId(): string;
/**
* Fetches all address books for a given principal uri
*
* @since 19.0.0
* @param string $principalUri E.g. principals/users/user1
* @return ExternalAddressBook[] Array of all address books
*/
public function fetchAllForAddressBookHome(string $principalUri): array;
/**
* Checks whether plugin has an address book for a given principalUri and URI
*
* @since 19.0.0
* @param string $principalUri E.g. principals/users/user1
* @param string $uri E.g. personal
* @return bool True if address book for principalUri and URI exists, false otherwise
*/
public function hasAddressBookInAddressBookHome(string $principalUri, string $uri): bool;
/**
* Fetches an address book for a given principalUri and URI
* Returns null if address book does not exist
*
* @param string $principalUri E.g. principals/users/user1
* @param string $uri E.g. personal
*
* @return ExternalAddressBook|null address book if it exists, null otherwise
*@since 19.0.0
*/
public function getAddressBookInAddressBookHome(string $principalUri, string $uri): ?ExternalAddressBook;
}

View file

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
@ -24,8 +27,10 @@
namespace OCA\DAV\CardDAV;
use OCA\DAV\AppInfo\PluginManager;
use OCP\IConfig;
use OCP\IL10N;
use Sabre\CardDAV\Backend;
class UserAddressBooks extends \Sabre\CardDAV\AddressBookHome {
@ -35,8 +40,18 @@ class UserAddressBooks extends \Sabre\CardDAV\AddressBookHome {
/** @var IConfig */
protected $config;
/** @var PluginManager */
private $pluginManager;
public function __construct(Backend\BackendInterface $carddavBackend,
string $principalUri,
PluginManager $pluginManager) {
parent::__construct($carddavBackend, $principalUri);
$this->pluginManager = $pluginManager;
}
/**
* Returns a list of addressbooks
* Returns a list of address books
*
* @return array
*/
@ -49,13 +64,15 @@ class UserAddressBooks extends \Sabre\CardDAV\AddressBookHome {
}
$addressBooks = $this->carddavBackend->getAddressBooksForUser($this->principalUri);
$objects = [];
foreach($addressBooks as $addressBook) {
$objects = array_map(function(array $addressBook) {
if ($addressBook['principaluri'] === 'principals/system/system') {
$objects[] = new SystemAddressbook($this->carddavBackend, $addressBook, $this->l10n, $this->config);
} else {
$objects[] = new AddressBook($this->carddavBackend, $addressBook, $this->l10n);
return new SystemAddressbook($this->carddavBackend, $addressBook, $this->l10n, $this->config);
}
return new AddressBook($this->carddavBackend, $addressBook, $this->l10n);
}, $addressBooks);
foreach ($this->pluginManager->getAddressBookPlugins() as $plugin) {
$plugin->fetchAllForAddressBookHome($this->principalUri);
}
return $objects;
@ -78,9 +95,9 @@ class UserAddressBooks extends \Sabre\CardDAV\AddressBookHome {
$acl = parent::getACL();
if ($this->principalUri === 'principals/system/system') {
$acl[] = [
'privilege' => '{DAV:}read',
'principal' => '{DAV:}authenticated',
'protected' => true,
'privilege' => '{DAV:}read',
'principal' => '{DAV:}authenticated',
'protected' => true,
];
}

View file

@ -27,6 +27,7 @@
namespace OCA\DAV;
use OCA\DAV\AppInfo\PluginManager;
use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\CalDAV\CalendarRoot;
use OCA\DAV\CalDAV\Principal\Collection;
@ -41,6 +42,7 @@ use OCA\DAV\DAV\GroupPrincipalBackend;
use OCA\DAV\DAV\SystemPrincipalBackend;
use OCA\DAV\Provisioning\Apple\AppleProvisioningNode;
use OCA\DAV\Upload\CleanupService;
use OCP\App\IAppManager;
use OCP\AppFramework\Utility\ITimeFactory;
use Sabre\DAV\SimpleCollection;
@ -123,12 +125,13 @@ class RootCollection extends SimpleCollection {
\OC::$server->getLogger()
);
$pluginManager = new PluginManager(\OC::$server, \OC::$server->query(IAppManager::class));
$usersCardDavBackend = new CardDavBackend($db, $userPrincipalBackend, $userManager, $groupManager, $dispatcher);
$usersAddressBookRoot = new AddressBookRoot($userPrincipalBackend, $usersCardDavBackend, 'principals/users');
$usersAddressBookRoot = new AddressBookRoot($userPrincipalBackend, $usersCardDavBackend, $pluginManager, 'principals/users');
$usersAddressBookRoot->disableListing = $disableListing;
$systemCardDavBackend = new CardDavBackend($db, $userPrincipalBackend, $userManager, $groupManager, $dispatcher);
$systemAddressBookRoot = new AddressBookRoot(new SystemPrincipalBackend(), $systemCardDavBackend, 'principals/system');
$systemAddressBookRoot = new AddressBookRoot(new SystemPrincipalBackend(), $systemCardDavBackend, $pluginManager, 'principals/system');
$systemAddressBookRoot->disableListing = $disableListing;
$uploadCollection = new Upload\RootCollection(

View file

@ -147,6 +147,7 @@ return array(
'OCP\\Contacts\\ContactsMenu\\IEntry' => $baseDir . '/lib/public/Contacts/ContactsMenu/IEntry.php',
'OCP\\Contacts\\ContactsMenu\\ILinkAction' => $baseDir . '/lib/public/Contacts/ContactsMenu/ILinkAction.php',
'OCP\\Contacts\\ContactsMenu\\IProvider' => $baseDir . '/lib/public/Contacts/ContactsMenu/IProvider.php',
'OCP\\Contacts\\Events\\ContactInteractedWithEvent' => $baseDir . '/lib/public/Contacts/Events/ContactInteractedWithEvent.php',
'OCP\\Contacts\\IManager' => $baseDir . '/lib/public/Contacts/IManager.php',
'OCP\\DB\\ISchemaWrapper' => $baseDir . '/lib/public/DB/ISchemaWrapper.php',
'OCP\\DB\\QueryBuilder\\ICompositeExpression' => $baseDir . '/lib/public/DB/QueryBuilder/ICompositeExpression.php',

View file

@ -176,6 +176,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OCP\\Contacts\\ContactsMenu\\IEntry' => __DIR__ . '/../../..' . '/lib/public/Contacts/ContactsMenu/IEntry.php',
'OCP\\Contacts\\ContactsMenu\\ILinkAction' => __DIR__ . '/../../..' . '/lib/public/Contacts/ContactsMenu/ILinkAction.php',
'OCP\\Contacts\\ContactsMenu\\IProvider' => __DIR__ . '/../../..' . '/lib/public/Contacts/ContactsMenu/IProvider.php',
'OCP\\Contacts\\Events\\ContactInteractedWithEvent' => __DIR__ . '/../../..' . '/lib/public/Contacts/Events/ContactInteractedWithEvent.php',
'OCP\\Contacts\\IManager' => __DIR__ . '/../../..' . '/lib/public/Contacts/IManager.php',
'OCP\\DB\\ISchemaWrapper' => __DIR__ . '/../../..' . '/lib/public/DB/ISchemaWrapper.php',
'OCP\\DB\\QueryBuilder\\ICompositeExpression' => __DIR__ . '/../../..' . '/lib/public/DB/QueryBuilder/ICompositeExpression.php',

View file

@ -0,0 +1,136 @@
<?php
declare(strict_types=1);
/**
* @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @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 OCP\Contacts\Events;
use OCP\EventDispatcher\Event;
use OCP\IUser;
/**
* An event that allows apps to notify other components about an interaction
* between two users. This can be used to build better recommendations and
* suggestions in user interfaces.
*
* Emitters should add at least one identifier (uid, email, federated cloud ID)
* of the recipient of the interaction.
*
* @since 19.0.0
*/
class ContactInteractedWithEvent extends Event {
/** @var IUser */
private $actor;
/** @var string|null */
private $uid;
/** @var string|null */
private $email;
/** @var string|null */
private $federatedCloudId;
/**
* @param IUser $actor the user who started the interaction
*
* @since 19.0.0
*/
public function __construct(IUser $actor) {
parent::__construct();
$this->actor = $actor;
}
/**
* @return IUser
* @since 19.0.0
*/
public function getActor(): IUser {
return $this->actor;
}
/**
* @return string|null
* @since 19.0.0
*/
public function getUid(): ?string {
return $this->uid;
}
/**
* Set the uid of the person interacted with, if known
*
* @param string $uid
*
* @return self
* @since 19.0.0
*/
public function setUid(string $uid): self {
$this->uid = $uid;
return $this;
}
/**
* @return string|null
* @since 19.0.0
*/
public function getEmail(): ?string {
return $this->email;
}
/**
* Set the email of the person interacted with, if known
*
* @param string $email
*
* @return self
* @since 19.0.0
*/
public function setEmail(string $email): self {
$this->email = $email;
return $this;
}
/**
* @return string|null
* @since 19.0.0
*/
public function getFederatedCloudId(): ?string {
return $this->federatedCloudId;
}
/**
* Set the federated cloud of the person interacted with, if known
*
* @param string $federatedCloudId
*
* @return self
* @since 19.0.0
*/
public function setFederatedCloudId(string $federatedCloudId): self {
$this->federatedCloudId = $federatedCloudId;
return $this;
}
}