|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Zikula package. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright Zikula Foundation - https://ziku.la/ |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Zikula\UsersModule\Command; |
|
15
|
|
|
|
|
16
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
17
|
|
|
use Symfony\Component\Console\Command\Command; |
|
18
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
19
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
20
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
21
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
22
|
|
|
use Zikula\UsersModule\Helper\DeleteHelper; |
|
23
|
|
|
|
|
24
|
|
|
class DeleteCommand extends Command |
|
25
|
|
|
{ |
|
26
|
|
|
protected static $defaultName = 'zikula:users:delete'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var DeleteHelper |
|
30
|
|
|
*/ |
|
31
|
|
|
private $deleteHelper; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct(DeleteHelper $deleteHelper) |
|
34
|
|
|
{ |
|
35
|
|
|
parent::__construct(); |
|
36
|
|
|
$this->deleteHelper = $deleteHelper; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
protected function configure() |
|
40
|
|
|
{ |
|
41
|
|
|
$this |
|
42
|
|
|
->addOption('uid', 'u', InputOption::VALUE_REQUIRED, 'User ID (int)') |
|
43
|
|
|
->addOption('gid', 'g', InputOption::VALUE_REQUIRED, 'Group ID (int)') |
|
44
|
|
|
->addOption('status', 's', InputOption::VALUE_REQUIRED, 'User activated status (A|I|P|M)') |
|
45
|
|
|
->addOption('date', 'd', InputOption::VALUE_REQUIRED, 'filter by user_regdate *before* YYYYMMDDHHMMSS') |
|
46
|
|
|
->setDescription('Delete one or more users') |
|
47
|
|
|
->setHelp( |
|
48
|
|
|
<<<'EOT' |
|
49
|
|
|
The <info>%command.name%</info> command deletes one or more users. |
|
50
|
|
|
This command dispatches all events and hooks like a standard user deletion. |
|
51
|
|
|
Do not use uid, gid or status simultaneously. Use only one |
|
52
|
|
|
|
|
53
|
|
|
<info>php %command.full_name% -u 478</info> |
|
54
|
|
|
|
|
55
|
|
|
This will delete user with uid 478. |
|
56
|
|
|
|
|
57
|
|
|
Options: |
|
58
|
|
|
<info>--uid</info> <comment>(int)</comment> delete one user by uid |
|
59
|
|
|
|
|
60
|
|
|
<info>--gid</info> <comment>(int)</comment> delete all users in group gid (does not delete the actual group) |
|
61
|
|
|
|
|
62
|
|
|
<info>--status</info> <comment>I|P|M</comment> delete users by status (active members cannot be deleted this way) (I=inactive, P=pending, M=marked for deletion) |
|
63
|
|
|
|
|
64
|
|
|
<info>--date</info> <comment>YYYYMMDDHHMMSS</comment> before deleting, filter user collection by date <comment>before</comment> this date. |
|
65
|
|
|
|
|
66
|
|
|
EOT |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
71
|
|
|
{ |
|
72
|
|
|
$io = new SymfonyStyle($input, $output); |
|
73
|
|
|
$params = [ |
|
74
|
|
|
'gid' => $input->getOption('gid'), |
|
75
|
|
|
'status' => $input->getOption('status'), |
|
76
|
|
|
'uid' => $input->getOption('uid'), |
|
77
|
|
|
]; |
|
78
|
|
|
$date = $input->getOption('date'); |
|
79
|
|
|
|
|
80
|
|
|
if (count(array_filter($params)) > 1) { |
|
81
|
|
|
$io->error('Do not use more than one option or argument.'); |
|
82
|
|
|
|
|
83
|
|
|
return 1; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$users = new ArrayCollection(); |
|
87
|
|
|
foreach ($params as $name => $value) { |
|
88
|
|
|
if (isset($value)) { |
|
89
|
|
|
try { |
|
90
|
|
|
$users = $this->deleteHelper->getUserCollection($name, $value, $date); |
|
91
|
|
|
} catch (\InvalidArgumentException $exception) { |
|
92
|
|
|
$io->error($exception->getMessage()); |
|
93
|
|
|
|
|
94
|
|
|
return 2; |
|
95
|
|
|
} |
|
96
|
|
|
break; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if ($users->isEmpty()) { |
|
101
|
|
|
$io->error('No users found!'); |
|
102
|
|
|
|
|
103
|
|
|
return 3; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$io->title('Zikula user deletion'); |
|
107
|
|
|
$count = count($users); |
|
108
|
|
|
$io->progressStart($count); |
|
109
|
|
|
foreach ($users as $user) { |
|
110
|
|
|
$this->deleteHelper->deleteUser($user); |
|
111
|
|
|
$io->progressAdvance(); |
|
112
|
|
|
} |
|
113
|
|
|
$io->progressFinish(); |
|
114
|
|
|
|
|
115
|
|
|
$io->success(sprintf('Success! %d users deleted.', $count)); |
|
116
|
|
|
|
|
117
|
|
|
return 0; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|