RemoveUser.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 RemoveUser extends Base {
  35. public function __construct(
  36. protected IUserManager $userManager,
  37. protected IGroupManager $groupManager,
  38. ) {
  39. parent::__construct();
  40. }
  41. protected function configure() {
  42. $this
  43. ->setName('group:removeuser')
  44. ->setDescription('remove a user from a group')
  45. ->addArgument(
  46. 'group',
  47. InputArgument::REQUIRED,
  48. 'group to remove the user from'
  49. )->addArgument(
  50. 'user',
  51. InputArgument::REQUIRED,
  52. 'user to remove from the group'
  53. );
  54. }
  55. protected function execute(InputInterface $input, OutputInterface $output): int {
  56. $group = $this->groupManager->get($input->getArgument('group'));
  57. if (is_null($group)) {
  58. $output->writeln('<error>group not found</error>');
  59. return 1;
  60. }
  61. $user = $this->userManager->get($input->getArgument('user'));
  62. if (is_null($user)) {
  63. $output->writeln('<error>user not found</error>');
  64. return 1;
  65. }
  66. $group->removeUser($user);
  67. return 0;
  68. }
  69. /**
  70. * @param string $argumentName
  71. * @param CompletionContext $context
  72. * @return string[]
  73. */
  74. public function completeArgumentValues($argumentName, CompletionContext $context) {
  75. if ($argumentName === 'group') {
  76. return array_map(static fn (IGroup $group) => $group->getGID(), $this->groupManager->search($context->getCurrentWord()));
  77. }
  78. if ($argumentName === 'user') {
  79. $groupId = $context->getWordAtIndex($context->getWordIndex() - 1);
  80. $group = $this->groupManager->get($groupId);
  81. if ($group === null) {
  82. return [];
  83. }
  84. return array_map(static fn (IUser $user) => $user->getUID(), $group->searchUsers($context->getCurrentWord()));
  85. }
  86. return [];
  87. }
  88. }