Enable.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  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\Core\Command\Base;
  26. use OCP\IUser;
  27. use OCP\IUserManager;
  28. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  29. use Symfony\Component\Console\Input\InputArgument;
  30. use Symfony\Component\Console\Input\InputInterface;
  31. use Symfony\Component\Console\Output\OutputInterface;
  32. class Enable extends Base {
  33. public function __construct(
  34. protected IUserManager $userManager,
  35. ) {
  36. parent::__construct();
  37. }
  38. protected function configure() {
  39. $this
  40. ->setName('user:enable')
  41. ->setDescription('enables the specified user')
  42. ->addArgument(
  43. 'uid',
  44. InputArgument::REQUIRED,
  45. 'the username'
  46. );
  47. }
  48. protected function execute(InputInterface $input, OutputInterface $output): int {
  49. $user = $this->userManager->get($input->getArgument('uid'));
  50. if (is_null($user)) {
  51. $output->writeln('<error>User does not exist</error>');
  52. return 1;
  53. }
  54. $user->setEnabled(true);
  55. $output->writeln('<info>The specified user is enabled</info>');
  56. return 0;
  57. }
  58. /**
  59. * @param string $argumentName
  60. * @param CompletionContext $context
  61. * @return string[]
  62. */
  63. public function completeArgumentValues($argumentName, CompletionContext $context) {
  64. if ($argumentName === 'uid') {
  65. return array_map(
  66. static fn (IUser $user) => $user->getUID(),
  67. array_filter(
  68. $this->userManager->search($context->getCurrentWord()),
  69. static fn (IUser $user) => !$user->isEnabled()
  70. )
  71. );
  72. }
  73. return [];
  74. }
  75. }