role_permissions.lua 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. require("fwutils.webapi")
  2. -- CREATE TABLE `fw_role_permissions` (
  3. -- `id` int NOT NULL AUTO_INCREMENT,
  4. -- `path` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '路径',
  5. -- `role_id` int DEFAULT NULL COMMENT '角色',
  6. -- `action` varchar(2048) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '动作',
  7. -- `desc` varchar(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '描述',
  8. -- `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  9. -- `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  10. -- `delete_time` datetime DEFAULT NULL COMMENT '删除时间',
  11. -- PRIMARY KEY (`id`),
  12. -- UNIQUE KEY `path` (`path`,`role_id`)
  13. -- ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Fastweb-角色访问权限';
  14. local M = {}
  15. M.get_by_id = function(id,conn)
  16. local select = conn:select()
  17. select:table("fw_role_permissions LEFT JOIN fw_role ON fw_role_permissions.role_id = fw_role.id")
  18. select:field({
  19. "fw_role_permissions.id",
  20. "fw_role_permissions.path",
  21. "fw_role_permissions.role_id",
  22. "fw_role_permissions.action",
  23. "fw_role_permissions.`desc`",
  24. "fw_role_permissions.create_time",
  25. "fw_role_permissions.update_time",
  26. "fw_role_permissions.delete_time",
  27. "fw_role.title as role_title",
  28. "fw_role.id as role_id",
  29. })
  30. select:where_i32("fw_role_permissions.id","=",id)
  31. select:where_expression("AND fw_role_permissions.delete_time IS NULL")
  32. select:limit(0,1)
  33. local result = select:query()
  34. if result:row_count() == 0 then
  35. return nil
  36. end
  37. local d = result:table()[1]
  38. return d
  39. end
  40. M.add = function(data,conn)
  41. local insert = conn:insert()
  42. insert:table("fw_role_permissions")
  43. insert:set_str("path",data.path)
  44. insert:set_i32("role_id",data.role_id)
  45. if data.action ~= nil then
  46. insert:set_str("action",data.action)
  47. end
  48. if data.desc ~= nil then
  49. insert:set_str("`desc`",data.desc)
  50. end
  51. insert:set_not_ppst("create_time","NOW()")
  52. local d = insert:exec()
  53. return d == 1
  54. end
  55. M.update = function(data,conn)
  56. local update = conn:update()
  57. update:table("fw_role_permissions")
  58. if data.path ~= nil then
  59. update:set_str("path",data.path)
  60. end
  61. if data.action ~= nil then
  62. update:set_str("action",data.action)
  63. end
  64. if data.desc ~= nil then
  65. update:set_str("`desc`",data.desc)
  66. end
  67. if data.role_id ~= nil then
  68. update:set_i32("role_id",data.role_id)
  69. end
  70. update:set("update_time=NOW()")
  71. update:where_i32("id","=",data.id)
  72. local d = update:exec()
  73. return d == 1
  74. end
  75. M.delete = function(id,conn)
  76. local update = conn:update()
  77. update:table("fw_role_permissions")
  78. update:set("delete_time=NOW()")
  79. update:where_i32("id","=",id)
  80. local d = update:exec()
  81. return d == 1
  82. end
  83. M.list = function(search,limit,conn)
  84. if search.role_id == -1 then
  85. return {
  86. count = 0,
  87. data = {}
  88. }
  89. end
  90. return query_model_ex(conn,[=[
  91. fw_role_permissions LEFT JOIN fw_role ON fw_role_permissions.role_id = fw_role.id
  92. ]=],{
  93. "fw_role_permissions.id",
  94. "fw_role_permissions.path",
  95. "fw_role_permissions.role_id",
  96. "fw_role_permissions.action",
  97. "fw_role_permissions.`desc`",
  98. "fw_role_permissions.create_time",
  99. "fw_role_permissions.update_time",
  100. "fw_role_permissions.delete_time",
  101. "fw_role.title as role_title",
  102. "fw_role.id as role_id",
  103. },limit.start,limit.length,function(sel)
  104. if search.role_id ~= nil and search.role_id ~= 0 then
  105. sel:where_i32("fw_role_permissions.role_id","=",search.role_id)
  106. end
  107. sel:where_expression("AND fw_role_permissions.delete_time IS NULL")
  108. end,function(sel_data)
  109. sel_data:orderby("fw_role_permissions.create_time DESC")
  110. end)
  111. end
  112. -- 更新
  113. M.make_bytecode = function(role_id,conn)
  114. -- 查询权限表
  115. local select = conn:select()
  116. select:table("fw_role_permissions")
  117. select:where_expression("AND delete_time IS NULL")
  118. if role_id ~= nil then
  119. select:where_i32("role_id", "=", role_id)
  120. end
  121. local result = select:query()
  122. local bc = {}
  123. while result:next() do
  124. local id = result:get("id")
  125. local path = result:get("path")
  126. local role_id = tostring(result:get("role_id"))
  127. local action = result:get("action")
  128. local desc = result:get("desc")
  129. local create_time = result:get("create_time")
  130. local update_time = result:get("update_time")
  131. local delete_time = result:get("delete_time")
  132. -- local public = result:get("public")
  133. if bc[role_id] == nil then
  134. bc[role_id] = {}
  135. end
  136. if bc[role_id]["public"] == nil then
  137. bc[role_id]["public"] = {}
  138. end
  139. if bc[role_id]["private"] == nil then
  140. bc[role_id]["private"] = {}
  141. end
  142. -- 处理 action 字段,将其切分为表或空表
  143. local actions_tbl = {}
  144. if action and action ~= "" then
  145. for act in string.gmatch(action, "([^,]+)") do
  146. table.insert(actions_tbl, act)
  147. end
  148. end
  149. local item = {
  150. create_time = create_time,
  151. update_time = update_time,
  152. delete_time = delete_time,
  153. action = actions_tbl,
  154. desc = desc,
  155. }
  156. -- if public == 1 then
  157. -- bc[role_id]["public"][path] = item
  158. -- else
  159. -- bc[role_id]["private"][path] = item
  160. -- end
  161. bc[role_id][path] = item
  162. end
  163. local code = "return " .. require("serpent").serialize(bc, {comment = false})
  164. utils.save_file(fw.website_dir().."/"..(fwutils_config.path.luabytecode:gsub("%.", "/")).."/acl_bc.lua",code)
  165. return true
  166. end
  167. -- 匹配
  168. M.match = function(cfg)
  169. local function match_path(path, patterns)
  170. -- print("[match_path] path:",path)
  171. for pattern, v in pairs(patterns) do
  172. -- 如果是正则(以^开头),用string.match,否则精确匹配
  173. if string.sub(pattern, 1, 1) == "^" then
  174. if string.match(path, pattern) then
  175. -- print("[TRUE] pattern:",pattern,",path:",path)
  176. return true, v
  177. -- else
  178. -- print("[FALSE] pattern:",pattern,",path:",path)
  179. end
  180. else
  181. if path == pattern then
  182. return true, v
  183. end
  184. end
  185. end
  186. return false, nil
  187. end
  188. -- 检查action
  189. local function check_action(actions,action)
  190. if actions == nil or #actions == 0 then
  191. return true
  192. end
  193. for _,v in pairs(actions) do
  194. if v == action then
  195. return true
  196. end
  197. end
  198. return false, "action not match"
  199. end
  200. local role_id_str = string.format("%d",cfg.role_id())
  201. local acl_bc = require(fwutils_config.path.luabytecode..".acl_bc")
  202. if acl_bc[role_id_str] == nil then
  203. return false,"role id("..role_id_str..") acl not found"
  204. end
  205. local result, item = match_path(cfg.filepath(), acl_bc[role_id_str])
  206. if result then
  207. return check_action(item.action,cfg.action())
  208. end
  209. return false,"path("..cfg.filepath()..") acl not found"
  210. end
  211. return M