1
0

Applicable.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Command;
  8. use OC\Core\Command\Base;
  9. use OCA\Files_External\Lib\StorageConfig;
  10. use OCA\Files_External\NotFoundException;
  11. use OCA\Files_External\Service\GlobalStoragesService;
  12. use OCP\AppFramework\Http;
  13. use OCP\IGroupManager;
  14. use OCP\IUserManager;
  15. use Symfony\Component\Console\Input\InputArgument;
  16. use Symfony\Component\Console\Input\InputInterface;
  17. use Symfony\Component\Console\Input\InputOption;
  18. use Symfony\Component\Console\Output\OutputInterface;
  19. class Applicable extends Base {
  20. public function __construct(
  21. protected GlobalStoragesService $globalService,
  22. private IUserManager $userManager,
  23. private IGroupManager $groupManager,
  24. ) {
  25. parent::__construct();
  26. }
  27. protected function configure(): void {
  28. $this
  29. ->setName('files_external:applicable')
  30. ->setDescription('Manage applicable users and groups for a mount')
  31. ->addArgument(
  32. 'mount_id',
  33. InputArgument::REQUIRED,
  34. 'The id of the mount to edit'
  35. )->addOption(
  36. 'add-user',
  37. '',
  38. InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
  39. 'user to add as applicable'
  40. )->addOption(
  41. 'remove-user',
  42. '',
  43. InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
  44. 'user to remove as applicable'
  45. )->addOption(
  46. 'add-group',
  47. '',
  48. InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
  49. 'group to add as applicable'
  50. )->addOption(
  51. 'remove-group',
  52. '',
  53. InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
  54. 'group to remove as applicable'
  55. )->addOption(
  56. 'remove-all',
  57. '',
  58. InputOption::VALUE_NONE,
  59. 'Set the mount to be globally applicable'
  60. );
  61. parent::configure();
  62. }
  63. protected function execute(InputInterface $input, OutputInterface $output): int {
  64. $mountId = $input->getArgument('mount_id');
  65. try {
  66. $mount = $this->globalService->getStorage($mountId);
  67. } catch (NotFoundException $e) {
  68. $output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts</error>');
  69. return Http::STATUS_NOT_FOUND;
  70. }
  71. if ($mount->getType() === StorageConfig::MOUNT_TYPE_PERSONAL) {
  72. $output->writeln('<error>Can\'t change applicables on personal mounts</error>');
  73. return self::FAILURE;
  74. }
  75. $addUsers = $input->getOption('add-user');
  76. $removeUsers = $input->getOption('remove-user');
  77. $addGroups = $input->getOption('add-group');
  78. $removeGroups = $input->getOption('remove-group');
  79. $applicableUsers = $mount->getApplicableUsers();
  80. $applicableGroups = $mount->getApplicableGroups();
  81. if ((count($addUsers) + count($removeUsers) + count($addGroups) + count($removeGroups) > 0) || $input->getOption('remove-all')) {
  82. foreach ($addUsers as $addUser) {
  83. if (!$this->userManager->userExists($addUser)) {
  84. $output->writeln('<error>User "' . $addUser . '" not found</error>');
  85. return Http::STATUS_NOT_FOUND;
  86. }
  87. }
  88. foreach ($addGroups as $addGroup) {
  89. if (!$this->groupManager->groupExists($addGroup)) {
  90. $output->writeln('<error>Group "' . $addGroup . '" not found</error>');
  91. return Http::STATUS_NOT_FOUND;
  92. }
  93. }
  94. if ($input->getOption('remove-all')) {
  95. $applicableUsers = [];
  96. $applicableGroups = [];
  97. } else {
  98. $applicableUsers = array_unique(array_merge($applicableUsers, $addUsers));
  99. $applicableUsers = array_values(array_diff($applicableUsers, $removeUsers));
  100. $applicableGroups = array_unique(array_merge($applicableGroups, $addGroups));
  101. $applicableGroups = array_values(array_diff($applicableGroups, $removeGroups));
  102. }
  103. $mount->setApplicableUsers($applicableUsers);
  104. $mount->setApplicableGroups($applicableGroups);
  105. $this->globalService->updateStorage($mount);
  106. }
  107. $this->writeArrayInOutputFormat($input, $output, [
  108. 'users' => $applicableUsers,
  109. 'groups' => $applicableGroups
  110. ]);
  111. return self::SUCCESS;
  112. }
  113. }