acl.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. local utils = require("utils")
  2. local fw = require("fastweb")
  3. local config = require("fwutils.config")
  4. local M = {}
  5. -- 更新
  6. M.update = function(role_id,conn)
  7. -- 查询权限表
  8. local select = conn:select()
  9. select:table("fw_role_permissions")
  10. select:where_expression("AND delete_time IS NULL")
  11. if role_id ~= nil then
  12. select:where_i32("role_id", "=", role_id)
  13. end
  14. local result = select:query()
  15. local bc = {}
  16. while result:next() do
  17. local id = result:get("id")
  18. local path = result:get("path")
  19. local role_id = tostring(result:get("role_id"))
  20. local action = result:get("action")
  21. local desc = result:get("desc")
  22. local create_time = result:get("create_time")
  23. local update_time = result:get("update_time")
  24. local delete_time = result:get("delete_time")
  25. -- local public = result:get("public")
  26. if bc[role_id] == nil then
  27. bc[role_id] = {}
  28. end
  29. if bc[role_id]["public"] == nil then
  30. bc[role_id]["public"] = {}
  31. end
  32. if bc[role_id]["private"] == nil then
  33. bc[role_id]["private"] = {}
  34. end
  35. -- 处理 action 字段,将其切分为表或空表
  36. local actions_tbl = {}
  37. if action and action ~= "" then
  38. for act in string.gmatch(action, "([^,]+)") do
  39. table.insert(actions_tbl, act)
  40. end
  41. end
  42. local item = {
  43. create_time = create_time,
  44. update_time = update_time,
  45. delete_time = delete_time,
  46. action = actions_tbl,
  47. desc = desc,
  48. }
  49. -- if public == 1 then
  50. -- bc[role_id]["public"][path] = item
  51. -- else
  52. -- bc[role_id]["private"][path] = item
  53. -- end
  54. bc[role_id][path] = item
  55. end
  56. local code = "return " .. require("serpent").serialize(bc, {comment = false})
  57. utils.save_file(fw.website_dir().."/"..(config.path.luabytecode:gsub("%.", "/")).."/acl_bc.lua",code)
  58. return true
  59. end
  60. -- 匹配
  61. M.match = function(cfg)
  62. local function match_path(path, patterns)
  63. -- print("[match_path] path:",path)
  64. for pattern, v in pairs(patterns) do
  65. -- 如果是正则(以^开头),用string.match,否则精确匹配
  66. if string.sub(pattern, 1, 1) == "^" then
  67. if string.match(path, pattern) then
  68. -- print("[TRUE] pattern:",pattern,",path:",path)
  69. return true, v
  70. -- else
  71. -- print("[FALSE] pattern:",pattern,",path:",path)
  72. end
  73. else
  74. if path == pattern then
  75. return true, v
  76. end
  77. end
  78. end
  79. return false, nil
  80. end
  81. -- 检查action
  82. local function check_action(actions,action)
  83. if actions == nil or #actions == 0 then
  84. return true
  85. end
  86. for _,v in pairs(actions) do
  87. if v == action then
  88. return true
  89. end
  90. end
  91. return false, "action not match"
  92. end
  93. local role_id_str = string.format("%d",cfg.role_id())
  94. local acl_bc = require(config.path.luabytecode..".acl_bc")
  95. if acl_bc[role_id_str] == nil then
  96. return false,"role id("..role_id_str..") acl not found"
  97. end
  98. local result, item = match_path(cfg.filepath(), acl_bc[role_id_str])
  99. if result then
  100. return check_action(item.action,cfg.action())
  101. end
  102. return false,"path("..cfg.filepath()..") acl not found"
  103. end
  104. return M