Security

Get current username from SplunkJS

lassel
Communicator

In a HTML dashboard, how can I determine the username of the current user?

I tried this, but it seems like there should be a simpler synchronous way to get that information:

require(["splunkjs/mvc"],
  function(mvc) {
        var service = mvc.createService({ owner: "nobody" });
        var username = "undefined";
        service.currentUser(function(err, user) {
             console.log("User name inside: ", user.name);
             username = user.properties().name;
        });
        console.log("User name after : ", username);
  });
1 Solution

Flynt
Splunk Employee
Splunk Employee

You can do the following -

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

Notice I set the token in the dashboard to current. You can log it here or do with it what you wish.

View solution in original post

clement
Explorer

I have converted my dashboard to HTML and I have removed the Launcher navigation bar on the top of my dashboard but would like to add hyperlink to allow user to change their password, could you advise how to utilize your codes to replace "UserID" with login username in html tag follows?

< a href="/manager/apps/authentication/changepassword/"UserID"?action=edit">Change Password

0 Karma

Flynt
Splunk Employee
Splunk Employee

Put your a where you want it to be, add an ID and don't add href. In my example I call it changepassword.

<a id="changepassword">Change Password</a>

Then at the bottom of your HTML (Right after DASHBOARD READY) add this -

    var current=Splunk.util.getConfigValue("USERNAME");     
    var sethref='{{SPLUNKWEB_URL_PREFIX}}/manager/YOURAPPNAME/authentication/changepassword/'+current+'?action=edit' 
   $("#changepassword").attr('href',sethref);

Make sure you change the YOURAPPNAME value in the sethref variable to be the app that the view is in.

This should create a link to the change password screen.

0 Karma

clement
Explorer

it works perfectly for me, thank you! Flynt.

0 Karma

Flynt
Splunk Employee
Splunk Employee

You can do the following -

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

Notice I set the token in the dashboard to current. You can log it here or do with it what you wish.

Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...