Add.php 5.0 KB

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