Delete.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Jens-Christian Fischer <jens-christian.fischer@switch.ch>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Core\Command\User;
  26. use OC\Core\Command\Base;
  27. use OCP\IUser;
  28. use OCP\IUserManager;
  29. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  30. use Symfony\Component\Console\Input\InputArgument;
  31. use Symfony\Component\Console\Input\InputInterface;
  32. use Symfony\Component\Console\Output\OutputInterface;
  33. class Delete extends Base {
  34. public function __construct(
  35. protected IUserManager $userManager,
  36. ) {
  37. parent::__construct();
  38. }
  39. protected function configure() {
  40. $this
  41. ->setName('user:delete')
  42. ->setDescription('deletes the specified user')
  43. ->addArgument(
  44. 'uid',
  45. InputArgument::REQUIRED,
  46. 'the username'
  47. );
  48. }
  49. protected function execute(InputInterface $input, OutputInterface $output): int {
  50. $user = $this->userManager->get($input->getArgument('uid'));
  51. if (is_null($user)) {
  52. $output->writeln('<error>User does not exist</error>');
  53. return 0;
  54. }
  55. if ($user->delete()) {
  56. $output->writeln('<info>The specified user was deleted</info>');
  57. return 0;
  58. }
  59. $output->writeln('<error>The specified user could not be deleted. Please check the logs.</error>');
  60. return 1;
  61. }
  62. /**
  63. * @param string $argumentName
  64. * @param CompletionContext $context
  65. * @return string[]
  66. */
  67. public function completeArgumentValues($argumentName, CompletionContext $context) {
  68. if ($argumentName === 'uid') {
  69. return array_map(static fn (IUser $user) => $user->getUID(), $this->userManager->search($context->getCurrentWord()));
  70. }
  71. return [];
  72. }
  73. }