/**
* Internal function to execute the command.
*
* @param InputInterface $input The input to inject into the command.
* @param OutputInterface $output The output to inject into the command.
*
* @return integer The command exit code
*
* @since 4.0.0
*/
protected function doExecute(InputInterface $input, OutputInterface $output) : int
{
$this->configureIO($input, $output);
$this->ioStyle->title('Delete users');
$this->username = $this->getStringFromOption('username', 'Please enter a username');
$userId = UserHelper::getUserId($this->username);
$db = Factory::getDbo();
if (empty($userId)) {
$this->ioStyle->error($this->username . ' does not exist!');
return Command::FAILURE;
}
if ($input->isInteractive() && !$this->ioStyle->confirm('Are you sure you want to delete this user?', false)) {
$this->ioStyle->note('User not deleted');
return Command::SUCCESS;
}
$groups = UserHelper::getUserGroups($userId);
$user = User::getInstance($userId);
if ($user->block == 0) {
foreach ($groups as $groupId) {
if (Access::checkGroup($groupId, 'core.admin')) {
$queryUser = $db->getQuery(true);
$queryUser->select('COUNT(*)')->from($db->quoteName('#__users', 'u'))->leftJoin($db->quoteName('#__user_usergroup_map', 'g'), '(' . $db->quoteName('u.id') . ' = ' . $db->quoteName('g.user_id') . ')')->where($db->quoteName('g.group_id') . " = :groupId")->where($db->quoteName('u.block') . " = 0")->bind(':groupId', $groupId, ParameterType::INTEGER);
$db->setQuery($queryUser);
$activeSuperUser = $db->loadResult();
if ($activeSuperUser < 2) {
$this->ioStyle->error("You can't delete the last active Super User");
return Command::FAILURE;
}
}
}
}
// Trigger delete of user
$result = $user->delete();
if (!$result) {
$this->ioStyle->error("Can't remove " . $this->username . ' from usertable');
return Command::FAILURE;
}
$this->ioStyle->success('User ' . $this->username . ' deleted!');
return Command::SUCCESS;
}