XF 2.0 How to get permissions of current user in XF2

HQCoder

Formerly dev.vnn
#1
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:

Xon

Well-known member
#2
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;
 

Xon

Well-known member
#4
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.
 

HQCoder

Formerly dev.vnn
#5
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
 

HQCoder

Formerly dev.vnn
#6
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');
 

Xon

Well-known member
#7
You no longer need to manually touch serializing permissions.

For global permission checks:
PHP:
/** var boolean $bool */
$bool = $user->hasPermission('general', 'username');
 
#9
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:

mcatze

Well-known member
#12
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