You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Delete.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018 Denis Mosolov <denismosolov@gmail.com>
  5. *
  6. * @author Denis Mosolov <denismosolov@gmail.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  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
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Core\Command\Group;
  26. use OC\Core\Command\Base;
  27. use OCP\IGroup;
  28. use OCP\IGroupManager;
  29. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  30. use Symfony\Component\Console\Input\InputArgument;
  31. use Symfony\Component\Console\Input\InputInterface;
  32. use Symfony\Component\Console\Output\OutputInterface;
  33. class Delete extends Base {
  34. protected IGroupManager $groupManager;
  35. public function __construct(IGroupManager $groupManager) {
  36. $this->groupManager = $groupManager;
  37. parent::__construct();
  38. }
  39. protected function configure() {
  40. $this
  41. ->setName('group:delete')
  42. ->setDescription('Remove a group')
  43. ->addArgument(
  44. 'groupid',
  45. InputArgument::REQUIRED,
  46. 'Group name'
  47. );
  48. }
  49. protected function execute(InputInterface $input, OutputInterface $output): int {
  50. $gid = $input->getArgument('groupid');
  51. if ($gid === 'admin') {
  52. $output->writeln('<error>Group "' . $gid . '" could not be deleted.</error>');
  53. return 1;
  54. }
  55. if (!$this->groupManager->groupExists($gid)) {
  56. $output->writeln('<error>Group "' . $gid . '" does not exist.</error>');
  57. return 1;
  58. }
  59. $group = $this->groupManager->get($gid);
  60. if ($group->delete()) {
  61. $output->writeln('Group "' . $gid . '" was removed');
  62. } else {
  63. $output->writeln('<error>Group "' . $gid . '" could not be deleted. Please check the logs.</error>');
  64. return 1;
  65. }
  66. return 0;
  67. }
  68. /**
  69. * @param string $argumentName
  70. * @param CompletionContext $context
  71. * @return string[]
  72. */
  73. public function completeArgumentValues($argumentName, CompletionContext $context) {
  74. if ($argumentName === 'groupid') {
  75. return array_map(static fn (IGroup $group) => $group->getGID(), $this->groupManager->search($context->getCurrentWord()));
  76. }
  77. return [];
  78. }
  79. }