Add.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021, hosting.de, Johannes Leuker <developers@hosting.de>
  4. *
  5. * @author Johannes Leuker <j.leuker@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. namespace OC\Core\Command\SystemTag;
  24. use OC\Core\Command\Base;
  25. use OCP\SystemTag\ISystemTag;
  26. use OCP\SystemTag\ISystemTagManager;
  27. use OCP\SystemTag\TagAlreadyExistsException;
  28. use Symfony\Component\Console\Input\InputArgument;
  29. use Symfony\Component\Console\Input\InputInterface;
  30. use Symfony\Component\Console\Output\OutputInterface;
  31. class Add extends Base {
  32. public function __construct(
  33. protected ISystemTagManager $systemTagManager,
  34. ) {
  35. parent::__construct();
  36. }
  37. protected function configure() {
  38. $this
  39. ->setName('tag:add')
  40. ->setDescription('Add new tag')
  41. ->addArgument(
  42. 'name',
  43. InputArgument::REQUIRED,
  44. 'name of the tag',
  45. )
  46. ->addArgument(
  47. 'access',
  48. InputArgument::REQUIRED,
  49. 'access level of the tag (public, restricted or invisible)',
  50. );
  51. parent::configure();
  52. }
  53. protected function execute(InputInterface $input, OutputInterface $output): int {
  54. $name = $input->getArgument('name');
  55. if ($name === '') {
  56. $output->writeln('<error>`name` can\'t be empty</error>');
  57. return 3;
  58. }
  59. switch ($input->getArgument('access')) {
  60. case 'public':
  61. $userVisible = true;
  62. $userAssignable = true;
  63. break;
  64. case 'restricted':
  65. $userVisible = true;
  66. $userAssignable = false;
  67. break;
  68. case 'invisible':
  69. $userVisible = false;
  70. $userAssignable = false;
  71. break;
  72. default:
  73. $output->writeln('<error>`access` property is invalid</error>');
  74. return 1;
  75. }
  76. try {
  77. $tag = $this->systemTagManager->createTag($name, $userVisible, $userAssignable);
  78. $this->writeArrayInOutputFormat($input, $output,
  79. [
  80. 'id' => $tag->getId(),
  81. 'name' => $tag->getName(),
  82. 'access' => ISystemTag::ACCESS_LEVEL_LOOKUP[$tag->getAccessLevel()],
  83. ]);
  84. return 0;
  85. } catch (TagAlreadyExistsException $e) {
  86. $output->writeln('<error>'.$e->getMessage().'</error>');
  87. return 2;
  88. }
  89. }
  90. }