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.

Get user's role name from user ID

edited July 2011 in Vanilla 2.0 - 2.8
I'm trying to write a simple plugin that will add a div before the user's name in a post which will be used to show an icon of my choosing.

Since the icon will come after the user's icon but before the name I need to override the UserAnchor function located in, /library/core/functions.render.php

The part I'm not sure how to do is the if statement. What is the best way to get the Role Name based on the comment that is being rendered?

class AdminIcon extends Gdn_Plugin { /** * Takes a user object, and writes out an achor of the user's name to the user's profile. */ if (!function_exists('UserAnchor')) { function UserAnchor($User, $CssClass = '') { if ($CssClass != '') $CssClass = ' class="'.$CssClass.'"'; //If admin append div if((RoleName->RoleID->($User->UserID)) == 'Administrator') { return '<div class="adminicon"></div><a href="'.Url('/profile/'.$User->UserID.'/'.urlencode($User->Name)).'"'.$CssClass.'>'.$User->Name.'</a>'; } else { return '<a href="'.Url('/profile/'.$User->UserID.'/'.urlencode($User->Name)).'"'.$CssClass.'>'.$User->Name.'</a>'; } } } }
Tagged:

Answers

  • lucluc ✭✭
    class AdminIcon extends Gdn_Pligin
    Plugin?

    That may be why it doesn't work as expected.
  • Why don't you use CSS instead of trying to move the element?

    There was an error rendering this rich post.

  • @luc: I fixed that miss spelling I just hadn't done so in my post
    @ddumount: I'm not moving any element. I'm adding another div that currently doesn't exist. This div will show between the icon and before the user's name

    I just need to get the logic for the IF statement such that when passing it a userid ($User->UserID) I will get an array of the Role names for that user ("Administartor", "Member", etc) so that I can test to see if they are an admin. If they are an admin then I will add the div (which will be styled in the css) so users know that the post is from an admin
  • LincLinc Detroit Admin
    I would think this is better done within your theme than a plugin. Modifying UserAnchor to add something unrelated to it seems like a kludge.
  • Can a theme tell if the users is an admin or not? If so whats the logic that would be used in the theme?
  • LincLinc Detroit Admin
    edited July 2011
    $Session = Gdn::Session();
    if (is_object($Session->User) && $Session->User->Admin == '1')
    echo "I'm an admin!";
    //edit: That's for detecting the superadmin who installed the software. Are you asking for discovering if they're in a role named 'Administrator'?
  • @Lincoln Yes I'm trying to determine if the author of the comment that calls UserAnchor is of the role 'Administrator'. This is how I think I would get there

    UserID->RoleID(s)->RoleNames

    This should have nothing to do with any session.

    Right now I've hacked to together to illustrate what I'm doing:
    http://www.gaslampgames.com/forum/discussion/632/logo-identification#Item_1

    if (!function_exists('UserAnchor')) { function UserAnchor($User, $CssClass = '') { if ($CssClass != '') $CssClass = ' class="'.$CssClass.'"'; if($User->UserID == '2'|| $User->UserID == '3' || $User->UserID == '5' || $User->UserID == '62'){ return '<div class="adminicon"></div><a href="'.Url('/profile/'.$User->UserID.'/'.urlencode($User->Name)).'"'.$CssClass.'>'.$User->Name.'</a>'; } else { return '<a href="'.Url('/profile/'.$User->UserID.'/'.urlencode($User->Name)).'"'.$CssClass.'>'.$User->Name.'</a>'; } }

    Obviously this is not a good way to do it, I should have it be more flexible in case new admins are added. Each of the users in the if statement have a RoleID which would give a RoleName of 'Administrator' I just don't know how to call up that information from within Vanilla.
  • Does somebody has any news on this? I am trying to determine what role(s) a user has. I only have it's UserID.
  • lucluc ✭✭
    edited August 2011
    From the tables, with that UserID, check the UserRole table to get the(s) RoleID, and then find the Name for that(those) RoleID in the Role table.

    All this found within a few minutes looking at applications/dashboard/settings/structure.php as I don't have a database set up where I am at now. Otherwise, you could have a look at your tables in your database.
  • edited August 2011
    I mentioned above that I could find the value I wanted in the database I was just looking for the code to get the name of the Role(s) for the current user. The first code block was just a guess and was non-working code.

    What I want to figure out is within Vanilla do I have to query the database to get the RoleIDs given a userID input or is there a function that can be called to do it? Also the same question given a roleID and wanting to get a role name in return.
  • lucluc ✭✭
    @deek: i was answering to kanduvisla.
  • sbenessbenes New
    edited September 2011
    I was looking to do something similar: show an outline around photos based on a user's role. I looked at the Role Badge plugin and found you can get the roles via this code which I put in my theme @ themes/mytheme/views/discussion/helper_functions.php .


    WriteComment() function
    code:

    $Author = UserBuilder($Object, 'Insert'); $userModel = new UserModel(); $roleData = $userModel->GetRoles( $Author->UserID ); if( $roleData !== FALSE && $roleData->NumRows(DATASET_TYPE_ARRAY) > 0 ) $roles = ConsolidateArrayValuesByKey( $roleData->Result(), 'Name' ); foreach( $roles as $role ) { //do something with each role }
  • LincLinc Detroit Admin
    Well done!
Sign In or Register to comment.