AddUser.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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\IGroupManager;
  27. use OCP\IUserManager;
  28. use Symfony\Component\Console\Input\InputArgument;
  29. use Symfony\Component\Console\Input\InputInterface;
  30. use Symfony\Component\Console\Output\OutputInterface;
  31. class AddUser extends Base {
  32. /** @var IUserManager */
  33. protected $userManager;
  34. /** @var IGroupManager */
  35. protected $groupManager;
  36. /**
  37. * @param IUserManager $userManager
  38. * @param IGroupManager $groupManager
  39. */
  40. public function __construct(IUserManager $userManager, IGroupManager $groupManager) {
  41. $this->userManager = $userManager;
  42. $this->groupManager = $groupManager;
  43. parent::__construct();
  44. }
  45. protected function configure() {
  46. $this
  47. ->setName('group:adduser')
  48. ->setDescription('add a user to a group')
  49. ->addArgument(
  50. 'group',
  51. InputArgument::REQUIRED,
  52. 'group to add the user to'
  53. )->addArgument(
  54. 'user',
  55. InputArgument::REQUIRED,
  56. 'user to add to the group'
  57. );
  58. }
  59. protected function execute(InputInterface $input, OutputInterface $output): int {
  60. $group = $this->groupManager->get($input->getArgument('group'));
  61. if (is_null($group)) {
  62. $output->writeln('<error>group not found</error>');
  63. return 1;
  64. }
  65. $user = $this->userManager->get($input->getArgument('user'));
  66. if (is_null($user)) {
  67. $output->writeln('<error>user not found</error>');
  68. return 1;
  69. }
  70. $group->addUser($user);
  71. return 0;
  72. }
  73. }