diff options
| author | Marcus Eibrink-Lunzenauer <lunzenauer@elan-ev.de> | 2023-05-16 09:17:37 +0000 |
|---|---|---|
| committer | Jan-Hendrik Willms <tleilax+studip@gmail.com> | 2023-05-16 09:17:37 +0000 |
| commit | 1de91f34232717544892bfddd3314900edf91f3b (patch) | |
| tree | 4ec7e15e07499ffe631135c2ae36a7da4da54588 /cli/Commands/User/UsersDelete.php | |
| parent | 132e3454d06db698ca5c184050b4a4aadef9d574 (diff) | |
Add CLI scripts to get and set global configuration.
Closes #2588
Merge request studip/studip!1746
Diffstat (limited to 'cli/Commands/User/UsersDelete.php')
| -rw-r--r-- | cli/Commands/User/UsersDelete.php | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/cli/Commands/User/UsersDelete.php b/cli/Commands/User/UsersDelete.php new file mode 100644 index 0000000..33b41c3 --- /dev/null +++ b/cli/Commands/User/UsersDelete.php @@ -0,0 +1,89 @@ +<?php + +namespace Studip\Cli\Commands\User; + +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +class UsersDelete extends Command +{ + protected static $defaultName = 'user:delete'; + + protected function configure(): void + { + $this->setDescription('Delete users.'); + $this->setHelp('Delete multiple studip user accounts'); + $this->addArgument('range', InputArgument::REQUIRED, 'Username or path to csv-file'); + $this->addOption( + 'file_range', + 'f', + InputOption::VALUE_OPTIONAL, + 'Set to true, if you want use a txt file with username', + true + ); + $this->addOption('email', 'e', InputOption::VALUE_OPTIONAL, 'Send a deletion email', true); + $this->addOption( + 'delete_admins', + 'd', + InputOption::VALUE_OPTIONAL, + 'Admins can also be deleted on request', + false + ); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $range = $input->getArgument('range'); + $file_range = $input->getOption('file_range'); + $email = $input->getOption('email'); + $delete_admins = $input->getOption('delete_admins'); + + if ($email && !($MAIL_LOCALHOST && $MAIL_HOST_NAME && $ABSOLUTE_URI_STUDIP)) { + $output->writeln( + "<error>To use this script you MUST set correct values for $MAIL_LOCALHOST, $MAIL_HOST_NAME and $ABSOLUTE_URI_STUDIP in local.inc!</error>" + ); + } + + if (!(bool) $file_range) { + $usernames = [$range]; + } else { + if (!is_file($range)) { + $output->writeln(sprintf('<error>File not found: %s</error>', $range)); + return Command::FAILURE; + } + $file = fopen($range, 'r'); + $list = ''; + while (!feof($file)) { + $list .= fgets($file, 1024); + } + $usernames = preg_split('/[\s,;]+/', $list, -1, PREG_SPLIT_NO_EMPTY); + $usernames = array_unique($usernames); + } + + $users = \User::findBySQL('username IN (?)', [$usernames]); + + if (!empty($users)) { + foreach ($users as $user) { + if (!$delete_admins && ($user->perms == 'admin' || $user->perms == 'root')) { + $output->writeln(sprintf('User: %s is %s, NOT deleted', $user->username, $user->perms)); + } else { + $umanager = new \UserManagement($user->id); + //wenn keine Email gewünscht, Adresse aus den Daten löschen + if (!$email) { + $umanager->user_data['auth_user_md5.Email'] = ''; + } + if ($umanager->deleteUser()) { + $output->writeln(sprintf('<info>User: %s successfully deleted:</info>', $user->username)); + } else { + $output->writeln(sprintf('<error>User: %s NOT deleted</error>', $user->username)); + } + $output->writeln(parse_msg_to_clean_text($umanager->msg)); + } + } + } + return Command::SUCCESS; + } +} |
