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!

.conf24 | Registration Open!

Hello, hello! I come bearing good news: Registration for .conf24 is now open!   conf is Splunk’s rad annual ...

ICYMI - Check out the latest releases of Splunk Edge Processor

Splunk is pleased to announce the latest enhancements to Splunk Edge Processor.  HEC Receiver authorization ...

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...