Add.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. public function __construct(
  41. protected IUserManager $userManager,
  42. protected IGroupManager $groupManager,
  43. ) {
  44. parent::__construct();
  45. }
  46. protected function configure() {
  47. $this
  48. ->setName('user:add')
  49. ->setDescription('adds a user')
  50. ->addArgument(
  51. 'uid',
  52. InputArgument::REQUIRED,
  53. 'User ID used to login (must only contain a-z, A-Z, 0-9, -, _ and @)'
  54. )
  55. ->addOption(
  56. 'password-from-env',
  57. null,
  58. InputOption::VALUE_NONE,
  59. 'read password from environment variable OC_PASS'
  60. )
  61. ->addOption(
  62. 'display-name',
  63. null,
  64. InputOption::VALUE_OPTIONAL,
  65. 'User name used in the web UI (can contain any characters)'
  66. )
  67. ->addOption(
  68. 'group',
  69. 'g',
  70. InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
  71. 'groups the user should be added to (The group will be created if it does not exist)'
  72. );
  73. }
  74. protected function execute(InputInterface $input, OutputInterface $output): int {
  75. $uid = $input->getArgument('uid');
  76. if ($this->userManager->userExists($uid)) {
  77. $output->writeln('<error>The user "' . $uid . '" already exists.</error>');
  78. return 1;
  79. }
  80. if ($input->getOption('password-from-env')) {
  81. $password = getenv('OC_PASS');
  82. if (!$password) {
  83. $output->writeln('<error>--password-from-env given, but OC_PASS is empty!</error>');
  84. return 1;
  85. }
  86. } elseif ($input->isInteractive()) {
  87. /** @var QuestionHelper $helper */
  88. $helper = $this->getHelper('question');
  89. $question = new Question('Enter password: ');
  90. $question->setHidden(true);
  91. $password = $helper->ask($input, $output, $question);
  92. $question = new Question('Confirm password: ');
  93. $question->setHidden(true);
  94. $confirm = $helper->ask($input, $output, $question);
  95. if ($password !== $confirm) {
  96. $output->writeln("<error>Passwords did not match!</error>");
  97. return 1;
  98. }
  99. } else {
  100. $output->writeln("<error>Interactive input or --password-from-env is needed for entering a password!</error>");
  101. return 1;
  102. }
  103. try {
  104. $user = $this->userManager->createUser(
  105. $input->getArgument('uid'),
  106. $password
  107. );
  108. } catch (\Exception $e) {
  109. $output->writeln('<error>' . $e->getMessage() . '</error>');
  110. return 1;
  111. }
  112. if ($user instanceof IUser) {
  113. $output->writeln('<info>The user "' . $user->getUID() . '" was created successfully</info>');
  114. } else {
  115. $output->writeln('<error>An error occurred while creating the user</error>');
  116. return 1;
  117. }
  118. if ($input->getOption('display-name')) {
  119. $user->setDisplayName($input->getOption('display-name'));
  120. $output->writeln('Display name set to "' . $user->getDisplayName() . '"');
  121. }
  122. $groups = $input->getOption('group');
  123. if (!empty($groups)) {
  124. // Make sure we init the Filesystem for the user, in case we need to
  125. // init some group shares.
  126. Filesystem::init($user->getUID(), '');
  127. }
  128. foreach ($groups as $groupName) {
  129. $group = $this->groupManager->get($groupName);
  130. if (!$group) {
  131. $this->groupManager->createGroup($groupName);
  132. $group = $this->groupManager->get($groupName);
  133. if ($group instanceof IGroup) {
  134. $output->writeln('Created group "' . $group->getGID() . '"');
  135. }
  136. }
  137. if ($group instanceof IGroup) {
  138. $group->addUser($user);
  139. $output->writeln('User "' . $user->getUID() . '" added to group "' . $group->getGID() . '"');
  140. }
  141. }
  142. return 0;
  143. }
  144. }