[英]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: 在Joomla 2.5.14中,我有一个获取当前用户组ID的脚本,如:
$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: 为此,我需要从viewlevels表中选择访问级别,如下所示:
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. 因此,例如,第17组可能会阅读AccessA,AccessB和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 首先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) 将获得用户匹配的组,包括通过继承(除非你使$ recursive为false)
https://github.com/joomla/joomla-cms/blob/master/libraries/joomla/access/access.php#L402 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. JAccess::getAuthorisedViewLevels($userId)
将为您提供允许用户作为数组查看的所有视图级别。
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.
在joomla总是试图让api为你做的工作,而不是与它战斗。
I think you just need to remove the periods from within the LIKE clause. 我想你只需要从LIKE子句中删除句点。 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%'";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.