You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Delete.php 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 Delete extends Base {
  16. public function __construct(
  17. protected IUserManager $userManager,
  18. ) {
  19. parent::__construct();
  20. }
  21. protected function configure() {
  22. $this
  23. ->setName('user:delete')
  24. ->setDescription('deletes 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 0;
  36. }
  37. if ($user->delete()) {
  38. $output->writeln('<info>The specified user was deleted</info>');
  39. return 0;
  40. }
  41. $output->writeln('<error>The specified user could not be deleted. Please check the logs.</error>');
  42. return 1;
  43. }
  44. /**
  45. * @param string $argumentName
  46. * @param CompletionContext $context
  47. * @return string[]
  48. */
  49. public function completeArgumentValues($argumentName, CompletionContext $context) {
  50. if ($argumentName === 'uid') {
  51. return array_map(static fn (IUser $user) => $user->getUID(), $this->userManager->search($context->getCurrentWord()));
  52. }
  53. return [];
  54. }
  55. }