Archive
Highlighted

Retrieving the user role in an HTML dashboard via JS

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
Highlighted

Re: Retrieving the user role in an HTML dashboard via JS

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

Re: Retrieving the user role in an HTML dashboard via JS

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
Highlighted

Re: Retrieving the user role in an HTML dashboard via JS

SplunkTrust
SplunkTrust

http://pastebin.com/d6unPuzG

Should get you started.

Highlighted

Re: Retrieving the user role in an HTML dashboard via JS

Communicator

@alacercogitatus : Some typo there :

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

🙂

Highlighted

Re: Retrieving the user role in an HTML dashboard via JS

SplunkTrust
SplunkTrust

Good Catch, thanks!

0 Karma
Highlighted

Re: Retrieving the user role in an HTML dashboard via JS

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