AuthPlatform/src/service/system/permission.lua

132 lines
4.3 KiB
Lua

---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by .
--- DateTime: 2025/9/27 17:06
--- 业务逻辑 对权限数据表进行数据表业务处理
local status = require("util.status")
local resp = require("util.response")
local permissionDao = require("dao.system.permission")
local validator = require("validator.system.permission")
local cjson = require("cjson.safe")
local perm = require("util.permissionfilter")
local _M = {}
--获取所有权限信息
function _M.getSystemPermissions()
local role = ngx.ctx.role
--权限数据
local perms = ngx.ctx.perms
--判断当前接口用户和角色是否有权限
if perm:hasPermission(role, perms) == false then
ngx.exit(ngx.HTTP_FORBIDDEN)
end
--获取页码和请求的数据量
local pageNum = ngx.var.pagenum or 1
local pageSize = ngx.var.pagesize or 10
local code,ret = permissionDao.getSystemPermissions(pageNum, pageSize)
local state = status.SUCCESS
if code ~= 0 then state = status.DATA_IS_WRONG end
resp: response(state, ret)
end
--根据权限id获取权限信息
function _M.get_permission(m)
local role = ngx.ctx.role
--权限数据
local perms = ngx.ctx.perms
--判断当前接口用户和角色是否有权限
if perm:hasPermission(role, perms) == false then
ngx.exit(ngx.HTTP_FORBIDDEN)
end
local code,ret = permissionDao.getPermission(m.id)
local state = status.SUCCESS
if code ~= 0 then state = status.DATA_IS_WRONG end
resp: response(state, ret)
end
--根据角色id获取使用的权限
function _M.getSystemPermissionByRole(m)
local role = ngx.ctx.role
--权限数据
local perms = ngx.ctx.perms
--判断当前接口用户和角色是否有权限
if perm:hasPermission(role, perms) == false then
ngx.exit(ngx.HTTP_FORBIDDEN)
end
local code,ret = dao.getPermissionByRole(m.id)
local state = status.SUCCESS
if code ~= 0 then state = status.DATA_IS_WRONG end
resp: response(state, ret)
end
--根据权限id获取账号信息
function _M.addSystemPermission()
local role = ngx.ctx.role
--权限数据
local perms = ngx.ctx.perms
--判断当前接口用户和角色是否有权限
if perm:hasPermission(role, perms) == false then
ngx.exit(ngx.HTTP_FORBIDDEN)
end
--读取请求体的数据
ngx.req.read_body()
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok = validator.validateJson(body_data)
--验证失败则返回
if not ok then
resp:response(status.PARAM_TYPE_BIND_ERROR)
return
end
--ngx.say(body_data)
local code, ret = permissionDao.addPermission(cjson.decode(body_data))
local state = status.SUCCESS
if code ~= 0 then state = status.DATA_IS_WRONG end
resp: response(state, ret)
end
--根据账号id删除账号信息
function _M.deleteSystemPermission(m)
local role = ngx.ctx.role
--权限数据
local perms = ngx.ctx.perms
--判断当前接口用户和角色是否有权限
if perm:hasPermission(role, perms) == false then
ngx.exit(ngx.HTTP_FORBIDDEN)
end
local code, ret = permissionDao.deleteSystemPermission(m.id)
local state = status.SUCCESS
if code ~= 0 then state = status.DATA_IS_WRONG end
resp: response(state, ret)
end
--根据账号id删除账号信息
function _M.updateSystemPermission(m)
local role = ngx.ctx.role
--权限数据
local perms = ngx.ctx.perms
--判断当前接口用户和角色是否有权限
if perm:hasPermission(role, perms) == false then
ngx.exit(ngx.HTTP_FORBIDDEN)
end
--读取请求体的数据
ngx.req.read_body()
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok = validator.validateJson(body_data)
--验证失败则返回
if not ok then
resp:response(status.PARAM_TYPE_BIND_ERROR)
return
end
local code, ret = permissionDao.updatePermission(m.id, cjson.decode(body_data))
local state = status.SUCCESS
if code ~= 0 then state = status.DATA_IS_WRONG end
resp: response(state, ret)
end
return _M