|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace N98\Magento\Command\Admin\User; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class DeleteUserCommand |
|
12
|
|
|
*/ |
|
13
|
|
|
class DeleteUserCommand extends AbstractAdminUserCommand |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Configure |
|
17
|
|
|
*/ |
|
18
|
|
|
protected function configure() |
|
19
|
|
|
{ |
|
20
|
|
|
$this |
|
21
|
|
|
->setName('admin:user:delete') |
|
22
|
|
|
->addArgument('id', InputArgument::OPTIONAL, 'Username or Email') |
|
23
|
|
|
->addOption('force', 'f', InputOption::VALUE_NONE, 'Force') |
|
24
|
|
|
->setDescription('Delete the account of a adminhtml user.') |
|
25
|
|
|
; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param InputInterface $input |
|
30
|
|
|
* @param OutputInterface $output |
|
31
|
|
|
* @throws \Exception |
|
32
|
|
|
* @return int|void |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->detectMagento($output); |
|
37
|
|
|
if ($this->initMagento()) { |
|
38
|
|
|
$dialog = $this->getHelperSet()->get('dialog'); |
|
39
|
|
|
// Username |
|
40
|
|
|
if (($id = $input->getArgument('id')) == null) { |
|
41
|
|
|
$id = $dialog->ask($output, '<question>Username or Email:</question>'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$user = $this->userModel->loadByUsername($id); |
|
45
|
|
|
if (!$user->getId()) { |
|
46
|
|
|
$user = $this->userModel->load($id, 'email'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if (!$user->getId()) { |
|
50
|
|
|
$output->writeln('<error>User was not found</error>'); |
|
51
|
|
|
return; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$shouldRemove = $input->getOption('force'); |
|
55
|
|
|
if (!$shouldRemove) { |
|
56
|
|
|
$shouldRemove = $dialog->askConfirmation( |
|
57
|
|
|
$output, |
|
58
|
|
|
'<question>Are you sure?</question> <comment>[n]</comment>: ', |
|
59
|
|
|
false |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if ($shouldRemove) { |
|
64
|
|
|
try { |
|
65
|
|
|
$user->delete(); |
|
66
|
|
|
$output->writeln('<info>User was successfully deleted</info>'); |
|
67
|
|
|
} catch (\Exception $e) { |
|
68
|
|
|
$output->writeln('<error>' . $e->getMessage() . '</error>'); |
|
69
|
|
|
} |
|
70
|
|
|
} else { |
|
71
|
|
|
$output->writeln('<error>Aborting delete</error>'); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|