Compare commits

...

6 Commits

24 changed files with 326 additions and 322 deletions

View File

@ -25,20 +25,22 @@ http {
#lua_ssl_verify_depth 3;
#在Nginx启动时执行的Lua代码块
lua_shared_dict dict_a 1m;
init_by_lua_block {
-- 定义一个全局变量
ngx.log(ngx.INFO, "Initializing global variable")
global_var = "Hello, Nginx with Lua!"
-- 初始化一个共享字典(需要 lua-shared-dict 模块)
local shared_dict = ngx.shared.dict_a
shared_dict:set("key", "value")
}
#初始化用户角色权限相关的共享内存
lua_shared_dict dict 10m;
#init_by_lua_block {
# -- 定义一个全局变量
# ngx.log(ngx.INFO, "Initializing global variable")
# global_var = "Hello, Nginx with Lua!"
# -- 初始化一个共享字典(需要 lua-shared-dict 模块)
# local shared_dict = ngx.shared.dict_a
# shared_dict:set("key", "value")
#}
#init_by_lua_block init_by_lua_file 只能初始化其中的一个,不能同时启用
#否则报错nginx: [emerg] "init_by_lua_file" directive is duplicate
#init_by_lua_file '/home/frankly/work/AuthPlatform/src/init.lua';
init_worker_by_lua_file '/home/frankly/work/AuthPlatform/src/init.lua';
server {
listen 9080;
server_name 127.0.0.1;
@ -60,7 +62,7 @@ http {
location /testRBAC {
content_by_lua_file '${APP_PATH}/src/test/testRBAC.lua';
}
location /cjson {
location /test {
content_by_lua_file '${APP_PATH}/src/test/test.lua';
}
location = /testSM {

View File

@ -24,12 +24,6 @@ location /api/system/departments {
content_by_lua_file '${APP_PATH}/src/api/system/department.lua';
}
#菜单信息数据接口
location /api/system/menus {
access_by_lua_file '${APP_PATH}/src/auth/jwt-auth.lua';
content_by_lua_file '${APP_PATH}/src/api/system/menu.lua';
}
#权限信息数据接口
location /api/system/permissions {
access_by_lua_file '${APP_PATH}/src/auth/jwt-auth.lua';

View File

@ -16,26 +16,31 @@ local routes = {
paths = { "/api/system/accounts" },
methods = { "GET" },
handler = systemAccount.getSystemAccounts,
metadata = "system::accounts::list",
},
{
paths = { "/api/system/accounts/:id" },
methods = { "GET" },
handler = systemAccount.getSystemAccount,
metadata = "system::accounts::view",
},
{
paths = { "/api/system/accounts" },
methods = { "POST" },
handler = systemAccount.addSystemAccount,
metadata = "system::accounts::add",
},
{
paths = { "/api/system/accounts/:id" },
methods = { "DELETE" },
handler = systemAccount.deleteSystemAccount,
metadata = "system::accounts::delete",
},
{
paths = { "/api/system/accounts/:id" },
methods = { "PUT" },
handler = systemAccount.updateSystemAccount,
metadata = "system::accounts::edit",
},
}

View File

@ -16,26 +16,31 @@ local routes = {
paths = { "/api/system/applications" },
methods = { "GET" },
handler = systemApplication.getSystemApplications,
metadata = "system::applications::list",
},
{
paths = { "/api/system/applications/:id" },
methods = { "GET" },
handler = systemApplication.getSystemApplication,
metadata = "system::applications::view",
},
{
paths = { "/api/system/applications" },
methods = { "POST" },
handler = systemApplication.addSystemApplication,
metadata = "system::applications::add",
},
{
paths = { "/api/system/applications/:id" },
methods = { "DELETE" },
handler = systemApplication.deleteSystemApplication,
metadata = "system::applications::delete",
},
{
paths = { "/api/system/applications/:id" },
methods = { "PUT" },
handler = systemApplication.updateSystemApplication,
metadata = "system::applications::edit",
},
}

View File

@ -16,26 +16,31 @@ local routes = {
paths = { "/api/system/departments" },
methods = { "GET" },
handler = systemDepartment.getSystemDepartments,
metadata = "system::departments::list",
},
{
paths = { "/api/system/departments/:id" },
methods = { "GET" },
handler = systemDepartment.getSystemDepartment,
metadata = "system::departments::view",
},
{
paths = { "/api/system/departments" },
methods = { "POST" },
handler = systemDepartment.addSystemDepartment,
metadata = "system::departments::add",
},
{
paths = { "/api/system/departments/:id" },
methods = { "DELETE" },
handler = systemDepartment.deleteSystemDepartment,
metadata = "system::departments::delete",
},
{
paths = { "/api/system/departments/:id" },
methods = { "PUT" },
handler = systemDepartment.updateSystemDepartment,
metadata = "system::departments::edit",
},
}

View File

@ -1,61 +0,0 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by admin.
--- DateTime: 2025/11/04 10:45
---
--解析url路由过滤库
local radix = require("resty.radixtree")
--数据表业务处理
local systemMenu = require("service.system.menu")
--定义相关路由前端接口url地址
local routes = {
--菜单相关路由接口
{
paths = { "/api/system/menus" },
methods = { "GET" },
handler = systemMenu.getSystemMenus,
},
{
paths = { "/api/system/menus/:id" },
methods = { "GET" },
handler = systemMenu.getSystemMenu,
},
{
paths = { "/api/system/menus" },
methods = { "POST" },
handler = systemMenu.addSystemMenu,
},
{
paths = { "/api/system/menus/:id" },
methods = { "DELETE" },
handler = systemMenu.deleteSystemMenu,
},
{
paths = { "/api/system/menus/:id" },
methods = { "PUT" },
handler = systemMenu.updateSystemMenu,
},
}
-- 初始化路由
local rx, err = radix.new(routes)
if not rx then
ngx.say("Not Found")
ngx.exit(ngx.HTTP_NOT_FOUND)
end
--获取访问的uri地址
local uri = ngx.var.uri
local opts = {
method = ngx.var.request_method,
matched = {}
}
-- 进行路由匹配和相关函数调用
local ok = rx:dispatch(uri, opts, opts.matched)
if not ok then
ngx.say("Not Found")
ngx.exit(ngx.HTTP_NOT_FOUND)
end

View File

@ -16,26 +16,31 @@ local routes = {
paths = { "/api/system/permissions" },
methods = { "GET" },
handler = systemPermission.getSystemPermissions,
metadata = "system::permissions::list",
},
{
paths = { "/api/system/permissions/:id" },
methods = { "GET" },
handler = systemPermission.getSystemPermission,
metadata = "system::permissions::view",
},
{
paths = { "/api/system/permissions" },
methods = { "POST" },
handler = systemPermission.addSystemPermission,
metadata = "system::permissions::add",
},
{
paths = { "/api/system/permissions/:id" },
methods = { "DELETE" },
handler = systemPermission.deleteSystemPermission,
metadata = "system::permissions::delete",
},
{
paths = { "/api/system/permission/:id" },
paths = { "/api/system/permissions/:id" },
methods = { "PUT" },
handler = systemPermission.updateSystemPermission,
metadata = "system::permissions::edit",
},
}

View File

@ -16,26 +16,31 @@ local routes = {
paths = { "/api/system/positions" },
methods = { "GET" },
handler = systemPosition.getSystemPositions,
metadata = "system::positions::list",
},
{
paths = { "/api/system/positions/:id" },
methods = { "GET" },
handler = systemPosition.getSystemPosition,
metadata = "system::positions::list",
},
{
paths = { "/api/system/positions" },
methods = { "POST" },
handler = systemPosition.addSystemPosition,
metadata = "system::positions::list",
},
{
paths = { "/api/system/positions/:id" },
methods = { "DELETE" },
handler = systemPosition.deleteSystemPosition,
metadata = "system::positions::list",
},
{
paths = { "/api/system/positions/:id" },
methods = { "PUT" },
handler = systemPosition.updateSystemPosition,
metadata = "system::positions::list",
},
}

View File

@ -16,26 +16,31 @@ local routes = {
paths = { "/api/system/roles" },
methods = { "GET" },
handler = systemRole.getSystemRoles,
metadata = "system::roles::list",
},
{
paths = { "/api/system/roles/:id" },
methods = { "GET" },
handler = systemRole.getSystemRole,
metadata = "system::roles::view",
},
{
paths = { "/api/system/roles" },
methods = { "POST" },
handler = systemRole.addSystemRole,
metadata = "system::roles::add",
},
{
paths = { "/api/system/roles/:id" },
methods = { "DELETE" },
handler = systemRole.deleteSystemRole,
metadata = "system::roles::delete",
},
{
paths = { "/api/system/roles/:id" },
methods = { "PUT" },
handler = systemRole.updateSystemRole,
metadata = "system::roles::edit",
},
}

View File

@ -15,26 +15,31 @@ local routes = {
paths = { "/api/system/users" },
methods = { "GET" },
handler = systemUser.getSystemUsers,
metadata = "system::users::list",
},
{
paths = { "/api/system/users/:id" },
methods = { "GET" },
handler = systemUser.getSystemUser,
metadata = "system::users::view",
},
{
paths = { "/api/system/users" },
methods = { "POST" },
handler = systemUser.addSystemUser,
metadata = "system::users::add",
},
{
paths = { "/api/system/users/:id" },
methods = { "DELETE" },
handler = systemUser.deleteSystemUser,
metadata = "system::users::delete",
},
{
paths = { "/api/system/users/:id" },
methods = { "PUT" },
handler = systemUser.updateSystemUser,
metadata = "system::users::edit",
},
}

View File

@ -1,6 +1,5 @@
local jwt = require "resty.jwt"
local cjson = require("cjson.safe")
local conf = require("config")
local jsonschema = require("jsonschema")
-- 定义一个JSON Schema
@ -34,7 +33,7 @@ end
--获取token的数据值
local token = string.sub(auth_header,8)
--校验令牌
local jwt_obj = jwt:verify(conf.secret_key, token)
local jwt_obj = jwt:verify(SYSTEM_CONFIG.secret_key, token)
--如果校验结果中的verified==false则表示令牌无效
if jwt_obj.verified == false then
ngx.log(ngx.WARN, "Invalid token: ".. jwt_obj.reason)
@ -49,5 +48,10 @@ if jwt_obj.payload.exp and ngx.time() > jwt_obj.payload.exp then
ngx.exit(ngx.HTTP_UNAUTHORIZED)
end
-- Access claims in the payload
local claims = verified.claims
-- write the uid variable
ngx.var.uid = jwt_obj.payload
--全部校验完成后,说明令牌有效,返回令牌数据
ngx.log(ngx.INFO, "令牌校验通过 JWT: " .. cjson.encode(jwt_obj))

View File

@ -2,9 +2,9 @@
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by admin.
--- DateTime: 2025/9/24 16:31
---
--- 配置文件配置信息
return {
SYSTEM_CONFIG = {
APP_ENV = "dev", -- dev/prod
locale = 'zh',
@ -18,7 +18,11 @@ return {
REDIS = {
HOST = "127.0.0.1", -- redis host
PORT = 6379, -- redis port
PASSWORD = nil -- redis password
PASSWORD = nil, -- redis password
POOL_MAX_IDLE_TIME = 10000,
POOL_TIMEOUT = 1000, -- pool timeout
POOL_SIZE = 20, -- pool size
TIMEOUT = 1000, -- timeout
},
-- 配置PostgresSQL数据库连接

View File

@ -1,91 +0,0 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by admin.
--- DateTime: 2025/11/04 15:06
--- 数据表模型文件
local helpers = require("share.helpers")
--引用使用的库文件
local model = require("share.model")
--创建一个数据表相关的模型
local menuModel = model:new('sys_menu')
local _M = {}
--判断菜单是否存在
local function isExistMenu(id)
--根据菜单id进行验证菜单是否存在
local code, res = menuModel:find(id)
if code ~= 0 then
return false
end
local num = 0
if res ~= nil then
num = table.getn(res)
end
--组织不存在返回错误
if num <= 0 then
return false
end
return true
end
-- 查询数据表中的所有菜单信息
function _M.getSystemMenus(pageNum, pageSize)
return menuModel:paginate(pageNum, pageSize)
end
--根据菜单id获取菜单信息
function _M.getSystemMenu(id)
return menuModel.find(id)
end
--增加菜单息到数据表
function _M.addSystemMenu(jsonData)
--解析json中的键和数据值
local menuid = jsonData['menu_id']
--根据菜单名称进行验证菜单是否存在
local code, res = menuModel:where("menu_id", "=", menuid):get()
if code ~= 0 then
return 0x000001,res
end
local num = 0
if res ~= nil then
num = table.getn(res)
end
--菜单存在时返回菜单已经存在
if num > 0 then
return 0x01000C, nil
end
--键值为id产生uuid数据值增加到json中
jsonData.id = helpers.getUuid()
-- 创建一个菜单
return menuModel:create(jsonData)
end
--删除菜单信息到数据表
function _M.deleteSystemDepartment(id)
--根据菜单id进行验证菜单是否存在
local ok = isExistMenu(id)
--菜单不存在则返回
if ok == false then
return 0x000001,nil
end
return menuModel:delete(id)
end
--更新菜单信息到数据表
function _M.updateSystemMenu(id, jsonData)
--根据菜单id进行验证菜单是否存在
local ok = isExistMenu(id)
--组织不存在则返回
if ok == false then
return 0x000001,nil
end
jsonData.update_time = ngx.time()
--对数据内容进行更新
return menuModel:where('menu_id', '=', id):update(jsonData)
end
return _M

View File

@ -9,10 +9,96 @@
--]]
print("init application...")
--判断程序是否加载权限数据
--local dict = ngx.shared.dict
--local load = dict:get("RBAC")
--if load then
-- return
--end
--只在第一个worker进程中执行一次
if ngx.worker.id() ~= 0 then
return
end
--初始化获取系统默认的用户权限为实现RBAC框架做权限数据准备
cjson = require "cjson"
local dict_a = ngx.shared.dict_a
local v = dict_a:get("abc")
if not v then
dict_a:set("abc", 9)
end
local function handler()
--引用使用的库文件
local Model = require("share.model")
--创建一个数据表相关的模型
local userModel = Model:new('sys_user')
--读取用户表、角色表和权限表中配置的权限数据
--获取数据表中的记录数
local code, res = userModel:count()
ngx.log(ngx.INFO, "user count:"..res)
local redis = require("resty.redis")
local red = redis:new()
-- 设置超时时间
red:set_timeout(conf.REDIS.TIMEOUT) -- 1秒
-- 连接到 Redis
local ok, err = red:connect(SYSTEM_CONFIG.REDIS.HOST, SYSTEM_CONFIG.REDIS.PORT)
if not ok then
ngx.log(ngx.ERR, "redis failed to connect: "..err)
return
end
--需要密码时对密码进行处理
if SYSTEM_CONFIG.REDIS.PASSWORD ~= nil then
local res, err = red:auth(SYSTEM_CONFIG.REDIS.PASSWORD)
if not res then
ngx.log(ngx.ERR, "redis failed to connect, password error: "..err)
return
end
end
-- 从连接池中获取连接
red:set_keepalive(SYSTEM_CONFIG.REDIS.POOL_MAX_IDLE_TIME, SYSTEM_CONFIG.REDIS.POOL_SIZE)
-- 设置 key-value
local ok, err = red:set("admin-system:user:add", "1")
if not ok then
ngx.log(ngx.ERR, "redis failed to set key: "..err)
return
end
local ok, err = red:set("admin-system:user:edit", "1")
if not ok then
ngx.log(ngx.ERR, "failed to set key: "..err)
return
end
local ok, err = red:set("admin-system:user:delete", "1")
if not ok then
ngx.log(ngx.ERR, "failed to set key: "..err)
return
end
local ok, err = red:set("admin-system:user:view", "1")
if not ok then
ngx.log(ngx.ERR, "failed to set key: "..err)
return
end
local ok, err = red:set("admin-system:user:list", "1")
if not ok then
ngx.log(ngx.ERR, "failed to set key: "..err)
return
end
ngx.log(ngx.INFO, "set key successfully")
--关闭redis连接
red:close()
--dict:set("RBAC", "1")
end
-- 设置定时器执行一次handler函数
local ok, err = ngx.timer.at(0, handler)
if not ok then
ngx.log(ngx.ERR, "failed to create timer")
return
end

View File

@ -95,7 +95,8 @@ function _M.logout()
--验证成功记录登出的日志信息
local userid = ret["body"]["payload"]["userid"]
local username = ret["body"]["payload"]["username"]
ngx.log(ngx.INFO, "userid:"..userid.." username:"..username.." logout system")
local rolename = ret["body"]["payload"]["username"]
ngx.log(ngx.INFO, "userid:"..userid.." username:"..username.." rolename:"..rolename.." logout system")
local result = resp:json(0, "用户退出系统成功")
resp:send(result)
end

View File

@ -1,76 +0,0 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by .
--- DateTime: 2025/11/04 14:35
--- 业务逻辑 对菜单数据表进行数据表业务处理
local resp = require("util.response")
local menuDao = require("dao.menu")
local validatorJson = require("validator.system.menu")
local cjson = require("cjson.safe")
local _M = {}
--获取所有菜单信息
function _M.getSystemMenus()
--获取页码和请求的数据量
local pageNum = ngx.var.pagenum or 1
local pageSize = ngx.var.pagesize or 10
local code,ret = menuDao.getSystemMenus(pageNum, pageSize)
local result = resp:json(code, ret)
resp:send(result)
end
--根据菜单id获取菜单信息
function _M.getSystemMenu(m)
local code,ret = menuDao.getSystemMenu(m.id)
local result = resp:json(code, ret)
resp:send(result)
end
--根据菜单id添加菜单信息
function _M.addSystemMenu()
--读取请求体的数据
ngx.req.read_body()
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok = validatorJson.validatorJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)
resp:send(result)
return
end
--ngx.say(body_data)
local code, ret = menuDao.addSystemMenu(cjson.decode(body_data))
local result = resp:json(code, ret)
resp:send(result)
end
--根据菜单id删除菜单信息
function _M.deleteSystemMenu(m)
local code, ret = menuDao.deleteSystemMenu(m.id)
local result = resp:json(code, ret)
resp:send(result)
end
--根据菜单id删除菜单信息
function _M.updateSystemMenu(m)
--读取请求体的数据
ngx.req.read_body()
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok = validatorJson.validatorJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)
resp:send(result)
return
end
local code, ret = menuDao.updateSystemMenu(m.id, cjson.decode(body_data))
local result = resp:json(code, ret)
resp:send(result)
end
return _M

View File

@ -23,8 +23,15 @@ local function getUserId()
return userid
end
--判断用户是都有权限使用接口
--local payload = ngx.var.uid
--获取所有用户信息
function _M.getSystemUsers()
function _M.getSystemUsers(m)
--获取登录的用户信息
local payload = ngx.var.uid
local metadata = m.metadata
ngx.log(ngx.INFO, "metadata value:"..metadata)
--获取页码和请求的数据量
--local args = ngx.req.get_uri_args()
local pageNum = ngx.var.pagenum or 1
@ -36,6 +43,11 @@ end
--根据用户id获取用户信息
function _M.getSystemUser(m)
--获取登录的用户信息
local payload = ngx.var.uid
local metadata = m.metadata
ngx.log(ngx.INFO, "metadata value:"..metadata)
local userid = getUserId()
if userid ~= m.id then
ngx.log(ngx.WARN, "用户与使用token中的用户id不一致")
@ -48,7 +60,7 @@ function _M.getSystemUser(m)
end
--根据用户id获取用户信息
function _M.addSystemUser()
function _M.addSystemUser(m)
--读取请求体的数据
ngx.req.read_body()
--获取请求数据

View File

@ -127,8 +127,7 @@ local function get_cookie(key)
end
local function get_local_time()
local config = require("config")
local time_zone = ngx.re.match(config.time_zone, "[0-9]+")
local time_zone = ngx.re.match(SYSTEM_CONFIG.time_zone, "[0-9]+")
if time_zone == nil then
local err = "not set time zone or format error, time zone should look like `+8:00` current is: " .. config.time_zone
ngx.log(ngx.ERR, err)

View File

@ -1,6 +1,4 @@
local Database = require('share.database')
local conf = require("config")
local helpers = require('share.helpers')
local implode = helpers.implode
local unique = helpers.unique
@ -12,28 +10,28 @@ local WRITE = 'WRITE'
local READ = 'READ'
local database_write = Database:new({
host = conf.POSTGRES.HOST,
port = conf.POSTGRES.PORT,
user = conf.POSTGRES.USERNAME,
password = conf.POSTGRES.PASSWORD,
database = conf.POSTGRES.DATABASE,
charset = conf.POSTGRES.CHARSET,
timeout = conf.POSTGRES.TIMEOUT,
db_pool_timeout = conf.POSTGRES.POOL_TIMEOUT,
db_pool_size = conf.POSTGRES.POOL_SIZE,
host = SYSTEM_CONFIG.POSTGRES.HOST,
port = SYSTEM_CONFIG.POSTGRES.PORT,
user = SYSTEM_CONFIG.POSTGRES.USERNAME,
password = SYSTEM_CONFIG.POSTGRES.PASSWORD,
database = SYSTEM_CONFIG.POSTGRES.DATABASE,
charset = SYSTEM_CONFIG.POSTGRES.CHARSET,
timeout = SYSTEM_CONFIG.POSTGRES.TIMEOUT,
db_pool_timeout = SYSTEM_CONFIG.POSTGRES.POOL_TIMEOUT,
db_pool_size = SYSTEM_CONFIG.POSTGRES.POOL_SIZE,
db_type = WRITE
})
local database_read = Database:new({
host = conf.POSTGRES.HOST,
port = conf.POSTGRES.PORT,
user = conf.POSTGRES.USERNAME,
password = conf.POSTGRES.PASSWORD,
database = conf.POSTGRES.DATABASE,
charset = conf.POSTGRES.CHARSET,
timeout = conf.POSTGRES.TIMEOUT,
db_pool_timeout = conf.POSTGRES.POOL_TIMEOUT,
db_pool_size = conf.POSTGRES.POOL_SIZE,
host = SYSTEM_CONFIG.POSTGRES.HOST,
port = SYSTEM_CONFIG.POSTGRES.PORT,
user = SYSTEM_CONFIG.POSTGRES.USERNAME,
password = SYSTEM_CONFIG.POSTGRES.PASSWORD,
database = SYSTEM_CONFIG.POSTGRES.DATABASE,
charset = SYSTEM_CONFIG.POSTGRES.CHARSET,
timeout = SYSTEM_CONFIG.POSTGRES.TIMEOUT,
db_pool_timeout = SYSTEM_CONFIG.POSTGRES.POOL_TIMEOUT,
db_pool_size = SYSTEM_CONFIG.POSTGRES.POOL_SIZE,
db_type = READ
})

61
src/share/redis.lua Normal file
View File

@ -0,0 +1,61 @@
local redis = require("resty.redis")
local _M = setmetatable({}, {__index = function(self, key)
local red = redis:new()
local ok, err = red:connect(SYSTEM_CONFIG.REDIS.HOST, SYSTEM_CONFIG.REDIS.PORT)
if not ok then
ngx.log(ngx.ERR, err)
end
if key == "red" then
return red
end
end})
function _M:set(key, value, time)
local ok, err = self.red:set(key, value)
if not ok then
return false, "redis failed to set data: " .. err
end
if time then
ok,err = self.red:expire(key, time) -- default expire time is seconds
if not ok then
return false,err
end
end
return true
end
function _M:get(key)
local value = self.red:get(key)
if value == ngx.null then
return nil
else
return value
end
end
function _M:del(key)
return self.red:del(key)
end
function _M:expire(key, time)
local ok,err = self.red:expire(key, time) -- default time is seconds
if not ok then
return false,err
end
return true
end
function _M:incr(key)
local ok,err = self.red:incr(key)
if not ok then
return false, err
end
return true
end
function _M:ttl(key)
return self.red:ttl(key)
end
return _M

View File

@ -7,6 +7,7 @@
local helpers = require("share.helpers")
local jsonschema = require("jsonschema")
local cjson = require("cjson.safe")
local redis = require("share.redis")
--local workerId = 0 -- 假设当前机器的ID是1范围在[0, 31]之间
--local datacenterId = 0 -- 数据中心ID范围在[0, 31]之间
@ -16,9 +17,67 @@ local cjson = require("cjson.safe")
--max =a and b or c--a?b:c
--[[
--获取用户相关的角色数据的数据
local function init_task()
local redis = require("share.redis")
--引用使用的库文件
local Model = require("share.model")
--创建一个数据表相关的模型
local userModel = Model:new('sys_user')
--获取数据表中的记录数
local code, res = userModel:count()
--redis:set("admin-system:user:add", "1")
--redis:set("admin-system:user:edit", "1")
--redis:set("admin-system:user:delete", "1")
--redis:set("admin-system:user:view", "1")
--local ok, err = redis:set("admin-system:user:list", "1")
--if not ok then
-- ngx.log(ngx.ERR, "failed to set key in Redis: ", err)
--else
-- ngx.log(ngx.INFO, "updated key: ", key, " with value: ", value)
--end
--dict:set("RBAC", "1")
ngx.thread.kill(t)
end
--启动线程进行处理
t = ngx.thread.spawn(init_task)
--]]
--[[
--调用c库相关例子
local mylib = require "addlib"
ngx.say(addlib.add(5,7))
--]]
--local dict = ngx.shared.dictRBAC
--local value, err = dict:get("zhangsan-system:user:list")
--if value then
-- ngx.say("zhangsan-system:user:list is exist")
--else
-- ngx.say("zhangsan-system:user:list is not exist")
--end
local val1, err = redis:get("admin-system:user:add")
local val2, err = redis:get("admin-system:user:edit")
local val3, err = redis:get("admin-system:user:delete")
local val4, err = redis:get("admin-system:user:view")
local val5, err = redis:get("admin-system:user:list")
ngx.say("add:"..val1)
ngx.say("edit:"..val2)
ngx.say("delete:"..val3)
ngx.say("view:"..val4)
ngx.say("list:"..val5)
local val6, err = redis:get("admin-system:user:test")
if val6 ~= nil then
ngx.say("test:"..val6)
end
--[[
local uuid = require("resty.jit-uuid")
uuid.seed()

View File

@ -75,4 +75,19 @@ function RBAC:get_user_permissions(user_id)
return user_permissions
end
-- 添加角色
--_, err = permit.AddPolicy(roleName, roleId, action)
-- 赋予用户角色
--_, err = permit.AddRoleForUser(user, roleName)
-- 查看具有某角色的所有用户
--res, err = permit.GetUsersForRole(roleName)
-- 移除用户具有的角色
--_, err = permit.DeleteRoleForUser(user, roleName)
-- 移除角色,跟角色相关联的用户都被移除
--_, err = permit.DeleteRole(roleName)
return RBAC

View File

@ -5,7 +5,6 @@
---
local jwt = require("resty.jwt")
local conf = require("config")
local jsonschema = require("jsonschema")
local _M = {}
@ -39,7 +38,7 @@ function _M.generateToken(userid, username)
obj.payload.userid = userid
obj.payload.username = username
--获取的登陆的用户信息返回tocken
local jwt_token = jwt:sign(conf.secret_key, obj)
local jwt_token = jwt:sign(SYSTEM_CONFIG.secret_key, obj)
return "Bearer "..jwt_token
end
@ -68,7 +67,7 @@ function _M.authorizationToken(auth_header)
--查找令牌中的Bearer前缀字符并进行截取
local token = string.sub(auth_header,8)
--校验令牌
local jwt_obj = jwt:verify(conf.secret_key, token)
local jwt_obj = jwt:verify(SYSTEM_CONFIG.secret_key, token)
--如果校验结果中的verified==false则表示令牌无效
if jwt_obj.verified == false then
response["code"] = 401

View File

@ -1,37 +0,0 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by .
--- DateTime: 2025/11/04 11:12
--- 业务逻辑 对菜单数据进行参数数据的验证
local jsonschema = require("jsonschema")
local _M = {}
-- 定义一个JSON Schema
local schema = {
{type = "object", properties = {
{name = "menu_id", type = "string"},
{name = "menu_name", type = "string"},
{name = "parent_id", type = "string"},
{name = "order_num", type = "number"},
{name = "url", type = "string"},
{name = "target", type = "string"},
{name = "menu_type", type = "string"},
{name = "status", type = "string"},
{name = "is_refresh", type = "string"},
{name = "perms", type = "string"},
{name = "perms", type = "string"},
{name = "create_by", type = "string"},
{name = "update_by", type = "string"},
{name = "remark", type = "string"},
}, required = {"menu_id", "menu_name"}}
}
function _M.validatorJson(jsonData)
-- 验证数据是否符合schema
local validator = jsonschema.generate_validator(schema)
local result = validator(jsonData)
return result
end
return _M