How can I check a user's permission?

AlexT

Well-known member
Say I create a list of all moderators who have a specific permission enabled (view moderated threads). I can get the list of moderators via the getContentModerators() and getAllGeneralModerators() methods in the Moderator Model.

What I don't know is how I can iterate through the list of moderators to find those moderators who have that specific permission. Any ideas?
 
Admin Panel -> Users -> Test Permissions -> Type in a user

A new link appears in the mod bar area where you can REVERT PERMISSIONS to get off that users permissions.
 
Russ, thank you, but what I meant was in a programmatic way.

Specifically, I am currently writing an add-on that should send out e-mails to all moderators who have the "view moderated threads" permission enabled. In vB, this was possible with a simple logical operation, like moderator.permissions2 && $this->registry->bf_misc_moderatorpermissions2['caneditvisitormessages']. Unfortunately, I am still learning to understand how xF is storing permissions and right now I am pretty clueless what would be the "official" way of doing it. :oops:
 
Try something like this:

PHP:
if ($visitor->hasPermission('forum', 'viewModerated'))
        {
            // do something
        }
 
Teapot, your code would work if you are checking permissions only for the user who is currently viewing your board.

I believe I found the solution.

PHP:
$userModel = XenForo_Model::create('XenForo_Model_User');
 
$user = $userModel->getUserById($user_id, array(
'join' => XenForo_Model_User::FETCH_USER_PERMISSIONS
));

From here.

And then you could do something like this:

PHP:
$user['permissions'] = XenForo_Permission::unserializePermissions($user['global_permission_cache']);
 
Or, if querying the permission status for more than one user at once

PHP:
$userModel = XenForo_Model::create('XenForo_Model_User');
 
$users = $userModel->getUserByIds($userIds, array(
'join' => XenForo_Model_User::FETCH_USER_PERMISSIONS
));
 
Top Bottom