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

getting the permissions of a non visitorUser

Discussion in 'XenForo Development Discussions' started by tenants, Aug 18, 2012.

  1. tenants

    tenants Well-Known Member

    I want to get the permissions (added to the development Permission Definitions) of the thread creator (rather than the visitingUser).

    I'm doing this within a Template_Hook

    For this, can I use

    Code:
    $threadCreatorPerms= XenForo_Model::create('XenForo_Model_Permission')->getPermissionCombinationByUserId($threadCreatorUserId);
    and then
    Code:
    $perms['somePerm'] = (XenForo_Permission::hasPermission(XenForo_Permission::unserializePermissions($threadCreatorPerms['cache_value']), 'myMod',  'somePerm') ? true : false);
    I ask this, since getPermissionCombinationByUserId quries a table and returns the permisions from a column labeled "cache_value" possibly meaning that they wont always be upto date. When is the cache_value updated?

    Usually, for the viewing user you can just use something like this:

    Code:
    class MyMod_Model_Perms extends XenForo_Model
    {
        public function getPermissions(array $viewingUser = null)
        {
            $this->standardizeViewingUserReference($viewingUser);
     
            $perms['somePerm'] = (XenForo_Permission::hasPermission($viewingUser['permissions'], 'myMod',  'somePerm') ? true : false);
     
            return $perms;
        }
    }
    but for the thread creator, we don't have cached user values (obviously, since having this for each thread would completely bloat the application)

    Is there a better way of doing this, or should I use the above 1st method mentioned to get the thread creators permissions?
     
  2. tenants

    tenants Well-Known Member

    This method is not reliable... not all the users are present in the xf_permission_combination ...

    There must be a way of getting the permission of a user (that is not the viewer)

    This method doesn't curretly work:

    Code:
    $user= XenForo_Model::create('XenForo_Model_User')->getUserById($user_id);
    $threadCreatorPerms = XenForo_Model::create('MyMod_Model_Perms')->getPermissions($user);
    since "Argument 1 passed to XenForo_Permission::hasPermission() must be an array, null given"

    IE, the getUserById function does not populate the $user['permissions']
     
  3. ragtek

    ragtek Guest

    try
    PHP:
    $user $userModel->getUserById($user_id, array(
    'join' => XenForo_Model_User::FETCH_USER_PERMISSIONS
    ));
     
    tenants and Jake Bunce like this.
  4. tenants

    tenants Well-Known Member

    Wow... that's extremely helpful.

    Thanks ragtek

    just to add..(if anyone else needs this), you'll still need to unserialize somewhere:

    Code:
            if(!array_key_exists('permissions', $viewingUser)){
                $viewingUser['permissions'] = XenForo_Permission::unserializePermissions($viewingUser['global_permission_cache']);
                }
            
     

Share This Page