Delete.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Command\Group;
  8. use OC\Core\Command\Base;
  9. use OCP\IGroup;
  10. use OCP\IGroupManager;
  11. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  12. use Symfony\Component\Console\Input\InputArgument;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. class Delete extends Base {
  16. public function __construct(
  17. protected IGroupManager $groupManager,
  18. ) {
  19. parent::__construct();
  20. }
  21. protected function configure() {
  22. $this
  23. ->setName('group:delete')
  24. ->setDescription('Remove a group')
  25. ->addArgument(
  26. 'groupid',
  27. InputArgument::REQUIRED,
  28. 'Group name'
  29. );
  30. }
  31. protected function execute(InputInterface $input, OutputInterface $output): int {
  32. $gid = $input->getArgument('groupid');
  33. if ($gid === 'admin') {
  34. $output->writeln('<error>Group "' . $gid . '" could not be deleted.</error>');
  35. return 1;
  36. }
  37. if (!$this->groupManager->groupExists($gid)) {
  38. $output->writeln('<error>Group "' . $gid . '" does not exist.</error>');
  39. return 1;
  40. }
  41. $group = $this->groupManager->get($gid);
  42. if ($group->delete()) {
  43. $output->writeln('Group "' . $gid . '" was removed');
  44. } else {
  45. $output->writeln('<error>Group "' . $gid . '" could not be deleted. Please check the logs.</error>');
  46. return 1;
  47. }
  48. return 0;
  49. }
  50. /**
  51. * @param string $argumentName
  52. * @param CompletionContext $context
  53. * @return string[]
  54. */
  55. public function completeArgumentValues($argumentName, CompletionContext $context) {
  56. if ($argumentName === 'groupid') {
  57. return array_map(static fn (IGroup $group) => $group->getGID(), $this->groupManager->search($context->getCurrentWord()));
  58. }
  59. return [];
  60. }
  61. }