Dashboards & Visualizations

Retrieving the user role in an HTML dashboard via JS

smichalski
Explorer

Hi there,

does anybody know how to retrieve the user role inside the JS section of an HTML dashboard? The goal behind this is to display different content inside a dashboard, depending on the user who is logged in to Splunk Web.

I'm aware that it's possible to grab the current username via Splunk.util.getConfigValue("USERNAME");, but using the username instead of the role is beyond question. When searching the forum for an answer, I stumbled over references to the Splunk SDK, showing how to grab the userdata for an external script. But that's not what I'm looking for and using the SDK is not favored anyway. Any ideas? The solution needs to work on Splunk 6.0 and 6.1.

Best regards, Sven

0 Karma

alacercogitatus
SplunkTrust
SplunkTrust

I do this. Since you are using HTML/JS, I would setup a SearchManager that fires a search to leverage the Rest API to pull role data. Then you can use the results to base your role based data on. Here's a sample search to get you started:

| rest /services/authentication/current-context | search NOT ( username=admin OR username=splunk* ) | stats count by username roles |rename roles as role| fields - count

And now for something completely different

Merrily we Splunk Along, 
Splunk Along, Splunk Along,
Merrily we Splunk Along, 
creating graphs and views.

And everywhere that Data Went,
Data Went, Data Went,
Everywhere that Data Went,
Lines and Pies we drew.

smichalski
Explorer

Thanks again, alacercogitatus 🙂 I tried to use your code, but standing just at the beginning of JS programming, I don't know how to access the role-array outside of myResults. I searched through various tutorials and tried to solve this on my own, but couldn't find any solution. Basically, I'm looking for something like this:

var roles = new Array;
myResults.on("data", function() {
_.each(myResults.data().rows, function(column, rowCount) {
[...]

roles.push(role);

});
alert(roles[0]);
});
alert(roles[0]);

Currently, the second alert will return an "undefined" result. Any idea?

0 Karma

alacercogitatus
SplunkTrust
SplunkTrust

Good Catch, thanks!

0 Karma

Rocket66
Communicator

@alacercogitatus : Some typo there :

WRONG:
var mySearch = mySearch.data("results");
WORKING:
var myResults = mySearch.data("results");

🙂

alacercogitatus
SplunkTrust
SplunkTrust

http://pastebin.com/d6unPuzG

Should get you started.

smichalski
Explorer

Thanks alacercogitatus, this brings me one step closer. But how did you manage to pass the user role into an JS array or object? Passing a search-result to an array or object has been one of my unanswered questions for some time now ...

0 Karma
Get Updates on the Splunk Community!

More Ways To Control Your Costs With Archived Metrics | Register for Tech Talk

Tuesday, May 14, 2024  |  11AM PT / 2PM ET Register to Attend Join us for this Tech Talk and learn how to ...

.conf24 | Personalize your .conf experience with Learning Paths!

Personalize your .conf24 Experience Learning paths allow you to level up your skill sets and dive deeper ...

Threat Hunting Unlocked: How to Uplevel Your Threat Hunting With the PEAK Framework ...

WATCH NOWAs AI starts tackling low level alerts, it's more critical than ever to uplevel your threat hunting ...