diff --git a/libraries/src/Console/AddUserCommand.php b/libraries/src/Console/AddUserCommand.php index 93c944641852a..5ca5eaf037fe6 100644 --- a/libraries/src/Console/AddUserCommand.php +++ b/libraries/src/Console/AddUserCommand.php @@ -238,35 +238,41 @@ protected function getUserGroups(): array $groupList = []; - // Group names have been supplied as input arguments + // Group values have been supplied if (!\is_null($groups) && $groups[0]) { $groups = explode(',', $groups); - foreach ($groups as $group) { - $groupId = $this->getGroupId($group); + foreach ($groups as $groupInput) { + $groupInput = trim($groupInput); + + // Try ID + if (ctype_digit($groupInput)) { + $groupId = $this->getGroupIdById((int) $groupInput); + } else { + $groupId = $this->getGroupIdByName($groupInput); + } if (empty($groupId)) { - $this->ioStyle->error("Invalid group name '" . $group . "'"); - throw new InvalidOptionException("Invalid group name " . $group); + $this->ioStyle->error("Invalid group: '" . $groupInput . "'"); + throw new InvalidOptionException("Invalid group: " . $groupInput); } - $groupList[] = $this->getGroupId($group); + $groupList[] = $groupId; } return $groupList; } - // Generate select list for user + // No group provided → prompt interactively $query = $db->getQuery(true) ->select($db->quoteName('title')) ->from($db->quoteName('#__usergroups')) ->order($db->quoteName('id') . 'ASC'); $db->setQuery($query); - $list = $db->loadColumn(); $choice = new ChoiceQuestion( - 'Please select a usergroup (separate multiple groups with a comma)', + 'Please select a usergroup (separate multiple groups with comma)', $list ); $choice->setMultiselect(true); @@ -274,12 +280,53 @@ protected function getUserGroups(): array $answer = (array) $this->ioStyle->askQuestion($choice); foreach ($answer as $group) { - $groupList[] = $this->getGroupId($group); + $groupList[] = $this->getGroupIdByName($group); } return $groupList; } + /** + * @param string $groupName + * + * @return int|null + * + * @since 6.0.0 + */ + protected function getGroupIdByName(string $groupName): ?int + { + $db = $this->getDatabase(); + + $query = $db->getQuery(true) + ->select($db->quoteName('id')) + ->from($db->quoteName('#__usergroups')) + ->where($db->quoteName('title') . ' = :groupName') + ->bind(':groupName', $groupName); + + $db->setQuery($query); + + return $db->loadResult() ?: null; + } + + /** + * @param int $groupId + * @return int|null + */ + protected function getGroupIdById(int $groupId): ?int + { + $db = $this->getDatabase(); + + $query = $db->getQuery(true) + ->select($db->quoteName('id')) + ->from($db->quoteName('#__usergroups')) + ->where($db->quoteName('id') . ' = :groupId') + ->bind(':groupId', $groupId); + + $db->setQuery($query); + + return $db->loadResult() ?: null; + } + /** * Configure the IO. *