API16

JAccess/getAuthorisedViewLevels

From Joomla! Documentation

< API16:JAccess
Revision as of 17:34, 22 March 2010 by Doxiki (talk | contribs) (New page: ===Description=== Method to return a list of view levels for which the user is authorised. <span class="editsection" style="font-size:76%;"> <nowiki>[</nowiki>[[Description:JAccess/ge...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

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.

[Edit Descripton]

Template:Description:JAccess/getAuthorisedViewLevels

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;
}

[Edit See Also] Template:SeeAlso:JAccess/getAuthorisedViewLevels

Examples[edit]

<CodeExamplesForm />