Back to Access class

Method getUsersByGroup

public static array
getUsersByGroup
(mixed $groupId, mixed $recursive = false)
Method to return a list of user Ids contained in a Group
Parameters
  • int $groupId The group Id
  • bool $recursive Recursively include all child groups (optional)
Returns
  • array
Since
  • 1.7.0
-
  • This method should move somewhere else
Class: Access
Project: Joomla

Method getUsersByGroup - Source code

/**
 * Method to return a list of user Ids contained in a Group
 *
 * @param   integer  $groupId    The group Id
 * @param   boolean  $recursive  Recursively include all child groups (optional)
 *
 * @return  array
 *
 * @since   1.7.0
 * @todo    This method should move somewhere else
 */
public static function getUsersByGroup($groupId, $recursive = false)
{
    // Cast as integer until method is typehinted.
    $groupId = (int) $groupId;
    // Get a database object.
    $db = Factory::getDbo();
    $test = $recursive ? ' >= ' : ' = ';
    // First find the users contained in the group
    $query = $db->getQuery(true)->select('DISTINCT(' . $db->quoteName('user_id') . ')')->from($db->quoteName('#__usergroups', 'ug1'))->join('INNER', $db->quoteName('#__usergroups', 'ug2'), $db->quoteName('ug2.lft') . $test . $db->quoteName('ug1.lft') . ' AND ' . $db->quoteName('ug1.rgt') . $test . $db->quoteName('ug2.rgt'))->join('INNER', $db->quoteName('#__user_usergroup_map', 'm'), $db->quoteName('ug2.id') . ' = ' . $db->quoteName('m.group_id'))->where($db->quoteName('ug1.id') . ' = :groupId')->bind(':groupId', $groupId, ParameterType::INTEGER);
    $db->setQuery($query);
    $result = $db->loadColumn();
    // Clean up any NULL values, just in case
    $result = ArrayHelper::toInteger($result);
    return $result;
}