1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How can I check a user's permission?

Discussion in 'XenForo Development Discussions' started by AlexT, Apr 8, 2013.

  1. AlexT

    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?
  2. Russ

    Russ Well-Known Member

    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.
  3. AlexT

    AlexT Well-Known Member

    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:
  4. James

    James Well-Known Member

    Look at XenForo_Permission::hasPermission() to check for a specific permission.
  5. Teapot

    Teapot Well-Known Member

    Try something like this:

    if ($visitor->hasPermission('forum''viewModerated'))
    // do something
  6. AlexT

    AlexT Well-Known Member

    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.

    $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:

    $user['permissions'] = XenForo_Permission::unserializePermissions($user['global_permission_cache']);
  7. AlexT

    AlexT Well-Known Member

    Or, if querying the permission status for more than one user at once

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

Share This Page