Add.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. /** @var IGroupManager */
  36. protected $groupManager;
  37. /**
  38. * @param IGroupManager $groupManager
  39. */
  40. public function __construct(IGroupManager $groupManager) {
  41. $this->groupManager = $groupManager;
  42. parent::__construct();
  43. }
  44. protected function configure() {
  45. $this
  46. ->setName('group:add')
  47. ->setDescription('Add a group')
  48. ->addArgument(
  49. 'groupid',
  50. InputArgument::REQUIRED,
  51. 'Group id'
  52. )
  53. ->addOption(
  54. 'display-name',
  55. null,
  56. InputOption::VALUE_REQUIRED,
  57. 'Group name used in the web UI (can contain any characters)'
  58. );
  59. }
  60. protected function execute(InputInterface $input, OutputInterface $output): int {
  61. $gid = $input->getArgument('groupid');
  62. $group = $this->groupManager->get($gid);
  63. if ($group) {
  64. $output->writeln('<error>Group "' . $gid . '" already exists.</error>');
  65. return 1;
  66. } else {
  67. $group = $this->groupManager->createGroup($gid);
  68. if (!$group instanceof IGroup) {
  69. $output->writeln('<error>Could not create group</error>');
  70. return 2;
  71. }
  72. $output->writeln('Created group "' . $group->getGID() . '"');
  73. $displayName = trim((string)$input->getOption('display-name'));
  74. if ($displayName !== '') {
  75. $group->setDisplayName($displayName);
  76. }
  77. }
  78. return 0;
  79. }
  80. }