Back to Access class

Method getGroupsByUser

public static array
getGroupsByUser
(mixed $userId, mixed $recursive = true)
Method to return a list of user groups mapped to a user. The returned list can optionally hold only the groups explicitly mapped to the user or all groups both explicitly mapped and inherited by the user.
Parameters
  • int $userId Id of the user for which to get the list of groups.
  • bool $recursive True to include inherited user groups.
Returns
  • array List of user group ids to which the user is mapped.
Since
  • 1.7.0
Class: Access
Project: Joomla

Method getGroupsByUser - Source code

/**
 * Method to return a list of user groups mapped to a user. The returned list can optionally hold
 * only the groups explicitly mapped to the user or all groups both explicitly mapped and inherited
 * by the user.
 *
 * @param   integer  $userId     Id of the user for which to get the list of groups.
 * @param   boolean  $recursive  True to include inherited user groups.
 *
 * @return  array    List of user group ids to which the user is mapped.
 *
 * @since   1.7.0
 */
public static function getGroupsByUser($userId, $recursive = true)
{
    // Cast as integer until method is typehinted.
    $userId = (int) $userId;
    // Creates a simple unique string for each parameter combination:
    $storeId = $userId . ':' . (int) $recursive;
    if (!isset(self::$groupsByUser[$storeId])) {
        // @todo: Uncouple this from ComponentHelper and allow for a configuration setting or value injection.
        $guestUsergroup = (int) ComponentHelper::getParams('com_users')->get('guest_usergroup', 1);
        // Guest user (if only the actually assigned group is requested)
        if (empty($userId) && !$recursive) {
            $result = array($guestUsergroup);
        } else {
            $db = Factory::getDbo();
            // Build the database query to get the rules for the asset.
            $query = $db->getQuery(true)->select($db->quoteName($recursive ? 'b.id' : 'a.id'));
            if (empty($userId)) {
                $query->from($db->quoteName('#__usergroups', 'a'))->where($db->quoteName('a.id') . ' = :guest')->bind(':guest', $guestUsergroup, ParameterType::INTEGER);
            } else {
                $query->from($db->quoteName('#__user_usergroup_map', 'map'))->where($db->quoteName('map.user_id') . ' = :userId')->join('LEFT', $db->quoteName('#__usergroups', 'a'), $db->quoteName('a.id') . ' = ' . $db->quoteName('map.group_id'))->bind(':userId', $userId, ParameterType::INTEGER);
            }
            // If we want the rules cascading up to the global asset node we need a self-join.
            if ($recursive) {
                $query->join('LEFT', $db->quoteName('#__usergroups', 'b'), $db->quoteName('b.lft') . ' <= ' . $db->quoteName('a.lft') . ' AND ' . $db->quoteName('b.rgt') . ' >= ' . $db->quoteName('a.rgt'));
            }
            // Execute the query and load the rules from the result.
            $db->setQuery($query);
            $result = $db->loadColumn();
            // Clean up any NULL or duplicate values, just in case
            $result = ArrayHelper::toInteger($result);
            if (empty($result)) {
                $result = array(1);
            } else {
                $result = array_unique($result);
            }
        }
        self::$groupsByUser[$storeId] = $result;
    }
    return self::$groupsByUser[$storeId];
}