1.多用户权限说明
在dispatcher.lua 添加函数
function remove_idx( tbl, index )
-- initiate variables for save procedure
local tables,lookup = { tbl },{ [tbl] = 1 }
for idx,t in ipairs( tables ) do
local thandled = {}
for i,v in ipairs( t ) do
thandled[i] = true
local stype = type( v )
-- only handle value
if stype == "table" then
if not lookup[v] then
table.insert( tables, v )
lookup[v] = #tables
end
else
if i == index then
t[i] = nil
return
end
end
end
for i,v in pairs( t ) do
-- escape handled values
if (not thandled[i]) then
local flag = 0
local stype = type( i )
-- handle index
if stype == "table" then
if not lookup[i] then
table.insert( tables,i )
lookup[i] = #tables
end
else
flag = 1
if i == index then
t[i] = nil
return
end
end
if flag == 1 then
stype = type( v )
-- handle value
if stype == "table" then
if not lookup[v] then
table.insert( tables,v )
lookup[v] = #tables
end
else
if i == index then
t[i] = nil
return
end
end
end
end
end
end
end
在dispatch()函数中下面代码后面
if c and c.index then
local tpl = require "luci.template"
if util.copcall(tpl.render, "indexer", {}) then
return true
end
end
插入如下代码
if ctx.authuser == "user" then
remove_idx(ctx.tree, "packages")
remove_idx(ctx.tree, "leds")
end
处理访问权限可以仿照下面的权限功能
--- auth check
local req_path=ctx.path
if #req_path == 3 then
logx.print_r(req_path)
logx.print_r(req_path[1])
logx.print_r(req_path[2])
if req_path[1] == 'admin' and req_path[2] == 'v6plus' then
for _, page in ipairs(acl) do
if req_path[3] == page then
http.status(403, "Forbidden")
return
end
end
end
end
luci 开发调试模块,在/usr/lib/lua/luci 中添加log.lua文件,代码如下
local M = {}
local tconcat = table.concat
local tinsert = table.insert
local srep = string.rep
local function local_print(str)
local dbg = io.open("/tmp/luci.output", "a+")
local str = str or ""
if dbg then
dbg:write(str..'\n')
dbg:close()
end
end
function M.print(...)
local dbg = io.open("/tmp/luci.output", "a+")
if dbg then
dbg:write(os.date("[%H:%M:%S]: "))
for _, o in ipairs({...}) do
dbg:write(tostring(o)..' ')
end
dbg:write("\n")
dbg:close()
end
end
function M.print_r(data, depth)
local depth = depth or 3
local cstring = "";
local top_flag = true
local function table_len(t)
local i = 0
for k, v in pairs(t) do
i = i + 1
end
return i
end
local function tableprint(data,cstring, local_depth)
if data == nil then
local_print("core.print data is nil");
end
local cs = cstring .. " ";
if top_flag then
local_print(cstring .."{");
top_flag = false
end
if(type(data)=="table") then
for k, v in pairs(data) do
if type(v) ~= "table" then
if type(v) == "string" then
local_print(cs..tostring(k).." = ".."'"..tostring(v).."'");
else
local_print(cs..tostring(k).." = "..tostring(v));
end
elseif table_len(v) == 0 then
local_print(cs..tostring(k).." = ".."{}")
elseif local_depth < depth then
local_print(cs..tostring(k).." = {");
tableprint(v,cs,local_depth+1);
else
local_print(cs..tostring(k).." = ".."{*}")
end
end
else
local_print(cs..tostring(data));
end
local_print(cstring .."}");
end
tableprint(data,cstring,0);
end
return M
代码调用功能
local logx = require "luci.log"
logx.print("hello world")
logx.print({"hello", "world")
查看调试内容
tail -f /tmp/luci.output
调试代码上传到相应位置后需要做的操作
删除/tmp/luci-indexcache 与 /tmp/luci-modulecache
rm /tmp/luci-*
刷新页面即完成相应功能