module(..., package.seeall) local list_redir = function(self) self.conf.action = "read" self.conf.type = "redir" error(self.conf) end local cfgfile local service mvc={} mvc.on_load = function(self, parent) cfgfile = self:new("cfgfile") service = self:new("service") if (self.worker[self.conf.action] == nil) or (self.conf.action == "init") then self.worker[self.conf.action] = list_redir(self) end end -- Public methods -- /hostname/get local function getNotes(self) ret = {} for k,v in pairs(cfgfile.model:list(nil)) do if v.status then ret[#ret + 1] = { content = "There are some configuration changes. Please do not forget to save." } break end end return ret end read = function(self) return { list=cfgfile.model:list(function(x) return x.app == "firewall" end), script=ENV["SCRIPT_NAME"], prefix=self.conf.prefix, controller=self.conf.controller, action="update", note=getNotes(self), } end update = function(self) local id = tonumber(self.clientdata.id) or -1 local result local data result, data = cfgfile.model:get(id) if not result then return list_redir(self) end if self.clientdata.cmd then for k,v in pairs (data) do if self.clientdata[k] then data[k].value = self.clientdata[k] end end result, data = cfgfile.model:set(id, data) if result then return list_redir(self) end end data.cmd = cfe { type="action", value="save", label="action" } return cfe{ type="form", option={ script=ENV["SCRIPT_NAME"], prefix=self.conf.prefix, controller = self.conf.controller, action = "update", extra = ""}, value = data} end local function serviceAction(self, action) local id = tonumber(self.clientdata.id) or -1 local svc = service.model:list("firewall") local ret = { script=ENV["SCRIPT_NAME"], prefix=self.conf.prefix, controller = self.conf.controller, action={}, title="Firewall", text={}, active={ id=id } } for i,s in pairs(svc) do for i,a in ipairs(s.actions) do ret.action[#ret.action + 1] = { name = a, section = s.name .. " (" .. tostring(s.status) .. ")", id = s.id, label = a, } end end if self.clientdata[action] then local result, report = service.model:update(id, action) local label = "Error" if result then ret.active.action = action label = "Report" end ret.text[#ret.text + 1] = { label=label, content=report } end ret.note=getNotes(self) return ret end start = function(self) return serviceAction(self, "start") end stop = function(self) return serviceAction(self, "stop") end restart = function(self) return serviceAction(self, "restart") end