Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Check for Permissions

So I searched through the documentation, and I can't seem to find anything on how to check if a user has "x" permission. I want to add my own link to the menu but only when the user has "x" permission and that's why I need this.

Comments

  • hgtonighthgtonight ∞ · New Moderator

    Are you adding the link through a plugin?

    A theme file?

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • @hgtonight said:
    Are you adding the link through a plugin?

    A theme file?

    Coding it through my theme I guess. Just looking for a way to check for the permission with the Garden Framework

  • hgtonighthgtonight ∞ · New Moderator
    edited October 2013

    Assuming you are using smarty, you can use the code below to check any permission:

    {if CheckPermission('Garden.Settings.Manage')}
      {dashboard_link}
    {/if}
    

    Anything between enclosed in the if statement will be shown only if the currently signed in user has the permission.

    For more information on Smarty tags, check out the wiki.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • @hgtonight said:
    Assuming you are using smarty, you can use the code below to check any permission:

    {if CheckPermission('Garden.Settings.Manage')}
      {dashboard_link}
    {/if}
    

    Anything between enclosed in the if statement will be shown only if the currently signed in user has the permission.

    For more information on Smarty tags, check out the wiki.

    Is smarty another plugin to install or is it built in?

  • hgtonighthgtonight ∞ · New Moderator

    A quote from the link I shared:

    VanillaWiki said:

    Vanilla supports smarty based templates as an alternative to PHP. All standard view files (*.php) support Smarty equivalents (*.tpl).

    It then goes on to talk about all the Smarty tags used in a standard master template.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • @hgtonight said:
    A quote from the link I shared:

    It then goes on to talk about all the Smarty tags used in a standard master template.

    So I need to make a smarty tpl to check for the permission? I was hoping for a basic way in PHP

  • ShadowdareShadowdare r_j MVP
    edited October 2013

    Vanilla 2.1+ uses Smarty, so you would have to use the default.master.tpl file for overriding the master template via the theme. Vanilla 2.0.18.8 uses default.master.php, so you can use PHP code in the master template for 2.0.18.8.

    You can also create a theme hooks file. Let's say your theme is called "example" and you want to create a theme hooks file. You have to create one manually and it requires that you enter the correct theme name in the file name and in the class name. Replace the word "example" below in the file name and in the class name below to match your theme's name, and when you're finished creating the file, clear the cache folder of Vanilla to make sure it gets loaded.

    /themes/example/class.examplethemehooks.php

    <?php if (!defined('APPLICATION')) exit();
    
    // Replace the word exampleThemeHooks with yourthemeThemeHooks
    class exampleThemeHooks extends Gdn_Plugin {
        public function Base_Render_Before($Sender) {
            if($Sender->Menu && $Sender->MasterView != 'admin') {
                // Example link for activity.
                // First parameter is link group name, second is link text, third is link path, and fourth is permission name.
                $Sender->Menu->AddLink('Activity2', T('Activity2'), '/activity', array('Garden.Moderation.Manage'));
            }
        }
    }
    
    

    Add Pages to Vanilla with the Basic Pages app

  • @Shadowdare said:

    I'm kind of using it in a slightly different way, so I was hoping there was a direct

    if('Garden.Moderation.Manage')
    //do this
    }else{
    do this
    }

    but from my understanding that would mean I need to download and upgrade to Vanilla 2.1 beta.

  • hgtonighthgtonight ∞ · New Moderator

    You can check permissions on a controller with $this->Permission('Permission.Name'). You can check permissions through the session object with Gdn::Session()->CheckPermission('Permission.Name').

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • @hgtonight said:
    You can check permissions on a controller with $this->Permission('Permission.Name'). You can check permissions through the session object with Gdn::Session()->CheckPermission('Permission.Name').

    I think that second one is exactly what I am looking for. Thank you.

Sign In or Register to comment.