Merge pull request #394 from nextcloud/tags-for-everything

SystemTags for everything not just files
This commit is contained in:
Morris Jobke 2016-07-19 10:47:12 +02:00 committed by GitHub
commit 0a6d95b126
9 changed files with 144 additions and 51 deletions

View File

@ -72,7 +72,7 @@ class RootCollection extends SimpleCollection {
\OC::$server->getSystemTagObjectMapper(), \OC::$server->getSystemTagObjectMapper(),
\OC::$server->getUserSession(), \OC::$server->getUserSession(),
\OC::$server->getGroupManager(), \OC::$server->getGroupManager(),
\OC::$server->getRootFolder() \OC::$server->getEventDispatcher()
); );
$commentsCollection = new Comments\RootCollection( $commentsCollection = new Comments\RootCollection(
\OC::$server->getCommentsManager(), \OC::$server->getCommentsManager(),

View File

@ -31,7 +31,6 @@ use OCP\SystemTag\ISystemTagManager;
use OCP\SystemTag\ISystemTagObjectMapper; use OCP\SystemTag\ISystemTagObjectMapper;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\IGroupManager; use OCP\IGroupManager;
use OCP\Files\IRootFolder;
/** /**
* Collection containing object ids by object type * Collection containing object ids by object type
@ -64,9 +63,9 @@ class SystemTagsObjectTypeCollection implements ICollection {
private $userSession; private $userSession;
/** /**
* @var IRootFolder * @var \Closure
**/ **/
protected $fileRoot; protected $childExistsFunction;
/** /**
* Constructor * Constructor
@ -76,7 +75,7 @@ class SystemTagsObjectTypeCollection implements ICollection {
* @param ISystemTagObjectMapper $tagMapper * @param ISystemTagObjectMapper $tagMapper
* @param IUserSession $userSession * @param IUserSession $userSession
* @param IGroupManager $groupManager * @param IGroupManager $groupManager
* @param IRootFolder $fileRoot * @param \Closure $childExistsFunction
*/ */
public function __construct( public function __construct(
$objectType, $objectType,
@ -84,19 +83,20 @@ class SystemTagsObjectTypeCollection implements ICollection {
ISystemTagObjectMapper $tagMapper, ISystemTagObjectMapper $tagMapper,
IUserSession $userSession, IUserSession $userSession,
IGroupManager $groupManager, IGroupManager $groupManager,
IRootFolder $fileRoot \Closure $childExistsFunction
) { ) {
$this->tagManager = $tagManager; $this->tagManager = $tagManager;
$this->tagMapper = $tagMapper; $this->tagMapper = $tagMapper;
$this->objectType = $objectType; $this->objectType = $objectType;
$this->userSession = $userSession; $this->userSession = $userSession;
$this->groupManager = $groupManager; $this->groupManager = $groupManager;
$this->fileRoot = $fileRoot; $this->childExistsFunction = $childExistsFunction;
} }
/** /**
* @param string $name * @param string $name
* @param resource|string $data Initial payload * @param resource|string $data Initial payload
* @return null|string
* @throws Forbidden * @throws Forbidden
*/ */
function createFile($name, $data = null) { function createFile($name, $data = null) {
@ -105,6 +105,7 @@ class SystemTagsObjectTypeCollection implements ICollection {
/** /**
* @param string $name * @param string $name
* @throws Forbidden
*/ */
function createDirectory($name) { function createDirectory($name) {
throw new Forbidden('Permission denied to create collections'); throw new Forbidden('Permission denied to create collections');
@ -112,6 +113,8 @@ class SystemTagsObjectTypeCollection implements ICollection {
/** /**
* @param string $objectId * @param string $objectId
* @return SystemTagsObjectMappingCollection
* @throws NotFound
*/ */
function getChild($objectId) { function getChild($objectId) {
// make sure the object exists and is reachable // make sure the object exists and is reachable
@ -133,17 +136,13 @@ class SystemTagsObjectTypeCollection implements ICollection {
} }
/** /**
* Checks if a child-node with the specified name exists
*
* @param string $name * @param string $name
* @return bool
*/ */
function childExists($name) { function childExists($name) {
// TODO: make this more abstract return call_user_func($this->childExistsFunction, $name);
if ($this->objectType === 'files') {
// make sure the object is reachable for the current user
$userId = $this->userSession->getUser()->getUID();
$nodes = $this->fileRoot->getUserFolder($userId)->getById(intval($name));
return !empty($nodes);
}
return true;
} }
function delete() { function delete() {
@ -156,6 +155,7 @@ class SystemTagsObjectTypeCollection implements ICollection {
/** /**
* @param string $name * @param string $name
* @throws Forbidden
*/ */
function setName($name) { function setName($name) {
throw new Forbidden('Permission denied to rename this collection'); throw new Forbidden('Permission denied to rename this collection');

View File

@ -25,11 +25,12 @@ namespace OCA\DAV\SystemTag;
use OCP\SystemTag\ISystemTagManager; use OCP\SystemTag\ISystemTagManager;
use OCP\SystemTag\ISystemTagObjectMapper; use OCP\SystemTag\ISystemTagObjectMapper;
use OCP\SystemTag\SystemTagsEntityEvent;
use Sabre\DAV\Exception\Forbidden; use Sabre\DAV\Exception\Forbidden;
use Sabre\DAV\SimpleCollection; use Sabre\DAV\SimpleCollection;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\IGroupManager; use OCP\IGroupManager;
use OCP\Files\IRootFolder; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class SystemTagsRelationsCollection extends SimpleCollection { class SystemTagsRelationsCollection extends SimpleCollection {
@ -40,14 +41,14 @@ class SystemTagsRelationsCollection extends SimpleCollection {
* @param ISystemTagObjectMapper $tagMapper * @param ISystemTagObjectMapper $tagMapper
* @param IUserSession $userSession * @param IUserSession $userSession
* @param IGroupManager $groupManager * @param IGroupManager $groupManager
* @param IRootFolder $fileRoot * @param EventDispatcherInterface $dispatcher
*/ */
public function __construct( public function __construct(
ISystemTagManager $tagManager, ISystemTagManager $tagManager,
ISystemTagObjectMapper $tagMapper, ISystemTagObjectMapper $tagMapper,
IUserSession $userSession, IUserSession $userSession,
IGroupManager $groupManager, IGroupManager $groupManager,
IRootFolder $fileRoot EventDispatcherInterface $dispatcher
) { ) {
$children = [ $children = [
new SystemTagsObjectTypeCollection( new SystemTagsObjectTypeCollection(
@ -56,10 +57,27 @@ class SystemTagsRelationsCollection extends SimpleCollection {
$tagMapper, $tagMapper,
$userSession, $userSession,
$groupManager, $groupManager,
$fileRoot function($name) {
$nodes = \OC::$server->getUserFolder()->getById(intval($name));
return !empty($nodes);
}
), ),
]; ];
$event = new SystemTagsEntityEvent(SystemTagsEntityEvent::EVENT_ENTITY);
$dispatcher->dispatch(SystemTagsEntityEvent::EVENT_ENTITY, $event);
foreach ($event->getEntityCollections() as $entity => $entityExistsFunction) {
$children[] = new SystemTagsObjectTypeCollection(
$entity,
$tagManager,
$tagMapper,
$userSession,
$groupManager,
$entityExistsFunction
);
}
parent::__construct('root', $children); parent::__construct('root', $children);
} }

View File

@ -140,7 +140,7 @@ class SystemTagMappingNodeTest extends \Test\TestCase {
} }
/** /**
* @expectedException Sabre\DAV\Exception\NotFound * @expectedException \Sabre\DAV\Exception\NotFound
*/ */
public function testDeleteTagNotFound() { public function testDeleteTagNotFound() {
// assuming the tag existed at the time the node was created, // assuming the tag existed at the time the node was created,

View File

@ -79,7 +79,7 @@ class SystemTagNodeTest extends \Test\TestCase {
} }
/** /**
* @expectedException Sabre\DAV\Exception\MethodNotAllowed * @expectedException \Sabre\DAV\Exception\MethodNotAllowed
*/ */
public function testSetName() { public function testSetName() {
$this->getTagNode()->setName('2'); $this->getTagNode()->setName('2');
@ -196,7 +196,7 @@ class SystemTagNodeTest extends \Test\TestCase {
} }
/** /**
* @expectedException Sabre\DAV\Exception\Conflict * @expectedException \Sabre\DAV\Exception\Conflict
*/ */
public function testUpdateTagAlreadyExists() { public function testUpdateTagAlreadyExists() {
$tag = new SystemTag(1, 'tag1', true, true); $tag = new SystemTag(1, 'tag1', true, true);
@ -216,7 +216,7 @@ class SystemTagNodeTest extends \Test\TestCase {
} }
/** /**
* @expectedException Sabre\DAV\Exception\NotFound * @expectedException \Sabre\DAV\Exception\NotFound
*/ */
public function testUpdateTagNotFound() { public function testUpdateTagNotFound() {
$tag = new SystemTag(1, 'tag1', true, true); $tag = new SystemTag(1, 'tag1', true, true);
@ -294,7 +294,7 @@ class SystemTagNodeTest extends \Test\TestCase {
} }
/** /**
* @expectedException Sabre\DAV\Exception\NotFound * @expectedException \Sabre\DAV\Exception\NotFound
*/ */
public function testDeleteTagNotFound() { public function testDeleteTagNotFound() {
$tag = new SystemTag(1, 'tag1', true, true); $tag = new SystemTag(1, 'tag1', true, true);

View File

@ -74,14 +74,14 @@ class SystemTagsByIdCollectionTest extends \Test\TestCase {
} }
/** /**
* @expectedException Sabre\DAV\Exception\Forbidden * @expectedException \Sabre\DAV\Exception\Forbidden
*/ */
public function testForbiddenCreateFile() { public function testForbiddenCreateFile() {
$this->getNode()->createFile('555'); $this->getNode()->createFile('555');
} }
/** /**
* @expectedException Sabre\DAV\Exception\Forbidden * @expectedException \Sabre\DAV\Exception\Forbidden
*/ */
public function testForbiddenCreateDirectory() { public function testForbiddenCreateDirectory() {
$this->getNode()->createDirectory('789'); $this->getNode()->createDirectory('789');
@ -107,7 +107,7 @@ class SystemTagsByIdCollectionTest extends \Test\TestCase {
} }
/** /**
* @expectedException Sabre\DAV\Exception\BadRequest * @expectedException \Sabre\DAV\Exception\BadRequest
*/ */
public function testGetChildInvalidName() { public function testGetChildInvalidName() {
$this->tagManager->expects($this->once()) $this->tagManager->expects($this->once())
@ -119,7 +119,7 @@ class SystemTagsByIdCollectionTest extends \Test\TestCase {
} }
/** /**
* @expectedException Sabre\DAV\Exception\NotFound * @expectedException \Sabre\DAV\Exception\NotFound
*/ */
public function testGetChildNotFound() { public function testGetChildNotFound() {
$this->tagManager->expects($this->once()) $this->tagManager->expects($this->once())
@ -131,7 +131,7 @@ class SystemTagsByIdCollectionTest extends \Test\TestCase {
} }
/** /**
* @expectedException Sabre\DAV\Exception\NotFound * @expectedException \Sabre\DAV\Exception\NotFound
*/ */
public function testGetChildUserNotVisible() { public function testGetChildUserNotVisible() {
$tag = new SystemTag(123, 'Test', false, false); $tag = new SystemTag(123, 'Test', false, false);
@ -225,7 +225,7 @@ class SystemTagsByIdCollectionTest extends \Test\TestCase {
} }
/** /**
* @expectedException Sabre\DAV\Exception\BadRequest * @expectedException \Sabre\DAV\Exception\BadRequest
*/ */
public function testChildExistsBadRequest() { public function testChildExistsBadRequest() {
$this->tagManager->expects($this->once()) $this->tagManager->expects($this->once())

View File

@ -128,7 +128,7 @@ class SystemTagsObjectMappingCollectionTest extends \Test\TestCase {
} }
/** /**
* @expectedException Sabre\DAV\Exception\PreconditionFailed * @expectedException \Sabre\DAV\Exception\PreconditionFailed
*/ */
public function testAssignTagNotFound() { public function testAssignTagNotFound() {
$this->tagManager->expects($this->once()) $this->tagManager->expects($this->once())
@ -140,7 +140,7 @@ class SystemTagsObjectMappingCollectionTest extends \Test\TestCase {
} }
/** /**
* @expectedException Sabre\DAV\Exception\Forbidden * @expectedException \Sabre\DAV\Exception\Forbidden
*/ */
public function testForbiddenCreateDirectory() { public function testForbiddenCreateDirectory() {
$this->getNode()->createDirectory('789'); $this->getNode()->createDirectory('789');
@ -193,7 +193,7 @@ class SystemTagsObjectMappingCollectionTest extends \Test\TestCase {
} }
/** /**
* @expectedException Sabre\DAV\Exception\NotFound * @expectedException \Sabre\DAV\Exception\NotFound
*/ */
public function testGetChildRelationNotFound() { public function testGetChildRelationNotFound() {
$this->tagMapper->expects($this->once()) $this->tagMapper->expects($this->once())
@ -205,7 +205,7 @@ class SystemTagsObjectMappingCollectionTest extends \Test\TestCase {
} }
/** /**
* @expectedException Sabre\DAV\Exception\BadRequest * @expectedException \Sabre\DAV\Exception\BadRequest
*/ */
public function testGetChildInvalidId() { public function testGetChildInvalidId() {
$this->tagMapper->expects($this->once()) $this->tagMapper->expects($this->once())
@ -217,7 +217,7 @@ class SystemTagsObjectMappingCollectionTest extends \Test\TestCase {
} }
/** /**
* @expectedException Sabre\DAV\Exception\NotFound * @expectedException \Sabre\DAV\Exception\NotFound
*/ */
public function testGetChildTagDoesNotExist() { public function testGetChildTagDoesNotExist() {
$this->tagMapper->expects($this->once()) $this->tagMapper->expects($this->once())
@ -321,7 +321,7 @@ class SystemTagsObjectMappingCollectionTest extends \Test\TestCase {
} }
/** /**
* @expectedException Sabre\DAV\Exception\BadRequest * @expectedException \Sabre\DAV\Exception\BadRequest
*/ */
public function testChildExistsInvalidId() { public function testChildExistsInvalidId() {
$this->tagMapper->expects($this->once()) $this->tagMapper->expects($this->once())
@ -333,14 +333,14 @@ class SystemTagsObjectMappingCollectionTest extends \Test\TestCase {
} }
/** /**
* @expectedException Sabre\DAV\Exception\Forbidden * @expectedException \Sabre\DAV\Exception\Forbidden
*/ */
public function testDelete() { public function testDelete() {
$this->getNode()->delete(); $this->getNode()->delete();
} }
/** /**
* @expectedException Sabre\DAV\Exception\Forbidden * @expectedException \Sabre\DAV\Exception\Forbidden
*/ */
public function testSetName() { public function testSetName() {
$this->getNode()->setName('somethingelse'); $this->getNode()->setName('somethingelse');

View File

@ -71,13 +71,12 @@ class SystemTagsObjectTypeCollectionTest extends \Test\TestCase {
$this->userFolder = $this->getMockBuilder('\OCP\Files\Folder') $this->userFolder = $this->getMockBuilder('\OCP\Files\Folder')
->getMock(); ->getMock();
$userFolder = $this->userFolder;
$fileRoot = $this->getMockBuilder('\OCP\Files\IRootFolder') $closure = function($name) use ($userFolder) {
->getMock(); $nodes = $userFolder->getById(intval($name));
$fileRoot->expects($this->any()) return !empty($nodes);
->method('getUserfolder') };
->with('testuser')
->will($this->returnValue($this->userFolder));
$this->node = new \OCA\DAV\SystemTag\SystemTagsObjectTypeCollection( $this->node = new \OCA\DAV\SystemTag\SystemTagsObjectTypeCollection(
'files', 'files',
@ -85,19 +84,19 @@ class SystemTagsObjectTypeCollectionTest extends \Test\TestCase {
$this->tagMapper, $this->tagMapper,
$userSession, $userSession,
$groupManager, $groupManager,
$fileRoot $closure
); );
} }
/** /**
* @expectedException Sabre\DAV\Exception\Forbidden * @expectedException \Sabre\DAV\Exception\Forbidden
*/ */
public function testForbiddenCreateFile() { public function testForbiddenCreateFile() {
$this->node->createFile('555'); $this->node->createFile('555');
} }
/** /**
* @expectedException Sabre\DAV\Exception\Forbidden * @expectedException \Sabre\DAV\Exception\Forbidden
*/ */
public function testForbiddenCreateDirectory() { public function testForbiddenCreateDirectory() {
$this->node->createDirectory('789'); $this->node->createDirectory('789');
@ -115,7 +114,7 @@ class SystemTagsObjectTypeCollectionTest extends \Test\TestCase {
} }
/** /**
* @expectedException Sabre\DAV\Exception\NotFound * @expectedException \Sabre\DAV\Exception\NotFound
*/ */
public function testGetChildWithoutAccess() { public function testGetChildWithoutAccess() {
$this->userFolder->expects($this->once()) $this->userFolder->expects($this->once())
@ -126,7 +125,7 @@ class SystemTagsObjectTypeCollectionTest extends \Test\TestCase {
} }
/** /**
* @expectedException Sabre\DAV\Exception\MethodNotAllowed * @expectedException \Sabre\DAV\Exception\MethodNotAllowed
*/ */
public function testGetChildren() { public function testGetChildren() {
$this->node->getChildren(); $this->node->getChildren();
@ -149,14 +148,14 @@ class SystemTagsObjectTypeCollectionTest extends \Test\TestCase {
} }
/** /**
* @expectedException Sabre\DAV\Exception\Forbidden * @expectedException \Sabre\DAV\Exception\Forbidden
*/ */
public function testDelete() { public function testDelete() {
$this->node->delete(); $this->node->delete();
} }
/** /**
* @expectedException Sabre\DAV\Exception\Forbidden * @expectedException \Sabre\DAV\Exception\Forbidden
*/ */
public function testSetName() { public function testSetName() {
$this->node->setName('somethingelse'); $this->node->setName('somethingelse');

View File

@ -0,0 +1,76 @@
<?php
/**
* @author Joas Schilling <coding@schilljs.com>
*
* @copyright Copyright (c) 2016, 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 OCP\SystemTag;
use Symfony\Component\EventDispatcher\Event;
/**
* Class SystemTagsEntityEvent
*
* @package OCP\SystemTag
* @since 9.1.0
*/
class SystemTagsEntityEvent extends Event {
const EVENT_ENTITY = 'OCP\SystemTag\ISystemTagManager::registerEntity';
/** @var string */
protected $event;
/** @var \Closure[] */
protected $collections;
/**
* SystemTagsEntityEvent constructor.
*
* @param string $event
* @since 9.1.0
*/
public function __construct($event) {
$this->event = $event;
$this->collections = [];
}
/**
* @param string $name
* @param \Closure $entityExistsFunction The closure should take one
* argument, which is the id of the entity, that tags
* should be handled for. The return should then be bool,
* depending on whether tags are allowed (true) or not.
* @throws \OutOfBoundsException when the entity name is already taken
* @since 9.1.0
*/
public function addEntityCollection($name, \Closure $entityExistsFunction) {
if (isset($this->collections[$name])) {
throw new \OutOfBoundsException('Duplicate entity name "' . $name . '"');
}
$this->collections[$name] = $entityExistsFunction;
}
/**
* @return \Closure[]
* @since 9.1.0
*/
public function getEntityCollections() {
return $this->collections;
}
}