简体   繁体   中英

Get current user access level in Joomla 2.5

In Joomla 2.5.14, I have a script that gets the current user group id, like:

$groups = $user->get('groups');
foreach($groups as $group) {
    echo "<p>Your group ID is:" . $group . "</p>";
};

Now, I need to count the number of articles this user is able to read, given his access level.

For that, I need to select the access level from the viewlevels table, that looks like this:

id     title            rules JSON encoded access control.
1   Public      [1]
2   AccessA     [13,8,16,17]
3   AccesssF    [8]
4   AccessD     [14,8]
6   AccessB     [8,16,17]
7       AccessC     [8,17]

So, for example, Group 17 may read the articles in AccessA, AccessB and Access C.

I tried the following query, but it isn't selecting any rows:

$query="SELECT * FROM xmb9d_viewlevels WHERE rules LIKE '%.$group.%'";

How can I select all the acess levels for the current user group and then count the number of articles he's able to read?

Thanks for helping!

First https://github.com/joomla/joomla-cms/blob/master/libraries/joomla/access/access.php#L285

JAccess::getGroupsByUser($userId, $recursive = true)

Will get you the groups a user is matched to including via inheritance (unless you make $recursive false)

https://github.com/joomla/joomla-cms/blob/master/libraries/joomla/access/access.php#L402

JAccess::getAuthorisedViewLevels($userId) will give you all the view levels a user is allowed to see as an array.

If you do

$allowedViewLevels = JAccess::getAuthorisedViewLevels($userId);

if nothing else you could do

$implodedViewLevels = implode(',', $allowedViewLevels);

....

$query->select($db->quoteName('id'))
->from('#__content')
->where($db->quoteName('access') . 'IN (' . $implodedViewLevels . ')';
 ....

(not tested but you get the general idea). In joomla always try to let the api do the work for you rather than fight with it.

I think you just need to remove the periods from within the LIKE clause. Such as:

$query="SELECT * FROM xmb9d_viewlevels WHERE rules LIKE '%$group%'";

Also, to make it not specific to the database you should substitute #__ (Note: it's two underscores) for the table prefix:

$query="SELECT * FROM #__viewlevels WHERE rules LIKE '%$group%'";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM