Applicable.php 4.7 KB

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