Security

Get current user (and their roles) from SplunkJS

rmearkle
Explorer

I am creating a custom dashboard that will be distributed to a wide range of people with varying levels of access. I have found this code to get the current user:

require([ 'underscore', 'jquery', 'splunkjs/mvc', ],
       function(_, $, mvc ) {        
          var tokens = mvc.Components.getInstance("default");       
          var current=Splunk.util.getConfigValue("USERNAME");        
          tokens.set("currentuser", current);
       });

I also need to be able to get user roles, so that I can dynamically set properties in my D3 chart on this dashboard.

The only alternative I have found is creating what could become dozens of different dashboards with set permissions, which is not really acceptable as there will be so many different tiers under different branches.

loggar
Explorer

It can be done much easier...

                    var service = mvc.createService();
                service.currentUser(function(err, user) {
                    //user real name
                    real_name = user.properties().realname

                    //user name
                    usr = user.name

                    //user roles
                    roles = user.properties().roles
                });

rmearkle
Explorer

Well, answered my own question. Using the REST function I was able to get the roles and parse them inside my Javascript.

var userSearch = new SearchManager({
                    "id": "userSearch",
                    "preview": "false",
                    "cache": "false",
                    "search": "|rest splunk_server=local /services/authentication/current-context |table roles |mvexpand roles"
                }, {tokens: false});


    userSearch.startSearch();
    var roleSearchResults = userSearch.data('results');
    roleSearchResults.on("data", function() {   
        var userRoles = roleSearchResults.data().rows;          
        for(var i=0; i<userRoles.length; i++){
            console.log(userRoles[i][0]);               
        }

    });

This code will log the string values of all the roles for the current user to the console.

krishnarajapant
Path Finder

Hi rmearkle,

I have similar kind of requirement.

How can we call this entire code in a function?

How can use this code to restrict few inputs buttons as invisible?

-Krishna Rajapantula

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas     Cisco Live 2026 is almost here, and this ...

What Is the Name of the USB Key Inserted by Bob Smith? (BOTS Hint, Not the Answer)

Hello Splunkers,   So you searched, “what is the name of the usb key inserted by bob smith?”  Not gonna lie… ...

Automating Threat Operations and Threat Hunting with Recorded Future

    Automating Threat Operations and Threat Hunting with Recorded Future June 29, 2026 | Register   Is your ...