Add.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018 Denis Mosolov <denismosolov@gmail.com>
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Denis Mosolov <denismosolov@gmail.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OC\Core\Command\Group;
  27. use OC\Core\Command\Base;
  28. use OCP\IGroup;
  29. use OCP\IGroupManager;
  30. use Symfony\Component\Console\Input\InputArgument;
  31. use Symfony\Component\Console\Input\InputInterface;
  32. use Symfony\Component\Console\Input\InputOption;
  33. use Symfony\Component\Console\Output\OutputInterface;
  34. class Add extends Base {
  35. protected IGroupManager $groupManager;
  36. public function __construct(IGroupManager $groupManager) {
  37. $this->groupManager = $groupManager;
  38. parent::__construct();
  39. }
  40. protected function configure() {
  41. $this
  42. ->setName('group:add')
  43. ->setDescription('Add a group')
  44. ->addArgument(
  45. 'groupid',
  46. InputArgument::REQUIRED,
  47. 'Group id'
  48. )
  49. ->addOption(
  50. 'display-name',
  51. null,
  52. InputOption::VALUE_REQUIRED,
  53. 'Group name used in the web UI (can contain any characters)'
  54. );
  55. }
  56. protected function execute(InputInterface $input, OutputInterface $output): int {
  57. $gid = $input->getArgument('groupid');
  58. $group = $this->groupManager->get($gid);
  59. if ($group) {
  60. $output->writeln('<error>Group "' . $gid . '" already exists.</error>');
  61. return 1;
  62. } else {
  63. $group = $this->groupManager->createGroup($gid);
  64. if (!$group instanceof IGroup) {
  65. $output->writeln('<error>Could not create group</error>');
  66. return 2;
  67. }
  68. $output->writeln('Created group "' . $group->getGID() . '"');
  69. $displayName = trim((string)$input->getOption('display-name'));
  70. if ($displayName !== '') {
  71. $group->setDisplayName($displayName);
  72. }
  73. }
  74. return 0;
  75. }
  76. }