XF 2.0 How to get permissions of current user in XF2

HQCoder

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

HQCoder

Formerly dev.vnn
thanks @Xon so much
That is a horrible way to-do it in XF1.x.

For XF1.x
PHP:
$visitor = XenForo_Visitor::getInstance();
$visitor['permissions'];
i change this code to my XF1 addon and it is not working
 
Last edited:

Xon

Well-known member
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
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
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
You no longer need to manually touch serializing permissions.

For global permission checks:
PHP:
/** var boolean $bool */
$bool = $user->hasPermission('general', 'username');
 

HQCoder

Formerly dev.vnn
You no longer need to manually touch serializing permissions.

For global permission checks:
PHP:
/** var boolean $bool */
$bool = $user->hasPermission('general', 'username');
great. thanks for the big support
 

SamJakob

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

Xon

Well-known member
Yes, my comment was around the dev pre-release builds IIRC. Or I just typo'ed it
 

mcatze

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