add.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace OC\Core\Command\User;
  22. use OCP\IGroupManager;
  23. use OCP\IUser;
  24. use OCP\IUserManager;
  25. use Symfony\Component\Console\Command\Command;
  26. use Symfony\Component\Console\Input\InputInterface;
  27. use Symfony\Component\Console\Input\InputOption;
  28. use Symfony\Component\Console\Output\OutputInterface;
  29. use Symfony\Component\Console\Input\InputArgument;
  30. use Symfony\Component\Console\Question\Question;
  31. class Add extends Command {
  32. /** @var \OCP\IUserManager */
  33. protected $userManager;
  34. /** @var \OCP\IGroupManager */
  35. protected $groupManager;
  36. /**
  37. * @param IUserManager $userManager
  38. * @param IGroupManager $groupManager
  39. */
  40. public function __construct(IUserManager $userManager, IGroupManager $groupManager) {
  41. parent::__construct();
  42. $this->userManager = $userManager;
  43. $this->groupManager = $groupManager;
  44. }
  45. protected function configure() {
  46. $this
  47. ->setName('user:add')
  48. ->setDescription('adds a user')
  49. ->addArgument(
  50. 'uid',
  51. InputArgument::REQUIRED,
  52. 'User ID used to login (must only contain a-z, A-Z, 0-9, -, _ and @)'
  53. )
  54. ->addOption(
  55. 'password',
  56. 'p',
  57. InputOption::VALUE_OPTIONAL,
  58. ''
  59. )
  60. ->addOption(
  61. 'display-name',
  62. null,
  63. InputOption::VALUE_OPTIONAL,
  64. 'User name used in the web UI (can contain any characters)'
  65. )
  66. ->addOption(
  67. 'group',
  68. 'g',
  69. InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
  70. 'groups the user should be added to (The group will be created if it does not exist)'
  71. );
  72. }
  73. protected function execute(InputInterface $input, OutputInterface $output) {
  74. $uid = $input->getArgument('uid');
  75. if ($this->userManager->userExists($uid)) {
  76. $output->writeln('<error>The user "' . $uid . '" already exists.</error>');
  77. return 1;
  78. }
  79. $password = $input->getOption('password');
  80. while (!$password) {
  81. $question = new Question('Please enter a non-empty password:');
  82. $question->setHidden(true);
  83. $question->setHiddenFallback(false);
  84. $helper = $this->getHelper('question');
  85. $password = $helper->ask($input, $output, $question);
  86. }
  87. $user = $this->userManager->createUser(
  88. $input->getArgument('uid'),
  89. $password
  90. );
  91. if ($user instanceof IUser) {
  92. $output->writeln('The user "' . $user->getUID() . '" was created successfully');
  93. } else {
  94. $output->writeln('<error>An error occurred while creating the user</error>');
  95. return 1;
  96. }
  97. if ($input->getOption('display-name')) {
  98. $user->setDisplayName($input->getOption('display-name'));
  99. $output->writeln('Display name set to "' . $user->getDisplayName() . '"');
  100. }
  101. foreach ($input->getOption('group') as $groupName) {
  102. $group = $this->groupManager->get($groupName);
  103. if (!$group) {
  104. $this->groupManager->createGroup($groupName);
  105. $group = $this->groupManager->get($groupName);
  106. $output->writeln('Created group "' . $group->getGID() . '"');
  107. }
  108. $group->addUser($user);
  109. $output->writeln('User "' . $user->getUID() . '" added to group "' . $group->getGID() . '"');
  110. }
  111. }
  112. }