From 5babbc852f4f5a6b602704c6aaf862287d893c63 Mon Sep 17 00:00:00 2001 From: Ted Trask Date: Thu, 30 Nov 2017 14:56:10 +0000 Subject: Add history log entries for calls that modify data --- provisioning-model.lua | 64 ++++++++++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/provisioning-model.lua b/provisioning-model.lua index c46e058..24862aa 100644 --- a/provisioning-model.lua +++ b/provisioning-model.lua @@ -35,34 +35,6 @@ local functions -- ################################################################################ -- LOCAL FUNCTIONS -local logme = function(message) - local userid = "-" - if mymodule.sessiondata and mymodule.sessiondata.userinfo and mymodule.sessiondata.userinfo.userid then - userid = tostring(mymodule.sessiondata.userinfo.userid) - end - if provdb.isconnected() then - local sql = string.format("INSERT INTO dbhistlog VALUES ('now', '%s', '%s')", provdb.escape(message), provdb.escape(userid)) - provdb.runsqlcommand(sql, true) - end -end - --- Delete history log information from more than a month ago -local groomdbhistlog = function() - if provdb.isconnected() then - local sql = "DELETE FROM dbhistlog WHERE logdatetime < (now() - INTERVAL '1 month')" - provdb.runsqlcommand(sql, true) - logme("removed " .. res .. " old dbhistlog lines") - end -end - -local function logcall(fn) - if mymodule.sessiondata and mymodule.sessiondata.userinfo and mymodule.sessiondata.userinfo.userid then - mymodule.logevent("acf-provisioning: "..tostring(fn).." by "..tostring(mymodule.sessiondata.userinfo.userid)) - else - mymodule.logevent("acf-provisioning: "..tostring(fn)) - end -end - -- This function is used by scripts, do not change prototype local runsqlcommand = function(sql) mymodule.logevent(sql) @@ -190,6 +162,42 @@ local handlesqlexception = function(connected, err) end end +-- Delete history log information from more than a month ago +local groomdbhistlog = function() + if provdb.isconnected() then + local sql = "DELETE FROM dbhistlog WHERE logdatetime < (now() - INTERVAL '1 month')" + provdb.runsqlcommand(sql, true) + end +end + +local logme = function(message) + local userid = "-" + if mymodule.sessiondata and mymodule.sessiondata.userinfo and mymodule.sessiondata.userinfo.userid then + userid = tostring(mymodule.sessiondata.userinfo.userid) + end + + local connected + local res, err = pcall(function() + connected = databaseconnect() + local sql = string.format("INSERT INTO dbhistlog VALUES ('now', '%s', '%s')", provdb.escape(message), provdb.escape(userid)) + provdb.runsqlcommand(sql, true) + groomdbhistlog() + if connected then databasedisconnect() end + end) + if not res and err then + handlesqlexception(connected, err) + end +end + +local function logcall(fn) + if mymodule.sessiondata and mymodule.sessiondata.userinfo and mymodule.sessiondata.userinfo.userid then + mymodule.logevent("acf-provisioning: "..tostring(fn).." by "..tostring(mymodule.sessiondata.userinfo.userid)) + else + mymodule.logevent("acf-provisioning: "..tostring(fn)) + end + logme(fn) +end + local validateparam validateparam = function(p, allowdefault) if p.type == "group" then -- cgit v1.2.3