Add.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. declare(strict_types = 1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Settings\Command\AdminDelegation;
  8. use OC\Core\Command\Base;
  9. use OCA\Settings\Service\AuthorizedGroupService;
  10. use OCP\IGroupManager;
  11. use OCP\Settings\IDelegatedSettings;
  12. use OCP\Settings\IManager;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. use Symfony\Component\Console\Style\SymfonyStyle;
  17. class Add extends Base {
  18. public function __construct(
  19. private IManager $settingManager,
  20. private AuthorizedGroupService $authorizedGroupService,
  21. private IGroupManager $groupManager,
  22. ) {
  23. parent::__construct();
  24. }
  25. protected function configure(): void {
  26. $this
  27. ->setName('admin-delegation:add')
  28. ->setDescription('add setting delegation to a group')
  29. ->addArgument('settingClass', InputArgument::REQUIRED, 'Admin setting class')
  30. ->addArgument('groupId', InputArgument::REQUIRED, 'Delegate to group ID')
  31. ->addUsage('\'OCA\Settings\Settings\Admin\Server\' mygroup')
  32. ;
  33. }
  34. protected function execute(InputInterface $input, OutputInterface $output): int {
  35. $io = new SymfonyStyle($input, $output);
  36. $settingClass = $input->getArgument('settingClass');
  37. if (!in_array(IDelegatedSettings::class, (array)class_implements($settingClass), true)) {
  38. $io->error('The specified class isn’t a valid delegated setting.');
  39. return 2;
  40. }
  41. $groupId = $input->getArgument('groupId');
  42. if (!$this->groupManager->groupExists($groupId)) {
  43. $io->error('The specified group didn’t exist.');
  44. return 3;
  45. }
  46. $this->authorizedGroupService->create($groupId, $settingClass);
  47. $io->success('Administration of ' . $settingClass . ' delegated to ' . $groupId . '.');
  48. return 0;
  49. }
  50. }