How to display the currently logged username into the Luci WebUi

I have configured 2x Users in rpcd file as below. My goal is to display the currently logged username into Luci WebUi. I checked the luci_username field used for getting input during the login and it is available in sysauth.htm and dispatch.lua files. But i am not sure about How to retrieve it and display on the WebUi. Could somebody help on this ?

config rpcd
	option socket /var/run/ubus.sock
	option timeout 30

config login
	option username 'admin'
	option password '$p$admin'
	list read '*'
	list write '*'

config login
	option username 'engineer'
	option password '$p$engineer'
	list read '*'
	list write '*'

You need to create a new ACL file:

root@er-x:~# cat /usr/share/rpcd/acl.d/allow-session-access.json
{
	"allow-session-access": {
		"description": "Allow access to login session information",
		"read": {
			"ubus": {
				"session": [ "get" ]
			}
		}
	}
}

Then, in the ui views you can do:

"require rpc";

var callSessionGet = rpc.declare({
    object: "session",
    method: "get",
    reject: true,
    expect: {
        values: {}
    }
});

callSessionGet().then(function(res) {
    alert("Logged in username is: " + res.username);
});

Alternatively you can determine the name on the server side, e.g. in .htm, templates:

<p>
   The current username is: 
   <strong><%= luci.dispatcher.context.authuser %></strong>
</p>

You could also add it to the global client side LuCI environment:

diff --git a/modules/luci-base/luasrc/view/header.htm b/modules/luci-base/luasrc/view/header.htm
index cffe9482ca..3f563d0af1 100644
--- a/modules/luci-base/luasrc/view/header.htm
+++ b/modules/luci-base/luasrc/view/header.htm
@@ -25,6 +25,7 @@
                documentroot   = luci.http.getenv("DOCUMENT_ROOT"),
                requestpath    = luci.dispatcher.context.requestpath,
                dispatchpath   = luci.dispatcher.context.path,
+               username       = luci.dispatcher.context.authuser,
                pollinterval   = luci.config.main.pollinterval or 5,
                ubuspath       = luci.config.main.ubuspath or '/ubus/',
                sessionid      = luci.dispatcher.context.authsession,

... and query it in views using L.env.username:

alert("The currently logged in user is: " + L.env.username);
1 Like

@jow , Thanks for your valuable support. I tried these methods and it working fine.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.