Enable.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. protected IUserManager $userManager;
  34. public function __construct(IUserManager $userManager) {
  35. $this->userManager = $userManager;
  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. }