Enable.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Core\Command\User;
  8. use OC\Core\Command\Base;
  9. use OCP\IUser;
  10. use OCP\IUserManager;
  11. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  12. use Symfony\Component\Console\Input\InputArgument;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. class Enable extends Base {
  16. public function __construct(
  17. protected IUserManager $userManager,
  18. ) {
  19. parent::__construct();
  20. }
  21. protected function configure() {
  22. $this
  23. ->setName('user:enable')
  24. ->setDescription('enables the specified user')
  25. ->addArgument(
  26. 'uid',
  27. InputArgument::REQUIRED,
  28. 'the username'
  29. );
  30. }
  31. protected function execute(InputInterface $input, OutputInterface $output): int {
  32. $user = $this->userManager->get($input->getArgument('uid'));
  33. if (is_null($user)) {
  34. $output->writeln('<error>User does not exist</error>');
  35. return 1;
  36. }
  37. $user->setEnabled(true);
  38. $output->writeln('<info>The specified user is enabled</info>');
  39. return 0;
  40. }
  41. /**
  42. * @param string $argumentName
  43. * @param CompletionContext $context
  44. * @return string[]
  45. */
  46. public function completeArgumentValues($argumentName, CompletionContext $context) {
  47. if ($argumentName === 'uid') {
  48. return array_map(
  49. static fn (IUser $user) => $user->getUID(),
  50. array_filter(
  51. $this->userManager->search($context->getCurrentWord()),
  52. static fn (IUser $user) => !$user->isEnabled()
  53. )
  54. );
  55. }
  56. return [];
  57. }
  58. }