Skip to content

Commit b93b4fb

Browse files
author
Johannes Leuker
committed
Add commands to manage tags via OCC
list, add, delete, edit Signed-off-by: Johannes Leuker <j.leuker@hosting.de>
1 parent f031dd6 commit b93b4fb

File tree

11 files changed

+949
-0
lines changed

11 files changed

+949
-0
lines changed

core/Command/SystemTag/Add.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2021, hosting.de, Johannes Leuker <developers@hosting.de>
4+
*
5+
* @author Johannes Leuker <developers@hosting.de>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
namespace OC\Core\Command\SystemTag;
25+
26+
use OC\Core\Command\Base;
27+
use OCP\SystemTag\ISystemTag;
28+
use OCP\SystemTag\ISystemTagManager;
29+
use OCP\SystemTag\TagAlreadyExistsException;
30+
use Symfony\Component\Console\Input\InputArgument;
31+
use Symfony\Component\Console\Input\InputInterface;
32+
use Symfony\Component\Console\Output\OutputInterface;
33+
34+
class Add extends Base {
35+
36+
/** @var ISystemTagManager */
37+
protected $systemTagManager;
38+
39+
public function __construct(ISystemTagManager $systemTagManager) {
40+
$this->systemTagManager = $systemTagManager;
41+
parent::__construct();
42+
}
43+
44+
protected function configure() {
45+
$this
46+
->setName('tag:add')
47+
->setDescription('Add new tag')
48+
->addArgument(
49+
'name',
50+
InputArgument::REQUIRED,
51+
'name of the tag',
52+
)
53+
->addArgument(
54+
'access',
55+
InputArgument::REQUIRED,
56+
'access level of the tag (public, restricted or invisible)',
57+
);
58+
parent::configure();
59+
}
60+
61+
protected function execute(InputInterface $input, OutputInterface $output): int {
62+
$name = $input->getArgument('name');
63+
if ($name === '') {
64+
$output->writeln('<error>`name` can\'t be empty</error>');
65+
return 3;
66+
}
67+
68+
switch ($input->getArgument('access')) {
69+
case 'public':
70+
$userVisible = true;
71+
$userAssignable = true;
72+
break;
73+
case 'restricted':
74+
$userVisible = true;
75+
$userAssignable = false;
76+
break;
77+
case 'invisible':
78+
$userVisible = false;
79+
$userAssignable = false;
80+
break;
81+
default:
82+
$output->writeln('<error>`access` property is invalid</error>');
83+
return 1;
84+
}
85+
86+
try {
87+
$tag = $this->systemTagManager->createTag($name, $userVisible, $userAssignable);
88+
89+
$this->writeArrayInOutputFormat($input, $output,
90+
[
91+
'id' => $tag->getId(),
92+
'name' => $tag->getName(),
93+
'access' => ISystemTag::ACCESS_LEVEL_LOOKUP[$tag->getAccessLevel()],
94+
]);
95+
return 0;
96+
} catch (TagAlreadyExistsException $e) {
97+
$output->writeln('<error>'.$e->getMessage().'</error>');
98+
return 2;
99+
}
100+
}
101+
}

core/Command/SystemTag/Delete.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2021, hosting.de, Johannes Leuker <developers@hosting.de>
4+
*
5+
* @author Johannes Leuker <developers@hosting.de>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
namespace OC\Core\Command\SystemTag;
25+
26+
use OC\Core\Command\Base;
27+
use OCP\SystemTag\ISystemTagManager;
28+
use OCP\SystemTag\TagNotFoundException;
29+
use Symfony\Component\Console\Input\InputInterface;
30+
use Symfony\Component\Console\Input\InputOption;
31+
use Symfony\Component\Console\Output\OutputInterface;
32+
33+
class Delete extends Base {
34+
35+
/** @var ISystemTagManager */
36+
protected $systemTagManager;
37+
38+
public function __construct(ISystemTagManager $systemTagManager) {
39+
$this->systemTagManager = $systemTagManager;
40+
parent::__construct();
41+
}
42+
43+
protected function configure() {
44+
$this
45+
->setName('tag:delete')
46+
->setDescription('delete a tag')
47+
->addArgument(
48+
'id',
49+
InputOption::VALUE_REQUIRED,
50+
'The ID of the tag that should be deleted',
51+
);
52+
}
53+
54+
protected function execute(InputInterface $input, OutputInterface $output): int {
55+
try {
56+
$this->systemTagManager->deleteTags($input->getArgument('id'));
57+
$output->writeln('<info>The specified tag was deleted</info>');
58+
return 0;
59+
} catch (TagNotFoundException $e) {
60+
$output->writeln('<error>Tag not found</error>');
61+
return 1;
62+
}
63+
}
64+
}

core/Command/SystemTag/Edit.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2021, hosting.de, Johannes Leuker <developers@hosting.de>
4+
*
5+
* @author Johannes Leuker <developers@hosting.de>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
namespace OC\Core\Command\SystemTag;
25+
26+
use OC\Core\Command\Base;
27+
use OCP\SystemTag\ISystemTagManager;
28+
use OCP\SystemTag\TagAlreadyExistsException;
29+
use OCP\SystemTag\TagNotFoundException;
30+
use Symfony\Component\Console\Input\InputInterface;
31+
use Symfony\Component\Console\Input\InputOption;
32+
use Symfony\Component\Console\Output\OutputInterface;
33+
34+
class Edit extends Base {
35+
36+
/** @var ISystemTagManager */
37+
protected $systemTagManager;
38+
39+
/**
40+
* @param ISystemTagManager $systemTagManager
41+
*/
42+
public function __construct(ISystemTagManager $systemTagManager) {
43+
$this->systemTagManager = $systemTagManager;
44+
parent::__construct();
45+
}
46+
47+
protected function configure() {
48+
$this
49+
->setName('tag:edit')
50+
->setDescription('edit tag attributes')
51+
->addArgument(
52+
'id',
53+
InputOption::VALUE_REQUIRED,
54+
'The ID of the tag that should be deleted',
55+
)
56+
->addOption(
57+
'name',
58+
null,
59+
InputOption::VALUE_OPTIONAL,
60+
'sets the \'name\' parameter',
61+
)
62+
->addOption(
63+
'access',
64+
null,
65+
InputOption::VALUE_OPTIONAL,
66+
'sets the access control level (public, restricted, invisible)',
67+
);
68+
}
69+
70+
protected function execute(InputInterface $input, OutputInterface $output): int {
71+
$tagArray = $this->systemTagManager->getTagsByIds($input->getArgument('id'));
72+
// returns an array, but we always expect 0 or 1 results
73+
74+
if (!$tagArray) {
75+
$output->writeln('<error>Tag not found</error>');
76+
return 3;
77+
} else {
78+
$tag = array_values($tagArray)[0];
79+
$name = $tag->getName();
80+
if ($input->getOption('name') !== null && $input->getOption('name') !== '') {
81+
$name = $input->getOption('name');
82+
}
83+
84+
$userVisible = $tag->isUserVisible();
85+
$userAssignable = $tag->isUserAssignable();
86+
if ($input->getOption('access')) {
87+
switch ($input->getOption('access')) {
88+
case 'public':
89+
$userVisible = true;
90+
$userAssignable = true;
91+
break;
92+
case 'restricted':
93+
$userVisible = true;
94+
$userAssignable = false;
95+
break;
96+
case 'invisible':
97+
$userVisible = false;
98+
$userAssignable = false;
99+
break;
100+
default:
101+
$output->writeln('<error>`access` property is invalid</error>');
102+
return 1;
103+
}
104+
}
105+
106+
try {
107+
$this->systemTagManager->updateTag($input->getArgument('id'), $name, $userVisible, $userAssignable);
108+
$output->writeln('<info>Tag updated ("' . $name . '", '. $userVisible . ', ' . $userAssignable . ')</info>');
109+
return 0;
110+
} catch (TagNotFoundException $e) {
111+
$output->writeln('<error>Tag not found</error>');
112+
return 1;
113+
} catch (TagAlreadyExistsException $e) {
114+
$output->writeln('<error>'.$e->getMessage().'</error>');
115+
return 2;
116+
}
117+
}
118+
}
119+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2021, hosting.de, Johannes Leuker <developers@hosting.de>
4+
*
5+
* @author Johannes Leuker <developers@hosting.de>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
namespace OC\Core\Command\SystemTag;
25+
26+
use OC\Core\Command\Base;
27+
use OCP\SystemTag\ISystemTagManager;
28+
use OCP\SystemTag\ISystemTag;
29+
use Symfony\Component\Console\Input\InputInterface;
30+
use Symfony\Component\Console\Input\InputOption;
31+
use Symfony\Component\Console\Output\OutputInterface;
32+
33+
class ListCommand extends Base {
34+
35+
/** @var ISystemTagManager */
36+
protected $systemTagManager;
37+
38+
public function __construct(ISystemTagManager $systemTagManager) {
39+
$this->systemTagManager = $systemTagManager;
40+
parent::__construct();
41+
}
42+
43+
protected function configure() {
44+
$this
45+
->setName('tag:list')
46+
->setDescription('list tags')
47+
->addOption(
48+
'visibilityFilter',
49+
null,
50+
InputOption::VALUE_OPTIONAL,
51+
'filter by visibility (1,0)'
52+
)
53+
->addOption(
54+
'nameSearchPattern',
55+
null,
56+
InputOption::VALUE_OPTIONAL,
57+
'optional search pattern for the tag name (infix)'
58+
);
59+
parent::configure();
60+
}
61+
62+
protected function execute(InputInterface $input, OutputInterface $output): int {
63+
$tags = $this->systemTagManager->getAllTags(
64+
$input->getOption('visibilityFilter'),
65+
$input->getOption('nameSearchPattern')
66+
);
67+
68+
$this->writeArrayInOutputFormat($input, $output, $this->formatTags($tags));
69+
return 0;
70+
}
71+
72+
/**
73+
* @param ISystemtag[] $tags
74+
* @return array
75+
*/
76+
private function formatTags(array $tags) {
77+
foreach ($tags as $tag) {
78+
$result[$tag->getId()] = [
79+
'name' => $tag->getName(),
80+
'access' => ISystemTag::ACCESS_LEVEL_LOOKUP[$tag->getAccessLevel()],
81+
];
82+
}
83+
return $result;
84+
}
85+
}

core/register_command.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,11 @@
190190
$application->add(new OC\Core\Command\Group\RemoveUser(\OC::$server->getUserManager(), \OC::$server->getGroupManager()));
191191
$application->add(new OC\Core\Command\Group\Info(\OC::$server->get(\OCP\IGroupManager::class)));
192192

193+
$application->add(new OC\Core\Command\SystemTag\ListCommand(\OC::$server->get(\OCP\SystemTag\ISystemTagManager::class)));
194+
$application->add(new OC\Core\Command\SystemTag\Delete(\OC::$server->get(\OCP\SystemTag\ISystemTagManager::class)));
195+
$application->add(new OC\Core\Command\SystemTag\Add(\OC::$server->get(\OCP\SystemTag\ISystemTagManager::class)));
196+
$application->add(new OC\Core\Command\SystemTag\Edit(\OC::$server->get(\OCP\SystemTag\ISystemTagManager::class)));
197+
193198
$application->add(new OC\Core\Command\Security\ListCertificates(\OC::$server->getCertificateManager(), \OC::$server->getL10N('core')));
194199
$application->add(new OC\Core\Command\Security\ImportCertificate(\OC::$server->getCertificateManager()));
195200
$application->add(new OC\Core\Command\Security\RemoveCertificate(\OC::$server->getCertificateManager()));

0 commit comments

Comments
 (0)