1
0
mirror of https://github.com/lxsang/antd-web-apps synced 2024-11-20 10:18:21 +01:00
antd-web-apps/os/libs/common.lua

58 lines
1.3 KiB
Lua
Raw Normal View History

2018-09-12 18:31:25 +02:00
require("sqlite")
function fail(msg)
2021-05-06 20:39:10 +02:00
std.custom_header("Connection","close")
2018-09-12 18:31:25 +02:00
std.json()
std.t(JSON.encode({error=msg}))
end
function result(obj)
2021-05-06 20:39:10 +02:00
std.custom_header("Connection","close")
2018-09-12 18:31:25 +02:00
std.json()
std.t(JSON.encode({result=obj, error=false}))
end
function die (msg)
fail(msg)
debug.traceback=nil
error("Permission denied")
2020-06-26 15:20:17 +02:00
return false
2018-09-12 18:31:25 +02:00
end
-- check if the sysdb is create, otherwise create the table
function sysdb()
local meta = {}
meta.sessionid = ""
meta.username = ""
meta.stamp = 0
return require("dbmodel").get("sysdb", "sessions", meta)
end
function is_auth()
2021-02-09 01:30:08 +01:00
local sessionid = nil
if SESSION.sessionid and SESSION.sessionid ~= '0' then
sessionid = SESSION.sessionid
-- should be used only by API call
elseif REQUEST.sessionid and REQUEST.sessionid ~= '0' then
sessionid = REQUEST.sessionid
end
if sessionid == nil then
return false
end
2018-09-12 18:31:25 +02:00
-- query session id from database
local db = sysdb()
if db == nil then return false end
2021-02-09 01:30:08 +01:00
local cond = {exp= {["="] = { sessionid = sessionid }}}
2018-09-12 18:31:25 +02:00
local data = db:find(cond)
--print(JSON.encode(data))
db:close()
2020-06-26 15:20:17 +02:00
if data == nil or data[1] == nil then return die("No user data found") end
2018-09-12 18:31:25 +02:00
-- next time check the stamp
SESSION.user = data[1].username
return true
end
function auth_or_die(msg)
if(is_auth() == false) then
die(msg)
end
end