Add.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. * @author Laurens Post <lkpost@scept.re>
  5. *
  6. * @copyright Copyright (c) 2016, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Core\Command\User;
  23. use OC\Files\Filesystem;
  24. use OCP\IGroupManager;
  25. use OCP\IUser;
  26. use OCP\IUserManager;
  27. use Symfony\Component\Console\Command\Command;
  28. use Symfony\Component\Console\Input\InputInterface;
  29. use Symfony\Component\Console\Input\InputOption;
  30. use Symfony\Component\Console\Output\OutputInterface;
  31. use Symfony\Component\Console\Input\InputArgument;
  32. use Symfony\Component\Console\Question\Question;
  33. class Add extends Command {
  34. /** @var \OCP\IUserManager */
  35. protected $userManager;
  36. /** @var \OCP\IGroupManager */
  37. protected $groupManager;
  38. /**
  39. * @param IUserManager $userManager
  40. * @param IGroupManager $groupManager
  41. */
  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) {
  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 $dialog \Symfony\Component\Console\Helper\DialogHelper */
  89. $dialog = $this->getHelperSet()->get('dialog');
  90. $password = $dialog->askHiddenResponse(
  91. $output,
  92. '<question>Enter password: </question>',
  93. false
  94. );
  95. $confirm = $dialog->askHiddenResponse(
  96. $output,
  97. '<question>Confirm password: </question>',
  98. false
  99. );
  100. if ($password !== $confirm) {
  101. $output->writeln("<error>Passwords did not match!</error>");
  102. return 1;
  103. }
  104. } else {
  105. $output->writeln("<error>Interactive input or --password-from-env is needed for entering a password!</error>");
  106. return 1;
  107. }
  108. $user = $this->userManager->createUser(
  109. $input->getArgument('uid'),
  110. $password
  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. $output->writeln('Created group "' . $group->getGID() . '"');
  134. }
  135. $group->addUser($user);
  136. $output->writeln('User "' . $user->getUID() . '" added to group "' . $group->getGID() . '"');
  137. }
  138. }
  139. }