diff options
-rw-r--r-- | app/acf-util/password-controller.lua | 7 | ||||
-rw-r--r-- | app/acf-util/roles-controller.lua | 5 | ||||
-rw-r--r-- | app/acf_cli-controller.lua | 64 | ||||
-rw-r--r-- | app/acf_www-controller.lua | 97 | ||||
-rw-r--r-- | lib/Makefile | 1 | ||||
-rw-r--r-- | lib/README | 1 | ||||
-rw-r--r-- | lib/controllerfunctions.lua | 99 | ||||
-rwxr-xr-x | lua/mvc.lua | 31 |
8 files changed, 197 insertions, 108 deletions
diff --git a/app/acf-util/password-controller.lua b/app/acf-util/password-controller.lua index 37e5ced..336f6f4 100644 --- a/app/acf-util/password-controller.lua +++ b/app/acf-util/password-controller.lua @@ -1,5 +1,4 @@ module(..., package.seeall) -require("controllerfunctions") require("roles") default_action = "editme" @@ -11,7 +10,7 @@ end function editme(self) -- just to make sure can't modify any other user from this action self.clientdata.userid = self.sessiondata.userinfo.userid - return controllerfunctions.handle_form(self, function() + return self.handle_form(self, function() local value = self.model.read_user(self, self.sessiondata.userinfo.userid) -- We don't allow a user to modify his own roles -- Since they can't modify roles, we should restrict the available options for home @@ -44,7 +43,7 @@ function editme(self) end function edituser(self) - return controllerfunctions.handle_form(self, function() + return self.handle_form(self, function() return self.model.read_user(self, self.clientdata.userid) end, function(value) -- If password and password_confirm are blank, don't set them @@ -65,7 +64,7 @@ function edituser(self) end function newuser(self) - return controllerfunctions.handle_form(self, function() + return self.handle_form(self, function() return self.model.read_user(self) end, function(value) return self.model.create_user(self, value) diff --git a/app/acf-util/roles-controller.lua b/app/acf-util/roles-controller.lua index 27ddef7..adf4b23 100644 --- a/app/acf-util/roles-controller.lua +++ b/app/acf-util/roles-controller.lua @@ -1,7 +1,6 @@ -- Roles/Group functions module (..., package.seeall) -require("controllerfunctions") default_action = "read" @@ -44,14 +43,14 @@ viewroles = function(self) end newrole = function(self) - return controllerfunctions.handle_form(self, + return self.handle_form(self, function() return self.model.getpermissions(self) end, function(value) return self.model.setpermissions(self, value, true) end, self.clientdata, "Create", "Create New Role", "New Role Created") end editrole = function(self) - return controllerfunctions.handle_form(self, + return self.handle_form(self, function() return self.model.getpermissions(self, self.clientdata.role) end, function(value) return self.model.setpermissions(self, value, false) end, self.clientdata, "Save", "Edit Role", "Role Saved") diff --git a/app/acf_cli-controller.lua b/app/acf_cli-controller.lua index e68dfe9..beb83b3 100644 --- a/app/acf_cli-controller.lua +++ b/app/acf_cli-controller.lua @@ -47,3 +47,67 @@ end logevent = function ( ... ) os.execute ( "logger \"" .. ... .. "\"" ) end + +-- FIXME - remove the haserl specific stuff +handle_clientdata = function(form, clientdata) + form.errtxt = nil + for n,value in pairs(form.value) do + value.errtxt = nil + local name = n + if name:find("%.") and not clientdata[name] then + -- If the name has a '.' in it, haserl will interpret it as a table + local actualval = clientdata + for entry in name:gmatch("[^%.]+") do + if tonumber(entry) then + actualval = actualval[tonumber(entry)] + else + actualval = actualval[entry] + end + if not actualval then break end + end + clientdata[name] = actualval + end + if value.type == "group" then + handle_clientdata(value, clientdata[name]) + elseif value.type == "boolean" then + value.value = (clientdata[name] ~= nil) and (clientdata[name] ~= "false") + elseif value.type == "multi" then + if clientdata[name] == nil then + -- for cli we use name[num] as the name + clientdata[name] = {} + for n,val in pairs(clientdata) do + if string.find(n, "^"..name.."%[%d+%]$") then + clientdata[name][tonumber(string.match(n, "%[(%d+)%]$"))] = val + end + end + end + -- FIXME this is because multi selects don't work in haserl + local oldtable = clientdata[name] or {} + -- Assume it's a sparse array, and remove blanks + local newtable={} + for x=1,table.maxn(oldtable) do + if oldtable[x] then + newtable[#newtable + 1] = oldtable[x] + end + end + value.value = newtable + elseif value.type == "list" then + value.value = {} + if clientdata[name] and clientdata[name] ~= "" then + -- for www we use \r separated list + for ip in string.gmatch(clientdata[name].."\n", "%s*([^\n]*%S)%s*\n") do + table.insert(value.value, ip) + end + else + -- for cli we use name[num] as the name + for n,val in pairs(clientdata) do + if string.find(n, "^"..name.."%[%d+%]$") then + value.value[tonumber(string.match(n, "%[(%d+)%]$"))] = val + end + end + end + else + value.value = clientdata[name] or value.value + end + end +end diff --git a/app/acf_www-controller.lua b/app/acf_www-controller.lua index 462a226..4aed5e0 100644 --- a/app/acf_www-controller.lua +++ b/app/acf_www-controller.lua @@ -555,3 +555,100 @@ logevent = function ( message ) __index.logevent(message) end end + +handle_clientdata = function(form, clientdata) + form.errtxt = nil + for n,value in pairs(form.value) do + value.errtxt = nil + local name = n + if name:find("%.") and not clientdata[name] then + -- If the name has a '.' in it, haserl will interpret it as a table + local actualval = clientdata + for entry in name:gmatch("[^%.]+") do + if tonumber(entry) then + actualval = actualval[tonumber(entry)] + else + actualval = actualval[entry] + end + if not actualval then break end + end + clientdata[name] = actualval + end + if value.type == "group" then + handle_clientdata(value, clientdata[name]) + elseif value.type == "boolean" then + value.value = (clientdata[name] ~= nil) and (clientdata[name] ~= "false") + elseif value.type == "multi" then + if clientdata[name] == nil then + -- for cli we use name[num] as the name + clientdata[name] = {} + for n,val in pairs(clientdata) do + if string.find(n, "^"..name.."%[%d+%]$") then + clientdata[name][tonumber(string.match(n, "%[(%d+)%]$"))] = val + end + end + end + -- FIXME this is because multi selects don't work in haserl + local oldtable = clientdata[name] or {} + -- Assume it's a sparse array, and remove blanks + local newtable={} + for x=1,table.maxn(oldtable) do + if oldtable[x] then + newtable[#newtable + 1] = oldtable[x] + end + end + value.value = newtable + elseif value.type == "list" then + value.value = {} + if clientdata[name] and clientdata[name] ~= "" then + -- for www we use \r separated list + for ip in string.gmatch(clientdata[name].."\n", "%s*([^\n]*%S)%s*\n") do + table.insert(value.value, ip) + end + else + -- for cli we use name[num] as the name + for n,val in pairs(clientdata) do + if string.find(n, "^"..name.."%[%d+%]$") then + value.value[tonumber(string.match(n, "%[(%d+)%]$"))] = val + end + end + end + else + value.value = clientdata[name] or value.value + end + end +end + +handle_form = function(self, getFunction, setFunction, clientdata, option, label, descr) + local form = getFunction(clientdata) + + if clientdata.submit then + self.handle_clientdata(form, clientdata) + + form = setFunction(form, clientdata.submit) + if not form.errtxt and descr then + form.descr = descr + end + + if clientdata.redir then + form.value.redir = cfe({ type="hidden", value=clientdata.redir, label="" }) + end + form = self:redirect_to_referrer(form) + if clientdata.redir and not form.errtxt then + form.value = form.descr -- make it a command result + form.descr = nil + self:redirect(clientdata.redir, form) + end + else + if clientdata.redir then + form.value.redir = cfe({ type="hidden", value=clientdata.redir, label="" }) + end + form = self:redirect_to_referrer() or form + end + + form.type = "form" + form.option = option or form.option + form.label = label or form.label + + return form +end diff --git a/lib/Makefile b/lib/Makefile index e1e616b..adc2faf 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -6,7 +6,6 @@ LIB_DIST=menubuilder.lua\ authenticator-plaintext.lua\ roles.lua\ htmlviewfunctions.lua\ - controllerfunctions.lua\ modelfunctions.lua\ EXTRA_DIST=README Makefile @@ -7,7 +7,6 @@ Also we use Lua Posix for the rest of the functionality. apk.lua - Helps with package version/install/remove authenticator-plaintext.lua - sub-authenticator for plaintext files authenticator.lua - Used for authentication and roles, generic and uses sub-authenticator -controllerfunctions.lua - Common controller functions date.lua - Date and Time functions format.lua - Library to help reformat strings and tables. fs.lua - File and filesystem library diff --git a/lib/controllerfunctions.lua b/lib/controllerfunctions.lua deleted file mode 100644 index b0879ae..0000000 --- a/lib/controllerfunctions.lua +++ /dev/null @@ -1,99 +0,0 @@ -module(..., package.seeall) - -function handle_clientdata(form, clientdata, group) - form.errtxt = nil - for n,value in pairs(form.value) do - value.errtxt = nil - local name = n - if group then name = group.."."..name end - if name:find("%.") and not clientdata[name] then - -- If the name has a '.' in it, haserl will interpret it as a table - local actualval = clientdata - for entry in name:gmatch("[^%.]+") do - if tonumber(entry) then - actualval = actualval[tonumber(entry)] - else - actualval = actualval[entry] - end - if not actualval then break end - end - clientdata[name] = actualval - end - if value.type == "group" then - handle_clientdata(value, clientdata, name) - elseif value.type == "boolean" then - value.value = (clientdata[name] ~= nil) and (clientdata[name] ~= "false") - elseif value.type == "multi" then - if clientdata[name] == nil then - -- for cli we use name[num] as the name - clientdata[name] = {} - for n,val in pairs(clientdata) do - if string.find(n, "^"..name.."%[%d+%]$") then - clientdata[name][tonumber(string.match(n, "%[(%d+)%]$"))] = val - end - end - end - -- FIXME this is because multi selects don't work in haserl - local oldtable = clientdata[name] or {} - -- Assume it's a sparse array, and remove blanks - local newtable={} - for x=1,table.maxn(oldtable) do - if oldtable[x] then - newtable[#newtable + 1] = oldtable[x] - end - end - value.value = newtable - elseif value.type == "list" then - value.value = {} - if clientdata[name] and clientdata[name] ~= "" then - -- for www we use \r separated list - for ip in string.gmatch(clientdata[name].."\n", "%s*([^\n]*%S)%s*\n") do - table.insert(value.value, ip) - end - else - -- for cli we use name[num] as the name - for n,val in pairs(clientdata) do - if string.find(n, "^"..name.."%[%d+%]$") then - value.value[tonumber(string.match(n, "%[(%d+)%]$"))] = val - end - end - end - else - value.value = clientdata[name] or value.value - end - end -end - -function handle_form(self, getFunction, setFunction, clientdata, option, label, descr) - local form = getFunction(clientdata) - - if clientdata.submit then - handle_clientdata(form, clientdata) - - form = setFunction(form, clientdata.submit) - if not form.errtxt and descr then - form.descr = descr - end - - if clientdata.redir then - form.value.redir = cfe({ type="hidden", value=clientdata.redir, label="" }) - end - form = self:redirect_to_referrer(form) - if clientdata.redir and not form.errtxt then - form.value = form.descr -- make it a command result - form.descr = nil - self:redirect(clientdata.redir, form) - end - else - if clientdata.redir then - form.value.redir = cfe({ type="hidden", value=clientdata.redir, label="" }) - end - form = self:redirect_to_referrer() or form - end - - form.type = "form" - form.option = option or form.option - form.label = label or form.label - - return form -end diff --git a/lua/mvc.lua b/lua/mvc.lua index c53d576..ca48be6 100755 --- a/lua/mvc.lua +++ b/lua/mvc.lua @@ -430,3 +430,34 @@ _G.cfe = cfe logevent = function ( ... ) os.execute ( "logger \"ACF: " .. (... or "") .. "\"" ) end + +handle_clientdata = function(form, clientdata) + form.errtxt = nil + for name,value in pairs(form.value) do + value.errtxt = nil + if value.type == "group" then + handle_clientdata(value, clientdata[name]) + else + value.value = clientdata[name] or value.value + end + end +end + +handle_form = function(self, getFunction, setFunction, clientdata, option, label, descr) + local form = getFunction(clientdata) + + if clientdata.submit then + self.handle_clientdata(form, clientdata) + + form = setFunction(form, clientdata.submit) + if not form.errtxt and descr then + form.descr = descr + end + end + + form.type = "form" + form.option = option or form.option + form.label = label or form.label + + return form +end |