1
0

Applicable.php 4.7 KB

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