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

How to add user group permission in my addon

#2
I suggest you study how library/Xenforo/ControllerAdmin/Permission.php does it and do similar.

In general if you want to know how to do something then just look at how the Xenforo controller code does it and that is usually your answer. ControllerPublic and ControllerAdmin are a gold mine of 'how-to'.
 
#4
It's in that source file. Take some time and study it. Once you understand how the code works you'll know what you need to do to implement it in your addon.
 

wang

Well-known member
#5
Hello.
I have a question.How to add user group permission in my addon?
Thanks.
You can add the permissions in the model and then call it. Or you can add it straight up, as I like to call it for lack of a better term.

PHP:
$visitor = XenForo_Visitor::getInstance();
      
        if (!XenForo_Permission::hasPermission($visitor['permissions'], 'forum', 'permissionName'))
        {
            return $this->responseNoPermission();
        }
The forum is the name of the permission group and permissionName is the name of your permission. This will add the custom permission under the Forum permission group. You can also create your own permission group, in which case, its name must be entered instead of the forum.

Then you can call your permission in template with this code.

HTML:
<xen:if is="{$visitor.permissions.forum.permissionName}">
your code to be executed goes here
</xen:if>
That would be the gist of it. I hope that it helped.
 

Jeremy

Well-known member
#6
To add permissions into your add-on, you need your site to be in debug mode. Then, navigate to Development > Permission Definitions in the ACP. Once there, you can create your own permission groups, permissions, and interface groups for your add-on. In code, you have to options to determine if the user has permission:

  1. XenForo_Visitor::hasPermission($group, $permission);

    Note, this function internally uses #2, it just simplifies the call. It also requires you to have the current instance of XenForo_Visitor.
  2. XenForo_Permission::hasPermission(
    array $permissions, $group, $permission)


    See wang's example.
You may also access permissions in the template using wang's example.