]> source.dussan.org Git - nextcloud-server.git/commitdiff
Added dav endpoints for system tags
authorVincent Petry <pvince81@owncloud.com>
Fri, 27 Nov 2015 11:54:31 +0000 (12:54 +0100)
committerVincent Petry <pvince81@owncloud.com>
Thu, 3 Dec 2015 14:23:21 +0000 (15:23 +0100)
apps/dav/lib/rootcollection.php
apps/dav/lib/server.php
apps/dav/lib/systemtag/systemtagmappingnode.php [new file with mode: 0644]
apps/dav/lib/systemtag/systemtagnode.php [new file with mode: 0644]
apps/dav/lib/systemtag/systemtagplugin.php [new file with mode: 0644]
apps/dav/lib/systemtag/systemtagsbyidcollection.php [new file with mode: 0644]
apps/dav/lib/systemtag/systemtagsobjectmappingcollection.php [new file with mode: 0644]
apps/dav/lib/systemtag/systemtagsobjecttypecollection.php [new file with mode: 0644]
apps/dav/lib/systemtag/systemtagsrelationscollection.php [new file with mode: 0644]
lib/private/systemtag/systemtagobjectmapper.php

index c1635c9cde5a5985d2e91434eb9d1f4803c344cf..9ee32822bbda9f1fed823adc2ac701afdeb7073f 100644 (file)
@@ -33,6 +33,13 @@ class RootCollection extends SimpleCollection {
                $caldavBackend = new CalDavBackend($db);
                $calendarRoot = new CalendarRoot($principalBackend, $caldavBackend, 'principals/users');
                $calendarRoot->disableListing = $disableListing;
+               $systemTagCollection = new SystemTag\SystemTagsByIdCollection(
+                       \OC::$server->getSystemTagManager()
+               );
+               $systemTagRelationsCollection = new SystemTag\SystemTagsRelationsCollection(
+                       \OC::$server->getSystemTagManager(),
+                       \OC::$server->getSystemTagObjectMapper()
+               );
 
                $usersCardDavBackend = new CardDavBackend($db, $principalBackend);
                $usersAddressBookRoot = new AddressBookRoot($principalBackend, $usersCardDavBackend, 'principals/users');
@@ -51,6 +58,8 @@ class RootCollection extends SimpleCollection {
                                new SimpleCollection('addressbooks', [
                                                $usersAddressBookRoot,
                                                $systemAddressBookRoot]),
+                               $systemTagCollection,
+                               $systemTagRelationsCollection,
                ];
 
                parent::__construct('root', $children);
index a031f2c442bbf60ac5848a6ed9ba788c35237ba6..ffcbb02db70a0beeeba70f02d4f42e2d6bf86b7e 100644 (file)
@@ -60,6 +60,9 @@ class Server {
                // addressbook plugins
                $this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin());
 
+               // system tags plugins
+               $this->server->addPlugin(new \OCA\DAV\SystemTag\SystemTagPlugin(\OC::$server->getSystemTagManager()));
+
                // Finder on OS X requires Class 2 WebDAV support (locking), since we do
                // not provide locking we emulate it using a fake locking plugin.
                if($request->isUserAgent(['/WebDAVFS/'])) {
diff --git a/apps/dav/lib/systemtag/systemtagmappingnode.php b/apps/dav/lib/systemtag/systemtagmappingnode.php
new file mode 100644 (file)
index 0000000..97c1afc
--- /dev/null
@@ -0,0 +1,81 @@
+<?php
+/**
+ * @author Vincent Petry <pvince81@owncloud.com>
+ *
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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, version 3,
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OCA\DAV\SystemTag;
+
+use Sabre\DAV\Exception\NotFound;
+use Sabre\DAV\Exception\MethodNotAllowed;
+
+use OCP\SystemTag\ISystemTag;
+use OCP\SystemTag\ISystemTagManager;
+use OCP\SystemTag\ISystemTagObjectMapper;
+use OCP\SystemTag\TagNotFoundException;
+
+/**
+ * Mapping node for system tag to object id
+ */
+class SystemTagMappingNode extends SystemTagNode {
+
+       /**
+        * @var ISystemTagObjectMapper
+        */
+       private $tagMapper;
+
+       /**
+        * @var string
+        */
+       private $objectId;
+
+       /**
+        * @var string
+        */
+       private $objectType;
+
+       /**
+        * Sets up the node, expects a full path name
+        *
+        * @param ISystemTag $tag system tag
+        */
+       public function __construct(
+               ISystemTag $tag,
+               $objectId,
+               $objectType,
+               ISystemTagManager $tagManager,
+               ISystemTagObjectMapper $tagMapper
+       ) {
+               $this->objectId = $objectId;
+               $this->objectType = $objectType;
+               $this->tagMapper = $tagMapper;
+               parent::__construct($tag, $tagManager);
+       }
+
+       /**
+        * Delete tag to object association
+        */
+       public function delete() {
+               try {
+                       $this->tagMapper->unassignTags($this->objectId, $this->objectType, $this->tag->getId());
+               } catch (TagNotFoundException $e) {
+                       // can happen if concurrent deletion occurred
+                       throw new NotFound('Tag with id ' . $this->tag->getId() . ' not found', 0, $e);
+               }
+       }
+}
diff --git a/apps/dav/lib/systemtag/systemtagnode.php b/apps/dav/lib/systemtag/systemtagnode.php
new file mode 100644 (file)
index 0000000..e425595
--- /dev/null
@@ -0,0 +1,112 @@
+<?php
+/**
+ * @author Vincent Petry <pvince81@owncloud.com>
+ *
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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, version 3,
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OCA\DAV\SystemTag;
+
+use Sabre\DAV\Exception\NotFound;
+use Sabre\DAV\Exception\MethodNotAllowed;
+use Sabre\DAV\Exception\Conflict;
+
+use OCP\SystemTag\ISystemTag;
+use OCP\SystemTag\ISystemTagManager;
+use OCP\SystemTag\TagNotFoundException;
+
+class SystemTagNode implements \Sabre\DAV\INode {
+
+       /**
+        * @var ISystemTag
+        */
+       protected $tag;
+
+       /**
+        * @var ISystemTagManager
+        */
+       protected $tagManager;
+
+       /**
+        * Sets up the node, expects a full path name
+        *
+        * @param ISystemTag $tag system tag
+        */
+       public function __construct(ISystemTag $tag, ISystemTagManager $tagManager) {
+               $this->tag = $tag;
+               $this->tagManager = $tagManager;
+       }
+
+       /**
+        *  Returns the id of the tag
+        *
+        * @return string
+        */
+       public function getName() {
+               return $this->tag->getId();
+       }
+
+       /**
+        * Returns the system tag represented by this node
+        *
+        * @return ISystemTag system tag
+        */
+       public function getSystemTag() {
+               return $this->tag;
+       }
+
+       /**
+        * Renames the node
+        *
+        * @param string $name The new name
+        *
+        * @throws \Sabre\DAV\Exception\MethodNotAllowed
+        */
+       public function setName($name) {
+               throw new MethodNotAllowed();
+       }
+
+       /**
+        * @param string $name new tag name
+        * @param bool $userVisible user visible
+        * @param bool $userAssignable user assignable
+        */
+       public function update($name, $userVisible, $userAssignable) {
+               try {
+                       $this->tagManager->updateTag($name, $userVisible, $userAssignable);
+               } catch (TagAlreadyExistsException $e) {
+                       throw new Conflict('Tag with the properties "' . $name . '", ' . $userVisible, ', ' . $userAssignable . ' already exists');
+               }
+       }
+
+       /**
+        * Returns null, not supported
+        *
+        */
+       public function getLastModified() {
+               return null;
+       }
+
+       public function delete() {
+               try {
+                       $this->tagManager->deleteTags($this->tag->getId());
+               } catch (TagNotFoundException $e) {
+                       // can happen if concurrent deletion occurred
+                       throw new NotFound('Tag with id ' . $this->tag->getId() . ' not found', 0, $e);
+               }
+       }
+}
diff --git a/apps/dav/lib/systemtag/systemtagplugin.php b/apps/dav/lib/systemtag/systemtagplugin.php
new file mode 100644 (file)
index 0000000..c213640
--- /dev/null
@@ -0,0 +1,241 @@
+<?php
+/**
+ * @author Vincent Petry <pvince81@owncloud.com>
+ *
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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, version 3,
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+namespace OCA\DAV\SystemTag;
+
+use Sabre\DAV\PropFind;
+use Sabre\DAV\PropPatch;
+use Sabre\DAV\Exception\BadRequest;
+use Sabre\DAV\Exception\UnsupportedMediaType;
+use Sabre\DAV\Exception\Conflict;
+
+use OCA\DAV\SystemTag\SystemTagNode;
+use OCP\SystemTag\ISystemTag;
+use OCP\SystemTag\ISystemTagManager;
+use OCP\SystemTag\TagAlreadyExistsException;
+
+class SystemTagPlugin extends \Sabre\DAV\ServerPlugin {
+
+       // namespace
+       const NS_OWNCLOUD = 'http://owncloud.org/ns';
+       const ID_PROPERTYNAME = '{http://owncloud.org/ns}id';
+       const DISPLAYNAME_PROPERTYNAME = '{http://owncloud.org/ns}display-name';
+       const USERVISIBLE_PROPERTYNAME = '{http://owncloud.org/ns}user-visible';
+       const USERASSIGNABLE_PROPERTYNAME = '{http://owncloud.org/ns}user-assignable';
+
+       /**
+        * @var \Sabre\DAV\Server $server
+        */
+       private $server;
+
+       /**
+        * @var ISystemTagManager
+        */
+       protected $tagManager;
+
+       /**
+        * System tags plugin
+        *
+        * @param ISystemTagManager $tagManager tag manager
+        */
+       public function __construct(ISystemTagManager $tagManager) {
+               $this->tagManager = $tagManager;
+       }
+
+       /**
+        * This initializes the plugin.
+        *
+        * This function is called by \Sabre\DAV\Server, after
+        * addPlugin is called.
+        *
+        * This method should set up the required event subscriptions.
+        *
+        * @param \Sabre\DAV\Server $server
+        * @return void
+        */
+       public function initialize(\Sabre\DAV\Server $server) {
+
+               $server->xmlNamespaces[self::NS_OWNCLOUD] = 'oc';
+
+               $server->protectedProperties[] = self::ID_PROPERTYNAME;
+
+               $server->on('propFind', array($this, 'handleGetProperties'));
+               $server->on('propPatch', array($this, 'handleUpdateProperties'));
+               $server->on('method:POST', [$this, 'httpPost']);
+
+               $this->server = $server;
+       }
+
+       /**
+        * We intercept this to handle POST requests on calendars.
+        *
+        * @param RequestInterface $request
+        * @param ResponseInterface $response
+        * @return null|false
+        */
+       function httpPost(RequestInterface $request, ResponseInterface $response) {
+               $path = $request->getPath();
+
+               // Making sure the node exists
+               try {
+                       $node = $this->server->tree->getNodeForPath($path);
+               } catch (NotFound $e) {
+                       return;
+               }
+
+               if ($node instanceof SystemTagsByIdCollection || $node instanceof SystemTagsObjectMappingCollection) {
+                       $data = $request->getBodyAsString();
+
+                       $tag = $this->createTag($data, $request->getHeader('Content-Type'));
+
+                       if ($node instanceof SystemTagsObjectMappingCollection) {
+                               // also add to collection
+                               $node->createFile($tag->getId());
+                               $url = $request->getBaseUrl() . 'systemtags/';
+                       } else {
+                               $url = $request->getUrl();
+                       }
+
+                       if ($url[strlen($url) - 1] !== '/') {
+                               $url .= '/';
+                       }
+
+                       $response->setHeader('Location', $url . $tag->getId());
+
+                       // created
+                       $response->setStatus(201);
+                       return false;
+               }
+       }
+
+       /**
+        * Creates a new tag
+        *
+        * @param string $data
+        * @param string $contentType content type of the data
+        *
+        * @return ISystemTag newly created system tag
+        *
+        * @throws UnsupportedMediaType if the content type is not supported
+        * @throws BadRequest if a field was missing
+        */
+       private function createTag($data, $contentType = 'application/json') {
+               if ($contentType === 'application/json') {
+                       $data = json_decode($data, true);
+                       // TODO: application/x-www-form-urlencoded ?
+               } else {
+                       throw new UnsupportedMediaType();
+               }
+
+               if (!isset($data['name'])) {
+                       throw new BadRequest('Missing "name" attribute');
+               }
+
+               $tagName = $data['name'];
+               $userVisible = true;
+               $userAssignable = true;
+
+               if (isset($data['userVisible'])) {
+                       $userVisible = (bool)$data['userVisible'];
+               }
+
+               if (isset($data['userAssignable'])) {
+                       $userVisible = (bool)$data['userAssignable'];
+               }
+               try {
+                       return $this->tagManager->createTag($tagName, $userVisible, $userAssignable);
+               } catch (TagAlreadyExistsException $e) {
+                       throw new Conflict('Tag already exists');
+               }
+       }
+
+
+       /**
+        * Retrieves system tag properties
+        *
+        * @param PropFind $propFind
+        * @param \Sabre\DAV\INode $node
+        */
+       public function handleGetProperties(
+               PropFind $propFind,
+               \Sabre\DAV\INode $node
+       ) {
+               if (!($node instanceof SystemTagNode)) {
+                       return;
+               }
+
+               $propFind->handle(self::ID_PROPERTYNAME, function() use ($node) {
+                       return $node->getSystemTag()->getId();
+               });
+
+               $propFind->handle(self::DISPLAYNAME_PROPERTYNAME, function() use ($node) {
+                       return $node->getSystemTag()->getName();
+               });
+
+               $propFind->handle(self::USERVISIBLE_PROPERTYNAME, function() use ($node) {
+                       return (int)$node->getSystemTag()->isUserVisible();
+               });
+
+               $propFind->handle(self::USERASSIGNABLE_PROPERTYNAME, function() use ($node) {
+                       return (int)$node->getSystemTag()->isUserAssignable();
+               });
+       }
+
+       /**
+        * Updates tag attributes
+        *
+        * @param string $path
+        * @param PropPatch $propPatch
+        *
+        * @return void
+        */
+       public function handleUpdateProperties($path, PropPatch $propPatch) {
+               $propPatch->handle([
+                       self::DISPLAYNAME_PROPERTYNAME,
+                       self::USERVISIBLE_PROPERTYNAME,
+                       self::USERASSIGNABLE_PROPERTYNAME,
+               ], function($props) use ($path) {
+                       $node = $this->tree->getNodeForPath($path);
+                       if (!($node instanceof SystemTagNode)) {
+                               return;
+                       }
+
+                       $tag = $node->getTag();
+                       $name = $tag->getName();
+                       $userVisible = $tag->getUserVisible();
+                       $userAssignable = $tag->getUserAssignable();
+
+                       if (isset($props[self::DISPLAYNAME_PROPERTYNAME])) {
+                               $name = $props[self::DISPLAYNAME_PROPERTYNAME];
+                       }
+
+                       if (isset($props[self::USERVISIBLE_PROPERTYNAME])) {
+                               $userVisible = (bool)$props[self::USERVISIBLE_PROPERTYNAME];
+                       }
+
+                       if (isset($props[self::USERASSIGNABLE_PROPERTYNAME])) {
+                               $userAssignable = (bool)$props[self::USERASSIGNABLE_PROPERTYNAME];
+                       }
+
+                       $node->update($name, $userVisible, $userAssignable);
+                       return true;
+               });
+       }
+}
diff --git a/apps/dav/lib/systemtag/systemtagsbyidcollection.php b/apps/dav/lib/systemtag/systemtagsbyidcollection.php
new file mode 100644 (file)
index 0000000..8643ffe
--- /dev/null
@@ -0,0 +1,119 @@
+<?php
+/**
+ * @author Vincent Petry <pvince81@owncloud.com>
+ *
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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, version 3,
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OCA\DAV\SystemTag;
+
+use Sabre\DAV\Exception\Forbidden;
+use Sabre\DAV\Exception\NotFound;
+use Sabre\DAV\Exception\BadRequest;
+use Sabre\DAV\ICollection;
+
+use OCP\SystemTag\ISystemTagManager;
+use OCP\SystemTag\ISystemTag;
+use OCP\SystemTag\TagNotFoundException;
+use OC\SystemTag\SystemTag;
+
+class SystemTagsByIdCollection implements ICollection {
+
+       /**
+        * @var ISystemTagManager
+        */
+       private $tagManager;
+
+       /**
+        * SystemTagsByIdCollection constructor.
+        *
+        * @param ISystemTagManager $tagManager
+        */
+       public function __construct($tagManager) {
+               $this->tagManager = $tagManager;
+       }
+
+       function createFile($name, $data = null) {
+               throw new Forbidden('Cannot create tags by id');
+       }
+
+       function createDirectory($name) {
+               throw new Forbidden('Permission denied to create collections');
+       }
+
+       function getChild($name) {
+               try {
+                       $tags = $this->tagManager->getTagsById($name);
+                       return $this->makeNode(current($tags));
+               } catch (\InvalidArgumentException $e) {
+                       throw new BadRequest('Invalid tag id', 0, $e);
+               } catch (TagNotFoundException $e) {
+                       throw new NotFound('Tag with id ' . $name . ' not found', 0, $e);
+               }
+       }
+
+       function getChildren() {
+               // TODO: set visibility filter based on principal/permissions ?
+               $tags = $this->tagManager->getAllTags(true);
+               return array_map(function($tag) {
+                       return $this->makeNode($tag);
+               }, $tags);
+       }
+
+       function childExists($name) {
+               try {
+                       $this->tagManager->getTagsById($name);
+                       return true;
+               } catch (\InvalidArgumentException $e) {
+                       throw new BadRequest('Invalid tag id', 0, $e);
+               } catch (TagNotFoundException $e) {
+                       return false;
+               }
+       }
+
+       function delete() {
+               throw new Forbidden('Permission denied to delete this collection');
+       }
+
+       function getName() {
+               return 'systemtags';
+       }
+
+       function setName($name) {
+               throw new Forbidden('Permission denied to rename this collection');
+       }
+
+       /**
+        * Returns the last modification time, as a unix timestamp
+        *
+        * @return int
+        */
+       function getLastModified() {
+               return null;
+       }
+
+       /**
+        * Create a sabre node for the given system tag
+        *
+        * @param ISystemTag $tag
+        *
+        * @return SystemTagNode
+        */
+       private function makeNode(ISystemTag $tag) {
+               return new SystemTagNode($tag, $this->tagManager);
+       }
+}
diff --git a/apps/dav/lib/systemtag/systemtagsobjectmappingcollection.php b/apps/dav/lib/systemtag/systemtagsobjectmappingcollection.php
new file mode 100644 (file)
index 0000000..a0a7130
--- /dev/null
@@ -0,0 +1,160 @@
+<?php
+/**
+ * @author Vincent Petry <pvince81@owncloud.com>
+ *
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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, version 3,
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OCA\DAV\SystemTag;
+
+use Sabre\DAV\Exception\Forbidden;
+use Sabre\DAV\Exception\NotFound;
+use Sabre\DAV\Exception\MethodNotAllowed;
+use Sabre\DAV\Exception\BadRequest;
+use Sabre\DAV\ICollection;
+
+use OCP\SystemTag\ISystemTagManager;
+use OCP\SystemTag\ISystemTagObjectMapper;
+use OCP\SystemTag\ISystemTag;
+use OCP\SystemTag\TagNotFoundException;
+use OC\SystemTag\SystemTag;
+
+/**
+ * Collection containing tags by object id
+ */
+class SystemTagsObjectMappingCollection implements ICollection {
+
+       /**
+        * @var string
+        */
+       private $objectId;
+
+       /**
+        * @var string
+        */
+       private $objectType;
+
+       /**
+        * @var ISystemTagManager
+        */
+       private $tagManager;
+
+       /**
+        * @var ISystemTagObjectMapper
+        */
+       private $tagMapper;
+
+       /**
+        * Constructor
+        *
+        * @param string $objectId object id
+        * @param string $objectType object type
+        * @param ISystemTagManager $tagManager
+        * @param ISystemTagObjectMapper $tagMapper
+        */
+       public function __construct($objectId, $objectType, $tagManager, $tagMapper) {
+               $this->tagManager = $tagManager;
+               $this->tagMapper = $tagMapper;
+               $this->objectId = $objectId;
+               $this->objectType = $objectType;
+       }
+
+       function createFile($tagId, $data = null) {
+               try {
+                       $this->tagMapper->assignTags($this->objectId, $this->objectType, $tagId);
+               } catch (TagNotFoundException $e) {
+                       throw new Forbidden('Tag with id ' . $tagId . ' does not exist, cannot assign');
+               }
+       }
+
+       function createDirectory($name) {
+               throw new Forbidden('Permission denied to create collections');
+       }
+
+       function getChild($tagId) {
+               try {
+                       if ($this->tagMapper->haveTag($this->objectId, $this->objectType, $tagId, true)) {
+                               $tag = $this->tagManager->getTagsById($tagId);
+                               return $this->makeNode(current($tag));
+                       }
+                       throw new NotFound('Tag with id ' . $tagId . ' not present for object ' . $this->objectId);
+               } catch (\InvalidArgumentException $e) {
+                       throw new BadRequest('Invalid tag id', 0, $e);
+               } catch (TagNotFoundException $e) {
+                       throw new NotFound('Tag with id ' . $tagId . ' not found', 0, $e);
+               }
+       }
+
+       function getChildren() {
+               $tagIds = current($this->tagMapper->getTagIdsForObjects($this->objectId, $this->objectType));
+               if (empty($tagIds)) {
+                       return [];
+               }
+               $tags = $this->tagManager->getTagsById($tagIds);
+               return array_map(function($tag) {
+                       return $this->makeNode($tag);
+               }, $tags);
+       }
+
+       function childExists($tagId) {
+               try {
+                       return ($this->tagMapper->haveTag($this->objectId, $this->objectType, $tagId, true));
+               } catch (\InvalidArgumentException $e) {
+                       throw new BadRequest('Invalid tag id', 0, $e);
+               } catch (TagNotFoundException $e) {
+                       throw new NotFound('Tag with id ' . $tagId . ' not found', 0, $e);
+               }
+       }
+
+       function delete() {
+               throw new Forbidden('Permission denied to delete this collection');
+       }
+
+       function getName() {
+               return $this->objectId;
+       }
+
+       function setName($name) {
+               throw new Forbidden('Permission denied to rename this collection');
+       }
+
+       /**
+        * Returns the last modification time, as a unix timestamp
+        *
+        * @return int
+        */
+       function getLastModified() {
+               return null;
+       }
+
+       /**
+        * Create a sabre node for the given system tag
+        *
+        * @param ISystemTag $tag
+        *
+        * @return SystemTagNode
+        */
+       private function makeNode(ISystemTag $tag) {
+               return new SystemTagMappingNode(
+                       $tag,
+                       $this->objectId,
+                       $this->objectType,
+                       $this->tagManager,
+                       $this->tagMapper
+               );
+       }
+}
diff --git a/apps/dav/lib/systemtag/systemtagsobjecttypecollection.php b/apps/dav/lib/systemtag/systemtagsobjecttypecollection.php
new file mode 100644 (file)
index 0000000..c90e4fd
--- /dev/null
@@ -0,0 +1,114 @@
+<?php
+/**
+ * @author Vincent Petry <pvince81@owncloud.com>
+ *
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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, version 3,
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OCA\DAV\SystemTag;
+
+use Sabre\DAV\Exception\Forbidden;
+use Sabre\DAV\Exception\NotFound;
+use Sabre\DAV\Exception\MethodNotAllowed;
+use Sabre\DAV\ICollection;
+
+use OCP\SystemTag\ISystemTagManager;
+use OCP\SystemTag\ISystemTagObjectMapper;
+use OCP\SystemTag\ISystemTag;
+use OCP\SystemTag\TagNotFoundException;
+use OC\SystemTag\SystemTag;
+
+/**
+ * Collection containing object ids by object type
+ */
+class SystemTagsObjectTypeCollection implements ICollection {
+
+       /**
+        * @var string
+        */
+       private $objectType;
+
+       /**
+        * @var ISystemTagManager
+        */
+       private $tagManager;
+
+       /**
+        * @var ISystemTagObjectMapper
+        */
+       private $tagMapper;
+
+       /**
+        * Constructor
+        *
+        * @param string $objectType object type
+        * @param ISystemTagManager $tagManager
+        * @param ISystemTagObjectMapper $tagMapper
+        */
+       public function __construct($objectType, $tagManager, $tagMapper) {
+               $this->tagManager = $tagManager;
+               $this->tagMapper = $tagMapper;
+               $this->objectType = $objectType;
+       }
+
+       function createFile($name, $data = null) {
+               throw new Forbidden('Permission denied to create collections');
+       }
+
+       function createDirectory($name) {
+               throw new Forbidden('Permission denied to create collections');
+       }
+
+       function getChild($objectId) {
+               return new SystemTagsObjectMappingCollection(
+                       $objectId,
+                       $this->objectType,
+                       $this->tagManager,
+                       $this->tagMapper
+               );
+       }
+
+       function getChildren() {
+               // do not list object ids
+               throw new MethodNotAllowed();
+       }
+
+       function childExists($name) {
+               return true;
+       }
+
+       function delete() {
+               throw new Forbidden('Permission denied to delete this collection');
+       }
+
+       function getName() {
+               return $this->objectType;
+       }
+
+       function setName($name) {
+               throw new Forbidden('Permission denied to rename this collection');
+       }
+
+       /**
+        * Returns the last modification time, as a unix timestamp
+        *
+        * @return int
+        */
+       function getLastModified() {
+               return null;
+       }
+}
diff --git a/apps/dav/lib/systemtag/systemtagsrelationscollection.php b/apps/dav/lib/systemtag/systemtagsrelationscollection.php
new file mode 100644 (file)
index 0000000..4bcb688
--- /dev/null
@@ -0,0 +1,50 @@
+<?php
+/**
+ * @author Vincent Petry <pvince81@owncloud.com>
+ *
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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, version 3,
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OCA\DAV\SystemTag;
+
+use Sabre\DAV\SimpleCollection;
+
+class SystemTagsRelationsCollection extends SimpleCollection {
+
+       /**
+        * SystemTagsRelationsCollection constructor.
+        *
+        * @param ISystemTagManager $tagManager
+        * @param ISystemTagObjectMapper $tagMapper
+        */
+       public function __construct($tagManager, $tagMapper) {
+               $children = [
+                       new SystemTagsObjectTypeCollection('files', $tagManager, $tagMapper),
+               ];
+
+               parent::__construct('root', $children);
+       }
+
+       function getName() {
+               return 'systemtags-relations';
+       }
+
+       function setName($name) {
+               throw new Forbidden('Permission denied to rename this collection');
+       }
+
+}
index 75f2631a010b5b5a5270426172d94e1b63083941..bb64a35456f6be9523664417f30b3e3c24f524b7 100644 (file)
@@ -171,6 +171,10 @@ class SystemTagObjectMapper implements ISystemTagObjectMapper {
        public function haveTag($objIds, $objectType, $tagId, $all = true) {
                $this->assertTagsExist([$tagId]);
 
+               if (!is_array($objIds)) {
+                       $objIds = [$objIds];
+               }
+
                $query = $this->connection->getQueryBuilder();
 
                if (!$all) {