Hide/Show Pocket Based on User Role

This discussion is related to the Pockets addon.

Is this possible? I'd like to use Pocket to insert ads but to hide them from administrators and moderators for example.

Comments

  • vrijvlindervrijvlinder Papillon-Sauvage MVP

    You are better off creating a plugin with a module for your adds and write it so if admin or an array of roles you can set up, then it returns nothing.

    It is sort of a reverse way of checking for permissions. Normally it would check to see if session is valid then show the content. Then by roles if permission x is the user then show content. So you can make a plugin that will prevent admin or mods from seeing it.

    However if you are admin you will not be able to see it either. You would need to create a special role that you would give the admin and mod on top of those roles.

    For example, admin minor , mod minor . The minor would be an extra role that you can customize certain permissions and make new rules for what they can or can't do.

    Adding more roles to the admin can create problems.

    Maybe the plugin can be more simple by using the ID of the Users you want to keep from viewing. Specific users instead of roles. I mean you know who is admin and mod so you can get their User ID and use that as the check to keep them from seeing.

    If session is valid and User Id is array x y z return. Maybe someone can build this for you.

  • if you purely want to hide via css. for admin and moderator roles. see the readme.

    here is a plugin attached to this thread.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

    vrijvlinderBleistivt
  • if you don't want to show it to moderator or admin, but everyone else.

    you could also modify any theme that uses default.master.tpl

    which would be better because you are not hiding via css.

    make a pocket. give it a name e.g. "samplepocketone" . DO NOT ENABLE the pocket.

    then add this to the location where you want the pocket.

      {if !CheckPermission('Garden.Moderation.Manage')}
              {pocket name="samplepocketone"}
             {/if}
    

    http://blog.vanillaforums.com/help/power-pockets-adding-custom-html-js-vanilla-forum/#more-6244

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

    Bleistivtvrijvlinder
  • Thanks @peregrine‌, can I write an IF statement based on a particular user role, not just an admin? Say users who are 'Donators', as well as being a member or an admin, for example.

  • @Simeon_Griggs said:
    Thanks peregrine‌, can I write an IF statement based on a particular user role, not just an admin? Say users who are 'Donators', as well as being a member or an admin, for example.

    there is no "role" based permission statement.

    the easiest way is to find a permission setting that one role has vs. another role and play off the permission types.

    if "somerole" has a permission and another doesn't use that permission to check.

    or you could create new permission

    'RegisterPermissions' => array('Plugins.HidePocket.AllowView'),

    or create a new plugin called PocketHelper

    create a folder under plugins called PocketHelper

    plugins/PocketHelper/default.php

    this is the default.php

    <?php if (!defined('APPLICATION')) exit();
    
    $PluginInfo['PocketHelper'] = array(
       'Name' => 'Pocket Helper',
       'Description' => "PocketHelper adds a new permission to the permission table  called Allowview",
       'RegisterPermissions' => array('Plugins.PocketHelper.AllowView'),
       'Version' => '1.0',
       'MobileFriendly' => TRUE,
       'Author' => "Peregrine",
    );
    
    class PockertHelperPlugin extends Gdn_Plugin {
    
      public function Setup() { 
    }
    

    so you would enable the plugin via the dashboard. then you will see a checkbox for AllowView
    when you go into roles and permissions.

    then you can do

     {if !CheckPermission('Garden.Moderation.Manage')}
    {pocket name="samplepocketone"}
    {/if}
    

    // show the pocket if checkbox for AllowView is checked in dashboard roles and permissions.

     {if CheckPermission('Plugins.PocketHelper.Manage')}
        {pocket name="samplepocketB"}
        {/if}
    

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

    Bleistivtvrijvlinder
  • typo above should be

         {if CheckPermission('Plugins.PocketHelper.AllowView')}
        {pocket name="samplepocketB"}
        {/if}
    

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • peregrineperegrine MVP
    edited December 2014

    here is another way.

     <?php if (!defined('APPLICATION')) exit();
     $PluginInfo['CheckRoleSmarty'] = array(
    'Name' => 'Check Role Smarty',
    'Description' => "create smarty function to be used in smarty template",
    'Version' => '1.0',
    'MobileFriendly' => TRUE,
    'Author' => "Peregrine",
    );
    class CheckRoleSmartyPlugin extends Gdn_Plugin {
    
        public function Gdn_Smarty_init_handler($Sender) {
        $Sender->register_function('check_role', 'checkrole');
        }
        public function Setup() {
        }
    }
    
    
        function checkrole($Params, &$Smarty) {
        $RoleArray = Gdn::UserModel()->GetRoles(Gdn::Session()->UserID)->ResultArray();
        $Roles = ConsolidateArrayValuesByKey($RoleArray, 'Name');
        $Name = GetValue('name', $Params);
    
        if (InArrayI($Name, $Roles)) {  
        $Smarty->assign("Role", $Name);
        }
     }
    
    // peregrines notes.. :)
    // usage in default.master.tpl in theme template    
    // {check_role name="therolename"}
    // then test   {if $Role eq "therolename" }
    
      //  check_role - where name is exact Role Name 
      //  $Role is set check_role.
    
    
    //       {check_role name="Member"}
    //       {if $Role eq "Member" }
    //          {pocket name="Pocket A"}
    //        {/if}
    
    //        {check_role name="Donate"}
    //        {if $Role eq "Donate" }
    //          {pocket name="Pocket C"}
    //        {/if}
    
    
    
    //        {check_role name="Applicant"}
    //        {if $Role eq "Applicant" }
    //         hi applicant
    //        {/if}
    

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

    BleistivtSimeon_Griggsvrijvlinder
  • !!!

    Amazing, will give this a shot.

    BTW as I'm new to Vanilla, is there documentation somewhere to describe how you're building these plugins? I can't find something like a tag reference for Vanilla.

  • peregrineperegrine MVP
    edited December 2014

    @Simeon_Griggs said BTW as I'm new to Vanilla, is there documentation somewhere to describe how you're building these plugins? I can't find something like a tag reference for Vanilla.

    I just look around :wink:

    http://www.smarty.net/best_practices

    search forum also for "smarty"

    and I read the tutorials

    one by @R_J http://vanillaforums.org/discussion/27767/how-to-create-custom-smarty-functions

    and the faq
    http://vanillaforums.org/discussion/28420/faq

    and the documentation
    http://docs.vanillaforums.com/

    http://vanillaforums.org/discussion/comment/218833/#Comment_218833

    here is a zip of CheckRoleSmarty - it could probably be improved.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

    vrijvlinderAaronWebstey
  • Thanks for all that. Setup the plugin, totally works! Though I used an If/Else to make sure all roles other than donators see the ads.

    {check_role name="Donators"}
    {if $Role eq "Donators" }
    {else}
    {pocket name="Google Ad"}
    {/if}

    vrijvlinderperegrine
  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    My first intention has been that there should be a Smarty function to check for roles, but I guess it is the "wrong" way to do so. Using the above described permission solution is much more the Vanilla way. What roles are allowed to do is not defined in each individual plugin, but in the permission table.


    peregrine
  • peregrineperegrine MVP
    edited December 2014

    @R_J said:
    My first intention has been that there should be a Smarty function to check for roles, but I guess it is the "wrong" way to do so. Using the above described permission solution is much more the Vanilla way. What roles are allowed to do is not defined in each individual plugin, but in the permission table.

    I totally agree with you @r_j ideally, quicker and better to base conditions on role permissions.

    http://vanillaforums.org/discussion/comment/220937/#Comment_220937

    line 12 in linked comment should be class PocketHelperPlugin extends Gdn_Plugin {

    just wanted to throw out the capabilities of vanilla, interaction with smarty and template, creating separate permissions via a plugin, and hiding through css, as well as vrijvlinder's idea with modules.

    had to laugh at this. equivocally Unequivocal

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

    vrijvlinderR_J
  • vrijvlindervrijvlinder Papillon-Sauvage MVP

    Yes and Lol does not give points anymore ... or you should have 1 point

    peregrine
  • peregrineperegrine MVP
    edited December 2014

    @vrijvlinder said:

    Yes and Lol does not give points anymore ... or you should have 1 point

    I made you tied with me :) >:) o:)

    lol yes laughing out loud is pointless. in some circles.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

    vrijvlinder
  • vrijvlindervrijvlinder Papillon-Sauvage MVP

    Hahahaha that proves that Lol = 0 points :'(

    peregrine
  • Not related to Pocket but this new SmartRole plugin is super good for adding custom classes to your themes, example:

    class="{check_role name="Administrators"}{if $Role eq "Administrators" }Role_Administrators{/if}"

    :)

    vrijvlinderShadowdare
  • AaronWebsteyAaronWebstey Headband Afficionado Cole Harbour, NS ✭✭✭

    @peregrine you're the best. Finally getting back to do some work on my forum, needed this functionality, and boom. TYAP

    vrijvlinder
  • There are various solutions in this thread, which one should I use on 2.2 with Bootstrap ? I want to hide certain pockets for one specific role.

Sign In or Register to comment.