Add.php 4.8 KB

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