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

Adding a Permission to an Existing Addon Help

Discussion in 'XenForo Development Discussions' started by dougiemac, Jul 11, 2016.

  1. dougiemac

    dougiemac Member

    Admin, please move if this isn't the correct forum. Thank you.

    Preface:
    I'm a product manager that hasnt coded since college really (20 years ago) and I'm very rusty at coding.

    Objective: I want to add a view permission for an existing addon.

    What I've done so far:
    I've researched the forum and found some threads, but trying their suggestions / solutions hasnt netted a victory.

    I've created the permission in the ACP Permission Definitions but I need to add some snipet of code to the template.

    Questions:

    So that leads me to two questions.
    1. What template do I add the code in and where?
    2. Is there a code snipet that I can use to copy and paste?

    Thank you for any help.
     
  2. Liam W

    Liam W Well-Known Member

    Generally you'd add the code to the template and the PHP controller to prevent direct linking.

    Generally, around the link to the page, you'd add a permission check like follows:

    Code:
    <xen:if is="{$visitor.permissions.<permission_group_id>.<permission_id>}">
    //Link code.
    </xen:if>
    
    Then, in the controller (for the entire controller):

    PHP:

    <?php

    XXX_XXX_ControllerPublic_XXX 
    extends XenForo_ControllerPublic_Abstract
    {
       protected function 
    _preDispatch($action)
       {
          if (!
    XenForo_Visitor::getInstance()->hasPermission('<permission_group_id>''<permsision_id>'))
          {
             throw 
    $this->getNoPermissionResponseException();
          }
       }

       
    // Rest of controller
    }

    Or for a specific action:

    PHP:
    XXX_XXX_ControllerPublic_XXX extends XenForo_ControllerPublic_Abstract
    {
       public function 
    actionXXX()
       {
          if (!
    XenForo_Visitor::getInstance()->hasPermission('<permission_group_id>''<permsision_id>'))
          {
               return 
    $this->responseNoPermission();
          }

          
    // Rest of action
        
    }

        
    //rest of controller
    }
    Hope that makes sense :)

    Liam
     
    dougiemac likes this.
  3. dougiemac

    dougiemac Member

    Thanks @Liam W, I'll give it a shot!
     

Share This Page