1
0

Add.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 Symfony\Component\Console\Input\InputArgument;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Input\InputOption;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. class Add extends Base {
  16. public function __construct(
  17. protected IGroupManager $groupManager,
  18. ) {
  19. parent::__construct();
  20. }
  21. protected function configure() {
  22. $this
  23. ->setName('group:add')
  24. ->setDescription('Add a group')
  25. ->addArgument(
  26. 'groupid',
  27. InputArgument::REQUIRED,
  28. 'Group id'
  29. )
  30. ->addOption(
  31. 'display-name',
  32. null,
  33. InputOption::VALUE_REQUIRED,
  34. 'Group name used in the web UI (can contain any characters)'
  35. );
  36. }
  37. protected function execute(InputInterface $input, OutputInterface $output): int {
  38. $gid = $input->getArgument('groupid');
  39. $group = $this->groupManager->get($gid);
  40. if ($group) {
  41. $output->writeln('<error>Group "' . $gid . '" already exists.</error>');
  42. return 1;
  43. } else {
  44. $group = $this->groupManager->createGroup($gid);
  45. if (!$group instanceof IGroup) {
  46. $output->writeln('<error>Could not create group</error>');
  47. return 2;
  48. }
  49. $output->writeln('Created group "' . $group->getGID() . '"');
  50. $displayName = trim((string)$input->getOption('display-name'));
  51. if ($displayName !== '') {
  52. $group->setDisplayName($displayName);
  53. }
  54. }
  55. return 0;
  56. }
  57. }