Back to RemoveUserFromGroupCommand class

Method doExecute

protected int
doExecute
(\Symfony\Component\Console\Input\InputInterface $input, \Symfony\Component\Console\Output\OutputInterface $output)
Internal function to execute the command.
Parameters
  • \Symfony\Component\Console\Input\InputInterface $input The input to inject into the command.
  • \Symfony\Component\Console\Output\OutputInterface $output The output to inject into the command.
Returns
  • int The command exit code
Since
  • 4.0.0

Method doExecute - Source code

/**
 * 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->username = $this->getStringFromOption('username', 'Please enter a username');
    $this->ioStyle->title('Remove user from group');
    $userId = UserHelper::getUserId($this->username);
    if (empty($userId)) {
        $this->ioStyle->error("The user " . $this->username . " does not exist!");
        return 1;
    }
    $user = User::getInstance($userId);
    $this->userGroups = $this->getGroups($user);
    $db = Factory::getDbo();
    $query = $db->getQuery(true)->select($db->quoteName('title'))->from($db->quoteName('#__usergroups'))->where($db->quoteName('id') . ' = :userGroup');
    foreach ($this->userGroups as $userGroup) {
        $query->bind(':userGroup', $userGroup);
        $db->setQuery($query);
        $result = $db->loadResult();
        if (Access::checkGroup($userGroup, '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', $userGroup);
            $db->setQuery($queryUser);
            $activeSuperUser = $db->loadResult();
            if ($activeSuperUser < 2) {
                $this->ioStyle->error("Can't remove user '" . $user->username . "' from group '" . $result . "'! " . $result . " needs at least one active user!");
                return Command::FAILURE;
            }
        }
        if (\count(Access::getGroupsByUser($user->id, false)) < 2) {
            $this->ioStyle->error("Can't remove '" . $user->username . "' from group '" . $result . "'! Every user needs to be a member of at least one group");
            return Command::FAILURE;
        }
        if (!UserHelper::removeUserFromGroup($user->id, $userGroup)) {
            $this->ioStyle->error("Can't remove '" . $user->username . "' from group '" . $result . "'!");
            return Command::FAILURE;
        }
        $this->ioStyle->success("Removed '" . $user->username . "' from group '" . $result . "'!");
    }
    return Command::SUCCESS;
}