AddUser.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Core\Command\Group;
  25. use OC\Core\Command\Base;
  26. use OCP\IGroup;
  27. use OCP\IGroupManager;
  28. use OCP\IUser;
  29. use OCP\IUserManager;
  30. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  31. use Symfony\Component\Console\Input\InputArgument;
  32. use Symfony\Component\Console\Input\InputInterface;
  33. use Symfony\Component\Console\Output\OutputInterface;
  34. class AddUser extends Base {
  35. protected IUserManager $userManager;
  36. protected IGroupManager $groupManager;
  37. public function __construct(IUserManager $userManager, IGroupManager $groupManager) {
  38. $this->userManager = $userManager;
  39. $this->groupManager = $groupManager;
  40. parent::__construct();
  41. }
  42. protected function configure() {
  43. $this
  44. ->setName('group:adduser')
  45. ->setDescription('add a user to a group')
  46. ->addArgument(
  47. 'group',
  48. InputArgument::REQUIRED,
  49. 'group to add the user to'
  50. )->addArgument(
  51. 'user',
  52. InputArgument::REQUIRED,
  53. 'user to add to the group'
  54. );
  55. }
  56. protected function execute(InputInterface $input, OutputInterface $output): int {
  57. $group = $this->groupManager->get($input->getArgument('group'));
  58. if (is_null($group)) {
  59. $output->writeln('<error>group not found</error>');
  60. return 1;
  61. }
  62. $user = $this->userManager->get($input->getArgument('user'));
  63. if (is_null($user)) {
  64. $output->writeln('<error>user not found</error>');
  65. return 1;
  66. }
  67. $group->addUser($user);
  68. return 0;
  69. }
  70. /**
  71. * @param string $argumentName
  72. * @param CompletionContext $context
  73. * @return string[]
  74. */
  75. public function completeArgumentValues($argumentName, CompletionContext $context) {
  76. if ($argumentName === 'group') {
  77. return array_map(static fn (IGroup $group) => $group->getGID(), $this->groupManager->search($context->getCurrentWord()));
  78. }
  79. if ($argumentName === 'user') {
  80. $groupId = $context->getWordAtIndex($context->getWordIndex() - 1);
  81. $group = $this->groupManager->get($groupId);
  82. if ($group === null) {
  83. return [];
  84. }
  85. $members = array_map(static fn (IUser $user) => $user->getUID(), $group->searchUsers($context->getCurrentWord()));
  86. $users = array_map(static fn (IUser $user) => $user->getUID(), $this->userManager->search($context->getCurrentWord()));
  87. return array_diff($users, $members);
  88. }
  89. return [];
  90. }
  91. }