Add.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Laurens Post <lkpost@scept.re>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  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, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Core\Command\User;
  27. use OC\Files\Filesystem;
  28. use OCP\IGroup;
  29. use OCP\IGroupManager;
  30. use OCP\IUser;
  31. use OCP\IUserManager;
  32. use Symfony\Component\Console\Command\Command;
  33. use Symfony\Component\Console\Helper\QuestionHelper;
  34. use Symfony\Component\Console\Input\InputArgument;
  35. use Symfony\Component\Console\Input\InputInterface;
  36. use Symfony\Component\Console\Input\InputOption;
  37. use Symfony\Component\Console\Output\OutputInterface;
  38. use Symfony\Component\Console\Question\Question;
  39. class Add extends Command {
  40. protected IUserManager $userManager;
  41. protected IGroupManager $groupManager;
  42. public function __construct(IUserManager $userManager, IGroupManager $groupManager) {
  43. parent::__construct();
  44. $this->userManager = $userManager;
  45. $this->groupManager = $groupManager;
  46. }
  47. protected function configure() {
  48. $this
  49. ->setName('user:add')
  50. ->setDescription('adds a user')
  51. ->addArgument(
  52. 'uid',
  53. InputArgument::REQUIRED,
  54. 'User ID used to login (must only contain a-z, A-Z, 0-9, -, _ and @)'
  55. )
  56. ->addOption(
  57. 'password-from-env',
  58. null,
  59. InputOption::VALUE_NONE,
  60. 'read password from environment variable OC_PASS'
  61. )
  62. ->addOption(
  63. 'display-name',
  64. null,
  65. InputOption::VALUE_OPTIONAL,
  66. 'User name used in the web UI (can contain any characters)'
  67. )
  68. ->addOption(
  69. 'group',
  70. 'g',
  71. InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
  72. 'groups the user should be added to (The group will be created if it does not exist)'
  73. );
  74. }
  75. protected function execute(InputInterface $input, OutputInterface $output): int {
  76. $uid = $input->getArgument('uid');
  77. if ($this->userManager->userExists($uid)) {
  78. $output->writeln('<error>The user "' . $uid . '" already exists.</error>');
  79. return 1;
  80. }
  81. if ($input->getOption('password-from-env')) {
  82. $password = getenv('OC_PASS');
  83. if (!$password) {
  84. $output->writeln('<error>--password-from-env given, but OC_PASS is empty!</error>');
  85. return 1;
  86. }
  87. } elseif ($input->isInteractive()) {
  88. /** @var QuestionHelper $helper */
  89. $helper = $this->getHelper('question');
  90. $question = new Question('Enter password: ');
  91. $question->setHidden(true);
  92. $password = $helper->ask($input, $output, $question);
  93. $question = new Question('Confirm password: ');
  94. $question->setHidden(true);
  95. $confirm = $helper->ask($input, $output, $question);
  96. if ($password !== $confirm) {
  97. $output->writeln("<error>Passwords did not match!</error>");
  98. return 1;
  99. }
  100. } else {
  101. $output->writeln("<error>Interactive input or --password-from-env is needed for entering a password!</error>");
  102. return 1;
  103. }
  104. try {
  105. $user = $this->userManager->createUser(
  106. $input->getArgument('uid'),
  107. $password
  108. );
  109. } catch (\Exception $e) {
  110. $output->writeln('<error>' . $e->getMessage() . '</error>');
  111. return 1;
  112. }
  113. if ($user instanceof IUser) {
  114. $output->writeln('<info>The user "' . $user->getUID() . '" was created successfully</info>');
  115. } else {
  116. $output->writeln('<error>An error occurred while creating the user</error>');
  117. return 1;
  118. }
  119. if ($input->getOption('display-name')) {
  120. $user->setDisplayName($input->getOption('display-name'));
  121. $output->writeln('Display name set to "' . $user->getDisplayName() . '"');
  122. }
  123. $groups = $input->getOption('group');
  124. if (!empty($groups)) {
  125. // Make sure we init the Filesystem for the user, in case we need to
  126. // init some group shares.
  127. Filesystem::init($user->getUID(), '');
  128. }
  129. foreach ($groups as $groupName) {
  130. $group = $this->groupManager->get($groupName);
  131. if (!$group) {
  132. $this->groupManager->createGroup($groupName);
  133. $group = $this->groupManager->get($groupName);
  134. if ($group instanceof IGroup) {
  135. $output->writeln('Created group "' . $group->getGID() . '"');
  136. }
  137. }
  138. if ($group instanceof IGroup) {
  139. $group->addUser($user);
  140. $output->writeln('User "' . $user->getUID() . '" added to group "' . $group->getGID() . '"');
  141. }
  142. }
  143. return 0;
  144. }
  145. }