Files
daydaytalk-fwutils/target/fwutils/acl.lua
2026-01-08 21:58:41 +08:00

117 lines
3.6 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
local utils = require("utils")
local fw = require("fastweb")
local config = require("fwutils.config")
local M = {}
-- 更新
M.update = function(role_id,conn)
-- 查询权限表
local select = conn:select()
select:table("fw_role_permissions")
select:where_expression("AND delete_time IS NULL")
if role_id ~= nil then
select:where_i32("role_id", "=", role_id)
end
local result = select:query()
local bc = {}
while result:next() do
local id = result:get("id")
local path = result:get("path")
local role_id = tostring(result:get("role_id"))
local action = result:get("action")
local desc = result:get("desc")
local create_time = result:get("create_time")
local update_time = result:get("update_time")
local delete_time = result:get("delete_time")
-- local public = result:get("public")
if bc[role_id] == nil then
bc[role_id] = {}
end
if bc[role_id]["public"] == nil then
bc[role_id]["public"] = {}
end
if bc[role_id]["private"] == nil then
bc[role_id]["private"] = {}
end
-- 处理 action 字段,将其切分为表或空表
local actions_tbl = {}
if action and action ~= "" then
for act in string.gmatch(action, "([^,]+)") do
table.insert(actions_tbl, act)
end
end
local item = {
create_time = create_time,
update_time = update_time,
delete_time = delete_time,
action = actions_tbl,
desc = desc,
}
-- if public == 1 then
-- bc[role_id]["public"][path] = item
-- else
-- bc[role_id]["private"][path] = item
-- end
bc[role_id][path] = item
end
local code = "return " .. require("serpent").serialize(bc, {comment = false})
utils.save_file(fw.website_dir().."/"..(config.path.luabytecode:gsub("%.", "/")).."/acl_bc.lua",code)
return true
end
-- 匹配
M.match = function(cfg)
local function match_path(path, patterns)
-- print("[match_path] path:",path)
for pattern, v in pairs(patterns) do
-- 如果是正则(以^开头用string.match否则精确匹配
if string.sub(pattern, 1, 1) == "^" then
if string.match(path, pattern) then
-- print("[TRUE] pattern:",pattern,",path:",path)
return true, v
-- else
-- print("[FALSE] pattern:",pattern,",path:",path)
end
else
if path == pattern then
return true, v
end
end
end
return false, nil
end
-- 检查action
local function check_action(actions,action)
if actions == nil or #actions == 0 then
return true
end
for _,v in pairs(actions) do
if v == action then
return true
end
end
return false, "action not match"
end
local role_id_str = string.format("%d",cfg.role_id())
local acl_bc = require(config.path.luabytecode..".acl_bc")
if acl_bc[role_id_str] == nil then
return false,"role id("..role_id_str..") acl not found"
end
local result, item = match_path(cfg.filepath(), acl_bc[role_id_str])
if result then
return check_action(item.action,cfg.action())
end
return false,"path("..cfg.filepath()..") acl not found"
end
return M