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!

Stay Connected: Your Guide to May Tech Talks, Office Hours, and Webinars!

Take a look below to explore our upcoming Community Office Hours, Tech Talks, and Webinars this month. This ...

They're back! Join the SplunkTrust and MVP at .conf24

With our highly anticipated annual conference, .conf, comes the fez-wearers you can trust! The SplunkTrust, as ...

Enterprise Security Content Update (ESCU) | New Releases

Last month, the Splunk Threat Research Team had two releases of new security content via the Enterprise ...