Admin permissions in templates

Nerbert

Active member
How do I access admin permissions in admin templates? I've tried guessing. I've tried searching for examples in templates. I've even tried searching here.
 
I don't believe you can, at least not directly. You would generally setup the permission check in your controller code.
 
It seems those permissions should always be available in every admin page. Somewhere in XenForo/Model/Admin.php you could have a loop that generates values like this:
PHP:
$hasAdminPermission['attachments'] = $visitor->hasAdminPermission("attachments');
and have that available in $viewParams for every controller file.

I managed to do it with an event listener for tempate_create:
PHP:
    public static function template_create(&$templateName, array &$params, XenForo_Template_Abstract $template)
    {
        if($templateName == "user_edit")
        {
            $visitor = XenForo_Visitor::getInstance();
            $params['canManageAttachments'] = ($visitor->hasAdminPermission('attachment')
                && $visitor->hasPermission('attachmentGallery','canViewOthersGallery')
            );
            //echo '<pre>';print_r($params);echo '</pre>';
        }
    }
It checks both the admin perms for attachments and ordinary perms for the admin group
 
Last edited:
Be sure to test the code above using a test admin account. I noticed a misspelling after I posted the copied code. Super admins have all permissions, even

PHP:
XenForo::getInstance()->hasAdminPermission('make_stupid_spelling_mistakes')

returns true
 
Top Bottom