Delete.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. /** @var IUserManager */
  35. protected $userManager;
  36. /**
  37. * @param IUserManager $userManager
  38. */
  39. public function __construct(IUserManager $userManager) {
  40. $this->userManager = $userManager;
  41. parent::__construct();
  42. }
  43. protected function configure() {
  44. $this
  45. ->setName('user:delete')
  46. ->setDescription('deletes the specified user')
  47. ->addArgument(
  48. 'uid',
  49. InputArgument::REQUIRED,
  50. 'the username'
  51. );
  52. }
  53. protected function execute(InputInterface $input, OutputInterface $output): int {
  54. $user = $this->userManager->get($input->getArgument('uid'));
  55. if (is_null($user)) {
  56. $output->writeln('<error>User does not exist</error>');
  57. return 0;
  58. }
  59. if ($user->delete()) {
  60. $output->writeln('<info>The specified user was deleted</info>');
  61. return 0;
  62. }
  63. $output->writeln('<error>The specified user could not be deleted. Please check the logs.</error>');
  64. return 1;
  65. }
  66. /**
  67. * @param string $argumentName
  68. * @param CompletionContext $context
  69. * @return string[]
  70. */
  71. public function completeArgumentValues($argumentName, CompletionContext $context) {
  72. if ($argumentName === 'uid') {
  73. return array_map(static fn (IUser $user) => $user->getUID(), $this->userManager->search($context->getCurrentWord()));
  74. }
  75. return [];
  76. }
  77. }