2016-07-27 16:37:03 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.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\WorkflowEngine\Check;
|
|
|
|
|
2020-06-15 09:23:04 +03:00
|
|
|
use OCA\Files_Sharing\SharedStorage;
|
2019-09-05 16:52:11 +03:00
|
|
|
use OCA\WorkflowEngine\Entity\File;
|
2016-07-27 16:37:03 +03:00
|
|
|
use OCP\Files\Cache\ICache;
|
2017-03-09 13:58:26 +03:00
|
|
|
use OCP\Files\IHomeStorage;
|
2016-08-01 18:56:33 +03:00
|
|
|
use OCP\IL10N;
|
2016-07-27 16:37:03 +03:00
|
|
|
use OCP\SystemTag\ISystemTagManager;
|
|
|
|
use OCP\SystemTag\ISystemTagObjectMapper;
|
|
|
|
use OCP\SystemTag\TagNotFoundException;
|
|
|
|
use OCP\WorkflowEngine\ICheck;
|
2019-09-09 15:27:10 +03:00
|
|
|
use OCP\WorkflowEngine\IFileCheck;
|
2016-07-27 16:37:03 +03:00
|
|
|
|
2019-09-09 15:27:10 +03:00
|
|
|
class FileSystemTags implements ICheck, IFileCheck {
|
2019-09-09 17:04:12 +03:00
|
|
|
use TFileCheck;
|
2016-07-27 16:37:03 +03:00
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
protected $fileIds;
|
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
protected $fileSystemTags;
|
|
|
|
|
2016-08-01 18:56:33 +03:00
|
|
|
/** @var IL10N */
|
|
|
|
protected $l;
|
|
|
|
|
2016-07-27 16:37:03 +03:00
|
|
|
/** @var ISystemTagManager */
|
|
|
|
protected $systemTagManager;
|
|
|
|
|
|
|
|
/** @var ISystemTagObjectMapper */
|
|
|
|
protected $systemTagObjectMapper;
|
|
|
|
|
|
|
|
/**
|
2016-08-01 18:56:33 +03:00
|
|
|
* @param IL10N $l
|
2016-07-27 16:37:03 +03:00
|
|
|
* @param ISystemTagManager $systemTagManager
|
|
|
|
* @param ISystemTagObjectMapper $systemTagObjectMapper
|
|
|
|
*/
|
2016-08-01 18:56:33 +03:00
|
|
|
public function __construct(IL10N $l, ISystemTagManager $systemTagManager, ISystemTagObjectMapper $systemTagObjectMapper) {
|
|
|
|
$this->l = $l;
|
2016-07-27 16:37:03 +03:00
|
|
|
$this->systemTagManager = $systemTagManager;
|
|
|
|
$this->systemTagObjectMapper = $systemTagObjectMapper;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $operator
|
|
|
|
* @param string $value
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function executeCheck($operator, $value) {
|
|
|
|
$systemTags = $this->getSystemTags();
|
|
|
|
return ($operator === 'is') === in_array($value, $systemTags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $operator
|
|
|
|
* @param string $value
|
|
|
|
* @throws \UnexpectedValueException
|
|
|
|
*/
|
|
|
|
public function validateCheck($operator, $value) {
|
|
|
|
if (!in_array($operator, ['is', '!is'])) {
|
2016-08-01 18:56:33 +03:00
|
|
|
throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1);
|
2016-07-27 16:37:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->systemTagManager->getTagsByIds($value);
|
|
|
|
} catch (TagNotFoundException $e) {
|
2016-08-01 18:56:33 +03:00
|
|
|
throw new \UnexpectedValueException($this->l->t('The given tag id is invalid'), 2);
|
2016-07-27 16:37:03 +03:00
|
|
|
} catch (\InvalidArgumentException $e) {
|
2016-08-01 18:56:33 +03:00
|
|
|
throw new \UnexpectedValueException($this->l->t('The given tag id is invalid'), 3);
|
2016-07-27 16:37:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the ids of the assigned system tags
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
protected function getSystemTags() {
|
|
|
|
$cache = $this->storage->getCache();
|
2020-06-15 09:23:04 +03:00
|
|
|
$fileIds = $this->getFileIds($cache, $this->path, !$this->storage->instanceOfStorage(IHomeStorage::class) || $this->storage->instanceOfStorage(SharedStorage::class));
|
2016-07-27 16:37:03 +03:00
|
|
|
|
|
|
|
$systemTags = [];
|
|
|
|
foreach ($fileIds as $i => $fileId) {
|
|
|
|
if (isset($this->fileSystemTags[$fileId])) {
|
|
|
|
$systemTags[] = $this->fileSystemTags[$fileId];
|
|
|
|
unset($fileIds[$i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($fileIds)) {
|
|
|
|
$mappedSystemTags = $this->systemTagObjectMapper->getTagIdsForObjects($fileIds, 'files');
|
|
|
|
foreach ($mappedSystemTags as $fileId => $fileSystemTags) {
|
|
|
|
$this->fileSystemTags[$fileId] = $fileSystemTags;
|
|
|
|
$systemTags[] = $fileSystemTags;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$systemTags = call_user_func_array('array_merge', $systemTags);
|
|
|
|
$systemTags = array_unique($systemTags);
|
|
|
|
return $systemTags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the file ids of the given path and its parents
|
|
|
|
* @param ICache $cache
|
|
|
|
* @param string $path
|
2017-03-09 13:58:26 +03:00
|
|
|
* @param bool $isExternalStorage
|
2016-07-27 16:37:03 +03:00
|
|
|
* @return int[]
|
|
|
|
*/
|
2017-03-09 13:58:26 +03:00
|
|
|
protected function getFileIds(ICache $cache, $path, $isExternalStorage) {
|
2016-07-27 16:37:03 +03:00
|
|
|
$cacheId = $cache->getNumericStorageId();
|
|
|
|
if (isset($this->fileIds[$cacheId][$path])) {
|
|
|
|
return $this->fileIds[$cacheId][$path];
|
|
|
|
}
|
|
|
|
|
2017-03-09 13:58:26 +03:00
|
|
|
$parentIds = [];
|
|
|
|
if ($path !== $this->dirname($path)) {
|
|
|
|
$parentIds = $this->getFileIds($cache, $this->dirname($path), $isExternalStorage);
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif (!$isExternalStorage) {
|
2016-07-27 16:37:03 +03:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$fileId = $cache->getId($path);
|
|
|
|
if ($fileId !== -1) {
|
|
|
|
$parentIds[] = $cache->getId($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->fileIds[$cacheId][$path] = $parentIds;
|
|
|
|
|
|
|
|
return $parentIds;
|
|
|
|
}
|
2017-03-09 13:58:26 +03:00
|
|
|
|
|
|
|
protected function dirname($path) {
|
|
|
|
$dir = dirname($path);
|
|
|
|
return $dir === '.' ? '' : $dir;
|
|
|
|
}
|
2019-09-05 16:52:11 +03:00
|
|
|
|
|
|
|
public function supportedEntities(): array {
|
|
|
|
return [ File::class ];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isAvailableForScope(int $scope): bool {
|
|
|
|
return true;
|
|
|
|
}
|
2016-07-27 16:37:03 +03:00
|
|
|
}
|