1. 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

Discussion in 'XenForo Development Discussions' started by Hugilore111, Dec 22, 2015.

  1. Hugilore111

    Hugilore111 Member

    I have a question.How to add user group permission in my addon?
  2. Erik P.

    Erik P. Member

    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'.
    wang likes this.
  3. Hugilore111

    Hugilore111 Member

    How to display the template?
  4. Erik P.

    Erik P. Member

    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.
  5. wang

    wang Well-Known Member

    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.

    $visitor XenForo_Visitor::getInstance();
            if (!
    XenForo_Permission::hasPermission($visitor['permissions'], 'forum''permissionName'))
    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.

    <xen:if is="{$visitor.permissions.forum.permissionName}">
    your code to be executed goes here
    That would be the gist of it. I hope that it helped.
    Dadparvar and Hugilore111 like this.
  6. Jeremy

    Jeremy Well-Known Member

    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.
    Dadparvar, wang and Hugilore111 like this.

Share This Page