--[[ @object-name: acl @object-desc: 权限管理 --]] local sqlite3 = require "lsqlite3" local rpc = require "oui.rpc" local M = {} --[[ @method-type: call @method-name: get_group_list @method-desc: 获取权限组列表 @out array groups 权限组列表 @in-example: {"jsonrpc":"2.0","id":1,"method":"call","params":["","acl","get_group_list"]} @out-example: {"jsonrpc": "2.0", "id": 1, "result": { "groups": ["root", "test"] }} --]] function M.get_group_list() local db = sqlite3.open("/etc/oui/oui.db") local groups = {} for a in db:rows("SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE 'acl_%'") do local group = a[1]:sub(5) groups[#groups + 1] = group end db:close() return { groups = groups } end --[[ @method-type: call @method-name: add_group @method-desc: 添加权限组 @in string group 要添加的权限组 @out number err_code 错误码(-1: 已存在) @in-example: {"jsonrpc":"2.0","id":1,"method":"call","params":["","acl","add_group", {"group": "test"}]} @out-example: {"jsonrpc": "2.0", "id": 1, "result": null} --]] function M.add_group(params) local group = params.group local err_code = 0 if type(group) ~= "string" then return rpc.ERROR_CODE_INVALID_PARAMS end local db = sqlite3.open("/etc/oui/oui.db") if db:exec(string.format("CREATE TABLE acl_%s(scope TEXT NOT NULL, entry TEXT NOT NULL, perm TEXT NOT NULL)", group)) ~= 0 then err_code = -1 end db:close() if err_code < 0 then return {err_code = err_code, err_msg = "exists"} end end --[[ @method-type: call @method-name: remove_group @method-desc: 删除权限组 @in string group 要删除的权限组 @out number err_code 错误码(-1: 不存在) @in-example: {"jsonrpc":"2.0","id":1,"method":"call","params":["","acl","remove_group", {"group": "test"}]} @out-example: {"jsonrpc": "2.0", "id": 1, "result": null} --]] function M.remove_group(params) local group = params.group local err_code = 0 if type(group) ~= "string" then return rpc.ERROR_CODE_INVALID_PARAMS end local db = sqlite3.open("/etc/oui/oui.db") if db:exec(string.format("DROP TABLE acl_%s", group)) ~= 0 then err_code = -1 end db:close() if err_code < 0 then return {err_code = err_code, err_msg = "not exists"} end end --[[ @method-type: call @method-name: get_acl_list @method-desc: 获取某个组的所有权限 @in string group 要获取的的权限组 @out array acls 权限列表 @out string acls.scope 权限范围 @out string acls.entry 权限条目 @out string acls.perm 权限 @in-example: {"jsonrpc":"2.0","id":1,"method":"call","params":["","acl","get_acl_list", {"group": "test"}]} @out-example: {"jsonrpc": "2.0", "id": 1, "result": {"acls": [{"scope": "rpc", "entry": "system.info", "perm": "x"}]}} --]] function M.get_acl_list(params) local group = params.group if type(group) ~= "string" then return rpc.ERROR_CODE_INVALID_PARAMS end local db = sqlite3.open("/etc/oui/oui.db") local sql = string.format("SELECT * FROM acl_%s", group) local acls = {} for a in db:nrows(sql) do acls[#acls + 1] = a end db:close() return { acls = acls } end --[[ @method-type: call @method-name: add_acl @method-desc: 添加权限 @in string group 权限组 @in string scope 权限范围 @in string entry 权限条目 @in string perm 权限 @out number err_code 错误码(-1: 已存在) @in-example: {"jsonrpc":"2.0","id":1,"method":"call","params":["","acl","add_acl", {"group": "test", "scope": "rpc", "entry": "system.info", "perm": "x"}]} @out-example: {"jsonrpc": "2.0", "id": 1, "result": null} --]] function M.add_acl(params) local group = params.group local scope = params.scope local entry = params.entry local perm = params.perm local err_code = 0 if type(group) ~= "string" or type(scope) ~= "string" or type(entry) ~= "string" or type(perm) ~= "string" then return rpc.ERROR_CODE_INVALID_PARAMS end local db = sqlite3.open("/etc/oui/oui.db") local sql = string.format("SELECT * FROM acl_%s WHERE scope = '%s' AND entry = '%s'", group, scope, entry) for _ in db:nrows(sql) do err_code = -1 end sql = string.format("INSERT INTO acl_%s values('%s', '%s', '%s')", group, scope, entry, perm) db:exec(sql) db:close() if err_code < 0 then return {err_code = err_code, err_msg = "exists"} end end --[[ @method-type: call @method-name: remove_acl @method-desc: 获取某个组的所有权限 @in string group 权限组 @in string scope 权限范围 @in string entry 权限条目 @out number err_code 错误码(-1: 不存在) @in-example: {"jsonrpc":"2.0","id":1,"method":"call","params":["","acl","remove_acl", {"group": "test", "scope": "rpc", "entry": "system.info"}]} @out-example: {"jsonrpc": "2.0", "id": 1, "result": null} --]] function M.remove_acl(params) local group = params.group local scope = params.scope local entry = params.entry local err_code = 0 if type(group) ~= "string" or type(scope) ~= "string" or type(entry) ~= "string" then return rpc.ERROR_CODE_INVALID_PARAMS end local db = sqlite3.open("/etc/oui/oui.db") local sql = string.format("SELECT * FROM acl_%s WHERE scope = '%s' AND entry = '%s'", group, scope, entry) for _ in db:nrows(sql) do err_code = -1 end db:exec(string.format("DELETE FROM acl_%s WHERE scope = '%s' AND entry = '%s'", group, scope, entry)) db:close() if err_code < 0 then return {err_code = err_code, err_msg = "not exists"} end end --[[ @method-type: call @method-name: add_user @method-desc: 将某个用户加入某个权限组 @in string group 权限组 @in string username 用户名 @in-example: {"jsonrpc":"2.0","id":1,"method":"call","params":["","acl","add_user", {"group": "test", "username": "test"}]} @out-example: {"jsonrpc": "2.0", "id": 1, "result": null} --]] function M.add_user(params) local group = params.group local username = params.username if type(group) ~= "string" or type(username) ~= "string" then return rpc.ERROR_CODE_INVALID_PARAMS end local db = sqlite3.open("/etc/oui/oui.db") db:exec(string.format("DELETE FROM account WHERE username = '%s'", username)) db:exec(string.format("INSERT INTO account VALUES('%s', '%s')", username, group)) db:close() end --[[ @method-type: call @method-name: remove_user @method-desc: 删除用户 @in string username 用户名 @out number err_code 错误码(-1: 不存在) @in-example: {"jsonrpc":"2.0","id":1,"method":"call","params":["","acl","remove_user", {"group": "test", "username": "test"}]} @out-example: {"jsonrpc": "2.0", "id": 1, "result": null} --]] function M.remove_user(params) local username = params.username local exist = false if type(username) ~= "string" then return rpc.ERROR_CODE_INVALID_PARAMS end local db = sqlite3.open("/etc/oui/oui.db") for _ in db:nrows(string.format("SELECT * FROM account WHERE username = '%s'", username)) do exist = true end if exist then db:exec(string.format("DELETE FROM account WHERE username = '%s'", username)) end db:close() if not exist then return {err_code = -1, err_msg = "not exists"} end end return M