Applicable.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Files_External\Command;
  26. use OC\Core\Command\Base;
  27. use OCA\Files_External\Lib\StorageConfig;
  28. use OCA\Files_External\NotFoundException;
  29. use OCA\Files_External\Service\GlobalStoragesService;
  30. use OCP\IGroupManager;
  31. use OCP\IUserManager;
  32. use Symfony\Component\Console\Input\InputArgument;
  33. use Symfony\Component\Console\Input\InputInterface;
  34. use Symfony\Component\Console\Input\InputOption;
  35. use Symfony\Component\Console\Output\OutputInterface;
  36. class Applicable extends Base {
  37. /**
  38. * @var GlobalStoragesService
  39. */
  40. protected $globalService;
  41. /**
  42. * @var IUserManager
  43. */
  44. private $userManager;
  45. /**
  46. * @var IGroupManager
  47. */
  48. private $groupManager;
  49. public function __construct(
  50. GlobalStoragesService $globalService,
  51. IUserManager $userManager,
  52. IGroupManager $groupManager
  53. ) {
  54. parent::__construct();
  55. $this->globalService = $globalService;
  56. $this->userManager = $userManager;
  57. $this->groupManager = $groupManager;
  58. }
  59. protected function configure() {
  60. $this
  61. ->setName('files_external:applicable')
  62. ->setDescription('Manage applicable users and groups for a mount')
  63. ->addArgument(
  64. 'mount_id',
  65. InputArgument::REQUIRED,
  66. 'The id of the mount to edit'
  67. )->addOption(
  68. 'add-user',
  69. '',
  70. InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
  71. 'user to add as applicable'
  72. )->addOption(
  73. 'remove-user',
  74. '',
  75. InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
  76. 'user to remove as applicable'
  77. )->addOption(
  78. 'add-group',
  79. '',
  80. InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
  81. 'group to add as applicable'
  82. )->addOption(
  83. 'remove-group',
  84. '',
  85. InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
  86. 'group to remove as applicable'
  87. )->addOption(
  88. 'remove-all',
  89. '',
  90. InputOption::VALUE_NONE,
  91. 'Set the mount to be globally applicable'
  92. );
  93. parent::configure();
  94. }
  95. protected function execute(InputInterface $input, OutputInterface $output): int {
  96. $mountId = $input->getArgument('mount_id');
  97. try {
  98. $mount = $this->globalService->getStorage($mountId);
  99. } catch (NotFoundException $e) {
  100. $output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts</error>');
  101. return 404;
  102. }
  103. if ($mount->getType() === StorageConfig::MOUNT_TYPE_PERSONAl) {
  104. $output->writeln('<error>Can\'t change applicables on personal mounts</error>');
  105. return 1;
  106. }
  107. $addUsers = $input->getOption('add-user');
  108. $removeUsers = $input->getOption('remove-user');
  109. $addGroups = $input->getOption('add-group');
  110. $removeGroups = $input->getOption('remove-group');
  111. $applicableUsers = $mount->getApplicableUsers();
  112. $applicableGroups = $mount->getApplicableGroups();
  113. if ((count($addUsers) + count($removeUsers) + count($addGroups) + count($removeGroups) > 0) || $input->getOption('remove-all')) {
  114. foreach ($addUsers as $addUser) {
  115. if (!$this->userManager->userExists($addUser)) {
  116. $output->writeln('<error>User "' . $addUser . '" not found</error>');
  117. return 404;
  118. }
  119. }
  120. foreach ($addGroups as $addGroup) {
  121. if (!$this->groupManager->groupExists($addGroup)) {
  122. $output->writeln('<error>Group "' . $addGroup . '" not found</error>');
  123. return 404;
  124. }
  125. }
  126. if ($input->getOption('remove-all')) {
  127. $applicableUsers = [];
  128. $applicableGroups = [];
  129. } else {
  130. $applicableUsers = array_unique(array_merge($applicableUsers, $addUsers));
  131. $applicableUsers = array_values(array_diff($applicableUsers, $removeUsers));
  132. $applicableGroups = array_unique(array_merge($applicableGroups, $addGroups));
  133. $applicableGroups = array_values(array_diff($applicableGroups, $removeGroups));
  134. }
  135. $mount->setApplicableUsers($applicableUsers);
  136. $mount->setApplicableGroups($applicableGroups);
  137. $this->globalService->updateStorage($mount);
  138. }
  139. $this->writeArrayInOutputFormat($input, $output, [
  140. 'users' => $applicableUsers,
  141. 'groups' => $applicableGroups
  142. ]);
  143. return 0;
  144. }
  145. }