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

Conditional to Member Role

edited March 2014 in Vanilla 2.0 - 2.8

Hello,

Trying to make something conditional for certain roles.

For example, I want it to show only for "Administrators" and "Moderators" and exclude all users.

Is there any documentation on conditional tags for roles?

Thanks!

Comments

  • Options
    whu606whu606 I'm not a SuperHero; I just like wearing tights... MVP

    @OurWebMedia‌

    If you are talking about threads or categories, you can set custom permissions on a category, so that only certain roles can see and or contribute.

  • Options
    R_JR_J Ex-Fanboy Munich Admin

    If something should only be possible for special roles, they should have the right permission for it. If the permissions are set properly, you can check for them with CheckPermission: https://github.com/vanillaforums/Garden/search?q=checkpermission&ref=cmdform

  • Options

    Hello,

    I'm not trying to change permissions for threads or categories, but something in the sidebar.

    I am creating a behavior similar to a ticket system.

    In the sidebar, I have an option that allows you to assign a discussion thread to another user.

    The purpose of this is it becomes favourited / bookmarked by that users account when assigned.

    When they log in, they can see a "My List" (which displays their favorites, ie., assigned threads).

    Basically, I've taken an outside-the-box approach to using favourites for Admins / Moderators.

    I want this sidebar option to be available only to Admin and Moderators, not to other roles.

    Hope this explains what I am trying to achieve. R_J, thanks for the link, looking through these.

  • Options

    Hi there,

    Thought it might be easier to show you what I want to do.

    I want to take this excerpt of coding here (inside the sidebar) and make it conditional:

    IF this is an ADMIN OR MODERATOR, it shows:

    <div class="Box"> <h4><?php echo 'Assign User'; echo " (".$this->All_Users->NumRows().")"; ?></h4> <ul class="PanelInfo"> <?php foreach($this->Users->Result() as $User) { ?> <?php $jon = $User->UserID; $book = mysql_query("SELECT * FROM GDN_UserDiscussion WHERE DiscussionID = '$array[3]' AND UserID ='$jon'"); $bk = mysql_fetch_array($book); $bmark = $bk['Bookmarked']; ?> <li <?php if($discid == $User->UserID) { echo 'class=Active'; } ?>><strong><a href="?user=<?php echo $User->UserID; ?>"><?php if($bmark == 1) { echo '* '; } else {} ?><?php echo $User->Name; ?></a></strong><?php if($discid == $User->UserID) { echo '-'; } else { echo '+'; } ?></li> <?php } ?> </ul> </div>

    IF this is a USER (not ADMIN OR MODERATOR) it doesn't show in the sidebar.

    Hope this clarifies what I am trying to achieve. Again, thank you for your help.

  • Options

    Update:

    Did manage to find a way to make this conditional to "Administrators", but not "Moderators".

    Here's what I have:

    <?php if (is_object($Session->User) && $Session->User->Admin == '1') { ?> <div class="Box"> <h4><?php echo 'Assign User'; echo " (".$this->All_Users->NumRows().")"; ?></h4> <ul class="PanelInfo"> <?php foreach($this->Users->Result() as $User) { ?> <?php $jon = $User->UserID; $book = mysql_query("SELECT * FROM GDN_UserDiscussion WHERE DiscussionID = '$array[3]' AND UserID ='$jon'"); $bk = mysql_fetch_array($book); $bmark = $bk['Bookmarked']; ?> <li <?php if($discid == $User->UserID) { echo 'class=Active'; } ?>><strong><a href="?user=<?php echo $User->UserID; ?>"><?php if($bmark == 1) { echo '* '; } else {} ?><?php echo $User->Name; ?></a></strong><?php if($discid == $User->UserID) { echo '-'; } else { echo '+'; } ?></li> <?php } ?> </ul> </div> <?php } ?>

    Could I get any pointers or suggestions on how to make this also visible to Moderators to?

    Thanks!

  • Options
    ShadowdareShadowdare r_j MVP
    edited March 2014

    You can check if the user has the role or if the user has a certain permission. See this post: http://vanillaforums.org/discussion/19212/check-if-a-user-is-in-a-given-role

    Add Pages to Vanilla with the Basic Pages app

  • Options

    For anyone whom may be following this, I've written conditionally to the "UserID".

    This method worked as for some reason I couldn't get the User Role to be conditional.

    Here's what I've used (just in case someone out there has a similar question):

    <?php if (is_object($Session->User) && $Session->User->UserID == '1' OR $Session->User->UserID == '3') { ?> <div class="Box"> <h4><?php echo 'Assign User'; echo " (".$this->All_Users->NumRows().")"; ?></h4> <ul class="PanelInfo"> <?php foreach($this->Users->Result() as $User) { ?> <?php $jon = $User->UserID; $book = mysql_query("SELECT * FROM GDN_UserDiscussion WHERE DiscussionID = '$array[3]' AND UserID ='$jon'"); $bk = mysql_fetch_array($book); $bmark = $bk['Bookmarked']; ?> <li <?php if($discid == $User->UserID) { echo 'class=Active'; } ?>><strong><a href="?user=<?php echo $User->UserID; ?>"><?php if($bmark == 1) { echo '* '; } else {} ?><?php echo $User->Name; ?></a></strong><?php if($discid == $User->UserID) { echo '-'; } else { echo '+'; } ?></li> <?php } ?> </ul> </div> <?php } ?>

    For my setup, user ID for "Admin" is 1.
    For my setup, user ID for Moderator is "3".

    If I need to setup more Moderators I'd just include those by editing the file.

    Hope it helps.

  • Options

    Hi @Shadowdare - Thanks! The link was really helpful!!!

  • Options
    x00x00 MVP
    edited March 2014

    first don't use mysql_query it is depreciated, and the connection method for garden is PDO.

    Garden has a native query builder and wrapper round PDO database connection

    It is also not a good optimization, or a good use of SQL to be making a separate query per user as you look through the users, However I understand it is only two users

    Another point views aren't the appropriate. place for a lot of heavy logic, instead you should pass the view the data it needs, either through themehooks or plugins.

    Last I presume you know that if a view/module need to be changed you should copy it to your theme.

    grep is your friend.

  • Options
    R_JR_J Ex-Fanboy Munich Admin

    As you can see in the discussion shadowdare linked to, it is recommended to use the standard permission system for granting access to something. So do not give permission to users or mods in your code itself. Instead register a permission (https://github.com/vanillaforums/Garden/search?q=registerpermissions&type=Code) and then give that permission to admins and moderators. Afterwards check for a permission inside of your code.

  • Options

    Hello @x00,

    Thank you for all these pointers and feedback. This is all a learning experience for me.

    I will be making revisions to things in the morning but have come a long way since posting this.

    It's not easy to find documentation or walk-throughs on Vanilla as it would be WordPress or similar.

    @R_J had mentioned in another comment that I'd miss my mainstream documentation, I DO! :)

    @R_J thank you for the additional details. Finding that I can count on you to drop in and comment!

    So far the Vanilla community has been very welcoming and helpful - thank you!

  • Options

    What i suspect you really what to do is to add a custom permissions.

    in a plugin you can register a permission in $PluginInfo

    $PluginInfo['YouPlugin'] = array(
    ...
    'RegisterPermissions' => array('Plugins.YouPlugin.YourRole'),
    ...
    

    You check the permission like so:

    $Session->CheckPermission('Plugins.YouPlugin.YourRole');

    then you add the permission to the role.

    grep is your friend.

  • Options
    hgtonighthgtonight ∞ · New Moderator

    You might find this tutorial about creating a module helpful: http://vanillawiki.homebrewforums.net/index.php/How_to_Build_a_Simple_Module_and_Integrate_it_as_a_Plugin

    In this example, the permission check would happen before the module is even added to the controller. (In the Base_Render_Before method)

    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.

  • Options
    R_JR_J Ex-Fanboy Munich Admin

    You should use the frameworks power:


    GetByRole and GetBookmarkUsers return objects and it would be better to have array of user ids in order to a) join admins and mods and b) do a simple in_array check for each user id in order to print out the line the way you want to.
    I'm convinced there is either a php or Vanilla way to do so but I don't know. The last time I was hoping to find a Vanilla way of doing something more simple, I was pointed to functions.general.php. It is definitely worth looking at.

  • Options

    Hello everyone,

    So much information shared with me. Thanks a lot!

    Extremely helpful in my doing this.

    It's our intention to build a fully customized ticket system.

    Was first trying the "IssueTracker" plugin but it's a dud.

    Now working on finishing things in a custom manner, and using the power of it's own framework.

    Again, a big thanks to all of you! It's more than appreciated!

Sign In or Register to comment.