nextcloud/apps/dav/lib/SystemTag/SystemTagsObjectMappingColl...

212 lines
5.3 KiB
PHP
Raw Normal View History

2015-11-27 14:54:31 +03:00
<?php
/**
2016-07-21 17:49:16 +03:00
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Joas Schilling <coding@schilljs.com>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Vincent Petry <vincent@nextcloud.com>
2015-11-27 14:54:31 +03:00
*
* @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/>
2015-11-27 14:54:31 +03:00
*
*/
namespace OCA\DAV\SystemTag;
use OCP\IUser;
use OCP\SystemTag\ISystemTag;
use OCP\SystemTag\ISystemTagManager;
use OCP\SystemTag\ISystemTagObjectMapper;
use OCP\SystemTag\TagNotFoundException;
use Sabre\DAV\Exception\BadRequest;
2015-11-27 14:54:31 +03:00
use Sabre\DAV\Exception\Forbidden;
use Sabre\DAV\Exception\NotFound;
2015-12-03 13:26:16 +03:00
use Sabre\DAV\Exception\PreconditionFailed;
2015-11-27 14:54:31 +03:00
use Sabre\DAV\ICollection;
/**
* 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;
/**
* User
*
* @var IUser
*/
private $user;
2015-11-27 14:54:31 +03:00
/**
* Constructor
*
* @param string $objectId object id
* @param string $objectType object type
* @param IUser $user user
* @param ISystemTagManager $tagManager tag manager
* @param ISystemTagObjectMapper $tagMapper tag mapper
2015-11-27 14:54:31 +03:00
*/
public function __construct(
$objectId,
$objectType,
IUser $user,
ISystemTagManager $tagManager,
ISystemTagObjectMapper $tagMapper
) {
2015-11-27 14:54:31 +03:00
$this->tagManager = $tagManager;
$this->tagMapper = $tagMapper;
$this->objectId = $objectId;
$this->objectType = $objectType;
$this->user = $user;
2015-11-27 14:54:31 +03:00
}
public function createFile($name, $data = null) {
$tagId = $name;
2015-11-27 14:54:31 +03:00
try {
$tags = $this->tagManager->getTagsByIds([$tagId]);
$tag = current($tags);
if (!$this->tagManager->canUserSeeTag($tag, $this->user)) {
throw new PreconditionFailed('Tag with id ' . $tagId . ' does not exist, cannot assign');
}
if (!$this->tagManager->canUserAssignTag($tag, $this->user)) {
throw new Forbidden('No permission to assign tag ' . $tagId);
}
2015-11-27 14:54:31 +03:00
$this->tagMapper->assignTags($this->objectId, $this->objectType, $tagId);
} catch (TagNotFoundException $e) {
2015-12-03 13:26:16 +03:00
throw new PreconditionFailed('Tag with id ' . $tagId . ' does not exist, cannot assign');
2015-11-27 14:54:31 +03:00
}
}
public function createDirectory($name) {
2015-11-27 14:54:31 +03:00
throw new Forbidden('Permission denied to create collections');
}
public function getChild($tagId) {
2015-11-27 14:54:31 +03:00
try {
2015-12-04 19:30:22 +03:00
if ($this->tagMapper->haveTag([$this->objectId], $this->objectType, $tagId, true)) {
$tag = $this->tagManager->getTagsByIds([$tagId]);
$tag = current($tag);
if ($this->tagManager->canUserSeeTag($tag, $this->user)) {
return $this->makeNode($tag);
}
2015-11-27 14:54:31 +03:00
}
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);
}
}
public function getChildren() {
2015-12-04 19:30:22 +03:00
$tagIds = current($this->tagMapper->getTagIdsForObjects([$this->objectId], $this->objectType));
2015-11-27 14:54:31 +03:00
if (empty($tagIds)) {
return [];
}
2015-12-04 19:30:22 +03:00
$tags = $this->tagManager->getTagsByIds($tagIds);
// filter out non-visible tags
$tags = array_filter($tags, function ($tag) {
return $this->tagManager->canUserSeeTag($tag, $this->user);
});
return array_values(array_map(function ($tag) {
2015-11-27 14:54:31 +03:00
return $this->makeNode($tag);
2015-12-03 13:26:16 +03:00
}, $tags));
2015-11-27 14:54:31 +03:00
}
public function childExists($tagId) {
2015-11-27 14:54:31 +03:00
try {
$result = $this->tagMapper->haveTag([$this->objectId], $this->objectType, $tagId, true);
if ($result) {
$tags = $this->tagManager->getTagsByIds([$tagId]);
$tag = current($tags);
if (!$this->tagManager->canUserSeeTag($tag, $this->user)) {
return false;
}
}
return $result;
2015-11-27 14:54:31 +03:00
} catch (\InvalidArgumentException $e) {
throw new BadRequest('Invalid tag id', 0, $e);
} catch (TagNotFoundException $e) {
2015-12-03 13:26:16 +03:00
return false;
2015-11-27 14:54:31 +03:00
}
}
public function delete() {
2015-11-27 14:54:31 +03:00
throw new Forbidden('Permission denied to delete this collection');
}
public function getName() {
2015-11-27 14:54:31 +03:00
return $this->objectId;
}
public function setName($name) {
2015-11-27 14:54:31 +03:00
throw new Forbidden('Permission denied to rename this collection');
}
/**
* Returns the last modification time, as a unix timestamp
*
* @return int
*/
public function getLastModified() {
2015-11-27 14:54:31 +03:00
return null;
}
/**
* Create a sabre node for the mapping of the
2015-12-04 19:30:22 +03:00
* given system tag to the collection's object
2015-11-27 14:54:31 +03:00
*
* @param ISystemTag $tag
*
* @return SystemTagMappingNode
2015-11-27 14:54:31 +03:00
*/
private function makeNode(ISystemTag $tag) {
return new SystemTagMappingNode(
$tag,
$this->objectId,
$this->objectType,
$this->user,
2015-11-27 14:54:31 +03:00
$this->tagManager,
$this->tagMapper
);
}
}