Applicable.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. use Symfony\Component\HttpFoundation\Response;
  37. class Applicable extends Base {
  38. public function __construct(
  39. protected GlobalStoragesService $globalService,
  40. private IUserManager $userManager,
  41. private IGroupManager $groupManager,
  42. ) {
  43. parent::__construct();
  44. }
  45. protected function configure(): void {
  46. $this
  47. ->setName('files_external:applicable')
  48. ->setDescription('Manage applicable users and groups for a mount')
  49. ->addArgument(
  50. 'mount_id',
  51. InputArgument::REQUIRED,
  52. 'The id of the mount to edit'
  53. )->addOption(
  54. 'add-user',
  55. '',
  56. InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
  57. 'user to add as applicable'
  58. )->addOption(
  59. 'remove-user',
  60. '',
  61. InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
  62. 'user to remove as applicable'
  63. )->addOption(
  64. 'add-group',
  65. '',
  66. InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
  67. 'group to add as applicable'
  68. )->addOption(
  69. 'remove-group',
  70. '',
  71. InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
  72. 'group to remove as applicable'
  73. )->addOption(
  74. 'remove-all',
  75. '',
  76. InputOption::VALUE_NONE,
  77. 'Set the mount to be globally applicable'
  78. );
  79. parent::configure();
  80. }
  81. protected function execute(InputInterface $input, OutputInterface $output): int {
  82. $mountId = $input->getArgument('mount_id');
  83. try {
  84. $mount = $this->globalService->getStorage($mountId);
  85. } catch (NotFoundException $e) {
  86. $output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts</error>');
  87. return Response::HTTP_NOT_FOUND;
  88. }
  89. if ($mount->getType() === StorageConfig::MOUNT_TYPE_PERSONAL) {
  90. $output->writeln('<error>Can\'t change applicables on personal mounts</error>');
  91. return self::FAILURE;
  92. }
  93. $addUsers = $input->getOption('add-user');
  94. $removeUsers = $input->getOption('remove-user');
  95. $addGroups = $input->getOption('add-group');
  96. $removeGroups = $input->getOption('remove-group');
  97. $applicableUsers = $mount->getApplicableUsers();
  98. $applicableGroups = $mount->getApplicableGroups();
  99. if ((count($addUsers) + count($removeUsers) + count($addGroups) + count($removeGroups) > 0) || $input->getOption('remove-all')) {
  100. foreach ($addUsers as $addUser) {
  101. if (!$this->userManager->userExists($addUser)) {
  102. $output->writeln('<error>User "' . $addUser . '" not found</error>');
  103. return Response::HTTP_NOT_FOUND;
  104. }
  105. }
  106. foreach ($addGroups as $addGroup) {
  107. if (!$this->groupManager->groupExists($addGroup)) {
  108. $output->writeln('<error>Group "' . $addGroup . '" not found</error>');
  109. return Response::HTTP_NOT_FOUND;
  110. }
  111. }
  112. if ($input->getOption('remove-all')) {
  113. $applicableUsers = [];
  114. $applicableGroups = [];
  115. } else {
  116. $applicableUsers = array_unique(array_merge($applicableUsers, $addUsers));
  117. $applicableUsers = array_values(array_diff($applicableUsers, $removeUsers));
  118. $applicableGroups = array_unique(array_merge($applicableGroups, $addGroups));
  119. $applicableGroups = array_values(array_diff($applicableGroups, $removeGroups));
  120. }
  121. $mount->setApplicableUsers($applicableUsers);
  122. $mount->setApplicableGroups($applicableGroups);
  123. $this->globalService->updateStorage($mount);
  124. }
  125. $this->writeArrayInOutputFormat($input, $output, [
  126. 'users' => $applicableUsers,
  127. 'groups' => $applicableGroups
  128. ]);
  129. return self::SUCCESS;
  130. }
  131. }