Edit.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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\ISystemTagManager;
  26. use OCP\SystemTag\TagAlreadyExistsException;
  27. use OCP\SystemTag\TagNotFoundException;
  28. use Symfony\Component\Console\Input\InputInterface;
  29. use Symfony\Component\Console\Input\InputOption;
  30. use Symfony\Component\Console\Output\OutputInterface;
  31. class Edit extends Base {
  32. protected ISystemTagManager $systemTagManager;
  33. public function __construct(ISystemTagManager $systemTagManager) {
  34. $this->systemTagManager = $systemTagManager;
  35. parent::__construct();
  36. }
  37. protected function configure() {
  38. $this
  39. ->setName('tag:edit')
  40. ->setDescription('edit tag attributes')
  41. ->addArgument(
  42. 'id',
  43. InputOption::VALUE_REQUIRED,
  44. 'The ID of the tag that should be deleted',
  45. )
  46. ->addOption(
  47. 'name',
  48. null,
  49. InputOption::VALUE_OPTIONAL,
  50. 'sets the \'name\' parameter',
  51. )
  52. ->addOption(
  53. 'access',
  54. null,
  55. InputOption::VALUE_OPTIONAL,
  56. 'sets the access control level (public, restricted, invisible)',
  57. );
  58. }
  59. protected function execute(InputInterface $input, OutputInterface $output): int {
  60. $tagArray = $this->systemTagManager->getTagsByIds($input->getArgument('id'));
  61. // returns an array, but we always expect 0 or 1 results
  62. if (!$tagArray) {
  63. $output->writeln('<error>Tag not found</error>');
  64. return 3;
  65. }
  66. $tag = array_values($tagArray)[0];
  67. $name = $tag->getName();
  68. if (!empty($input->getOption('name'))) {
  69. $name = $input->getOption('name');
  70. }
  71. $userVisible = $tag->isUserVisible();
  72. $userAssignable = $tag->isUserAssignable();
  73. if ($input->getOption('access')) {
  74. switch ($input->getOption('access')) {
  75. case 'public':
  76. $userVisible = true;
  77. $userAssignable = true;
  78. break;
  79. case 'restricted':
  80. $userVisible = true;
  81. $userAssignable = false;
  82. break;
  83. case 'invisible':
  84. $userVisible = false;
  85. $userAssignable = false;
  86. break;
  87. default:
  88. $output->writeln('<error>`access` property is invalid</error>');
  89. return 1;
  90. }
  91. }
  92. try {
  93. $this->systemTagManager->updateTag($input->getArgument('id'), $name, $userVisible, $userAssignable);
  94. $output->writeln('<info>Tag updated ("' . $name . '", '. $userVisible . ', ' . $userAssignable . ')</info>');
  95. return 0;
  96. } catch (TagNotFoundException $e) {
  97. $output->writeln('<error>Tag not found</error>');
  98. return 1;
  99. } catch (TagAlreadyExistsException $e) {
  100. $output->writeln('<error>'.$e->getMessage().'</error>');
  101. return 2;
  102. }
  103. }
  104. }