Hi everyone
I recently started contacting OpenWrt, and I couldn’t find any information about OpenWrt repeated login to the LuCI website with the same account. What I want to implement is the second login person. After logging in, the LuCI website automatically logs out the first user. Please give me some help or ideas, thank you
The easy answer is, you can't.
There is no medium complexity answer.
The hard answer would be, the source is open (and rpcd
paves the way for more fine grained ACL
s) - so the sky and your own abilities as a developer are the limit.
Thanks for replying, I am currently trying to grab all logged-in sessions in the dispatcher.lua file, using luci.util.ubus("session", "destroy", {ubus_rpc_session = sid }) to destroy other logged-in sessions
I tried this for three days, but it still didn’t work. I really hope to solve this problem.
Looping over session list and calling session destroy on each sans the current one is the proper approach. Maybe it helps if you post the actual code you've tried, preferably as unified diff.
Hi Jow
Thank you for your reply, I have already done it, I am really happy, thank you!
My code:
local dsp = require "luci.dispatcher"
local utl = require "luci.util"
local sid = dsp.context.authsession
local sessionList = luci.sys.exec("ubus call session list | grep 'ubus_rpc_session' | awk {'print substr($2, 2,length($2)-2)'}")
local sessArr = string.split(sessionList, '"')
for i, v in ipairs(sessArr) do
if (i < #sessArr ) then
local res = string.gsub(v,"^[ \t\n\r]+","")
if (res ~= sid) then
luci.sys.exec("ubus call session destroy '{ \"ubus_rpc_session\" : \""..res.."\"}'")
end
end
end
I hope this code is just a proof of concept and does not go into production like that. It is one of the least efficient potential implementation of your solution.
You should use the native ubus binding and not shell out and using grep/awk pipelines (which can be easily replaced with Lua pattern matching even when shelling out).
A more efficient solution should look like the untested code below:
local sessions = { utl.ubus("session", "list") }
for _, session in ipairs(sessions) do
if session.ubus_rpc_session ~= sid then
utl.ubus("session", "destroy", { ubus_rpc_session = session.ubus_rpc_session })
end
end
Hi jow
Thank you for your suggestion, I will try to modify it again,thanks