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
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?
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:
0
Answers
That may be why it doesn't work as expected.
There was an error rendering this rich 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
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.
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.
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.
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 }