add.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. * @author Laurens Post <lkpost@scept.re>
  5. *
  6. * @copyright Copyright (c) 2015, 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 OCP\IGroupManager;
  24. use OCP\IUser;
  25. use OCP\IUserManager;
  26. use Symfony\Component\Console\Command\Command;
  27. use Symfony\Component\Console\Input\InputInterface;
  28. use Symfony\Component\Console\Input\InputOption;
  29. use Symfony\Component\Console\Output\OutputInterface;
  30. use Symfony\Component\Console\Input\InputArgument;
  31. use Symfony\Component\Console\Question\Question;
  32. class Add extends Command {
  33. /** @var \OCP\IUserManager */
  34. protected $userManager;
  35. /** @var \OCP\IGroupManager */
  36. protected $groupManager;
  37. /**
  38. * @param IUserManager $userManager
  39. * @param IGroupManager $groupManager
  40. */
  41. public function __construct(IUserManager $userManager, IGroupManager $groupManager) {
  42. parent::__construct();
  43. $this->userManager = $userManager;
  44. $this->groupManager = $groupManager;
  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) {
  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 $dialog \Symfony\Component\Console\Helper\DialogHelper */
  88. $dialog = $this->getHelperSet()->get('dialog');
  89. $password = $dialog->askHiddenResponse(
  90. $output,
  91. '<question>Enter password: </question>',
  92. false
  93. );
  94. $confirm = $dialog->askHiddenResponse(
  95. $output,
  96. '<question>Confirm password: </question>',
  97. false
  98. );
  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. $user = $this->userManager->createUser(
  108. $input->getArgument('uid'),
  109. $password
  110. );
  111. if ($user instanceof IUser) {
  112. $output->writeln('<info>The user "' . $user->getUID() . '" was created successfully</info>');
  113. } else {
  114. $output->writeln('<error>An error occurred while creating the user</error>');
  115. return 1;
  116. }
  117. if ($input->getOption('display-name')) {
  118. $user->setDisplayName($input->getOption('display-name'));
  119. $output->writeln('Display name set to "' . $user->getDisplayName() . '"');
  120. }
  121. foreach ($input->getOption('group') as $groupName) {
  122. $group = $this->groupManager->get($groupName);
  123. if (!$group) {
  124. $this->groupManager->createGroup($groupName);
  125. $group = $this->groupManager->get($groupName);
  126. $output->writeln('Created group "' . $group->getGID() . '"');
  127. }
  128. $group->addUser($user);
  129. $output->writeln('User "' . $user->getUID() . '" added to group "' . $group->getGID() . '"');
  130. }
  131. }
  132. }