Add.php 4.7 KB

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