API16

JAccess/getAuthorisedViewLevels

From Joomla! Documentation

< API16:JAccess

The "API16" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

Description[edit]

Method to return a list of view levels for which the user is authorised.


<! removed transcluded page call, red link never existed >

Syntax[edit]

static getAuthorisedViewLevels($userId)
Parameter Name Default Value Description
$userId Id of the user for which to get the list of authorised view levels.

Returns[edit]

array List of view levels for which the user is authorised.

Defined in[edit]

libraries/joomla/access/access.php

Importing[edit]

jimport( 'joomla.access.access' );

Source Body[edit]

public static function getAuthorisedViewLevels($userId)
{
        // Get all groups that the user is mapped to recursively.
        $groups = self::getGroupsByUser($userId);

        // Only load the view levels once.
        if (empty(self::$viewLevels)) {
                // Get a database object.
                $db     = JFactory::getDBO();

                // Build the base query.
                $query  = $db->getQuery(true);
                $query->select('id, rules');
                $query->from('`#__viewlevels`');

                // Set the query for execution.
                $db->setQuery((string) $query);

                // Build the view levels array.
                foreach ($db->loadAssocList() as $level) {
                        self::$viewLevels[$level['id']] = (array) json_decode($level['rules']);
                }
        }

        // Initialise the authorised array.
        $authorised = array(1);

        // Find the authorized levels.
        foreach (self::$viewLevels as $level => $rule) {
                foreach ($rule as $id) {
                        if (($id < 0) && (($id * -1) == $userId)) {
                                $authorised[] = $level;
                                break;
                        }
                        // Check to see if the group is mapped to the level.
                        elseif (($id >= 0) && in_array($id, $groups))
                        {
                                $authorised[] = $level;
                                break;
                        }
                }
        }

        return $authorised;
}


<! removed transcluded page call, red link never existed >

Examples[edit]

Code Examples[edit]