XF 2.0 How to get permissions of current user in XF2

HQCoder

Member
Hello Everyone,
in my xf1 addon i get current user permissions with the code
PHP:
            $currentUserId = XenForo_Visitor::getInstance()->getUserId();

            $currentUser = $this->_getUserModel()->getUserById($currentUserId, array(

                'join' => XenForo_Model_User::FETCH_USER_PERMISSIONS

            ));

            $currentUser['permissions'] = XenForo_Permission::unserializePermissions($currentUser['global_permission_cache']);

please tell me how to get it in xf2
 
Last edited:
That is a horrible way to-do it in XF1.x.

For XF1.x
PHP:
$visitor = XenForo_Visitor::getInstance();
$visitor['permissions'];

For XF2.x
PHP:
$visitor = \XF::app()->visitor();
$visitor->PermissionSet;
 
thanks @Xon so much

i change this code to my XF1 addon and it is not working
You might need to rename $visitor to $currentUser, or try var_dump($visitor['permissions']) to see what it contains. But this should contain the permissions, as this is how various permission check get the current user's global permissions.
 
You might need to rename $visitor to $currentUser, or try var_dump($visitor['permissions']) to see what it contains. But this should contain the permissions, as this is how various permission check get the current user's global permissions.
many thanks @Xon. it worked.
thanks you again
 
You might need to rename $visitor to $currentUser, or try var_dump($visitor['permissions']) to see what it contains. But this should contain the permissions, as this is how various permission check get the current user's global permissions.
a last question.
@Xon
please help me how to use it in xf2

Code:
XenForo_Permission::unserializePermissions($visitor['global_permission_cache'])
and
XenForo_Permission::hasPermission($permissions, 'general', 'username');
 
You no longer need to manually touch serializing permissions.

For global permission checks:
PHP:
/** var boolean $bool */
$bool = $user->hasPermission('general', 'username');
 
That is a horrible way to-do it in XF1.x.

For XF1.x
PHP:
$visitor = XenForo_Visitor::getInstance();
$visitor['permissions'];

For XF2.x
PHP:
$visitor = \XF::app()->visitor();
$visitor->PermissionSet;
Actually, for XenForo 2, it now appears to be:
PHP:
$visitor = \XF::visitor();
 
Last edited:
I want to use a new permission in a template. Can someone give me please a short instruction?

I create the permission in acp, but what should i do after that? I don't get it in xf2.
Should i create Entity or something else?
 
Top Bottom