summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTed Trask <ttrask01@yahoo.com>2011-06-09 06:45:30 +0000
committerTed Trask <ttrask01@yahoo.com>2011-06-09 06:45:30 +0000
commit4512d2436bbb338900520cae66a762f9ab00d545 (patch)
tree85db409dedc752022fe8c3d4b3fc598f1a3efd04
parent70190e54e41ce5c64bf895e18492df1c4f83ab2f (diff)
downloadacf-provisioning-4512d2436bbb338900520cae66a762f9ab00d545.tar.bz2
acf-provisioning-4512d2436bbb338900520cae66a762f9ab00d545.tar.xz
Added lua validate code into provisioning_params table, added example for mac address
-rw-r--r--provisioning-model.lua77
-rwxr-xr-xprovisioning-scripts.lua56
2 files changed, 89 insertions, 44 deletions
diff --git a/provisioning-model.lua b/provisioning-model.lua
index 683b90d..bb68af6 100644
--- a/provisioning-model.lua
+++ b/provisioning-model.lua
@@ -219,13 +219,42 @@ validateparam = function(p)
return true
end
+-- These are the functions that may be called from within loaded Lua code
+local functions = {
+ getselectresponse=getselectresponse,
+ runsqlcommand=runsqlcommand,
+ get_device=get_device,
+ get_device_params=get_device_params,
+}
+
+local validateparamcoded
+validateparamcoded = function(p, top)
+ top = top or p
+ local success = true
+ if p.type == "group" then
+ for n,p2 in pairs(p.value) do
+ success = validateparamcoded(p2, top) and success
+ end
+ return success
+ end
+ if p.validate and p.validate ~= "" then
+ -- We have Lua validation code
+ local env = {}
+ setmetatable (env, {__index = _G})
+ -- loadfile loads into the global environment
+ -- so we set env 0, not env 1
+ setfenv (0, env)
+ local f = loadstring(p.validate)
+ if (f) then
+ p.value, p.errtxt = f(p.value, functions, top)
+ if p.errtxt then success = false end
+ end
+ setfenv (0, _G)
+ end
+ return success
+end
+
local function callscript(script, ...)
- local functions = {
- getselectresponse=getselectresponse,
- runsqlcommand=runsqlcommand,
- get_device=get_device,
- get_device_params=get_device_params,
- }
local env = {}
setmetatable (env, {__index = _G})
-- loadfile loads into the global environment
@@ -236,23 +265,30 @@ local function callscript(script, ...)
setfenv (0, _G)
end
-local function validatefiledetails(filedetails)
+local function validateluacode(code)
local success = true
-
+
-- Validate that contents are valid lua code
local env = {}
setmetatable (env, {__index = _G})
-- loadfile loads into the global environment
-- so we set env 0, not env 1
setfenv (0, env)
- local f,err = loadstring(filedetails.value.filecontent.value)
- if not (f) then
+ local f,errtxt = loadstring(code)
+ if not f then
success = false
- filedetails.value.filecontent.errtxt = "Invalid Lua code\n"..(err or "")
end
setfenv (0, _G)
-- setmetatable (self.conf.app_hooks, {})
+ return success, errtxt
+end
+
+local function validatefiledetails(filedetails)
+ local success, errtxt = validateluacode(filedetails.value.filecontent.value)
+ if not success then
+ filedetails.value.filecontent.errtxt = "Invalid Lua code\n"..(errtxt or "")
+ end
return success, filedetails
end
@@ -1003,8 +1039,8 @@ get_param = function(param_id)
retval.descr = cfe({label="Description", seq=5})
retval.value = cfe({label="Default Value", descr="Warning, this value is not validated", seq=6})
retval.regexp = cfe({label="Regular Expression", descr="Lua regular expression for validating the text parameter value", seq=7})
- retval.seq = cfe({label="Sequence", seq=8})
--- FIXME - we should add validation and option stuff here
+ retval.validate = cfe({type="longtext", label="Validation Code", descr="Lua code to validate the parameter value. Returns updated value and error text. Not used to validate group defaults.", seq=8})
+ retval.seq = cfe({label="Sequence", seq=9})
local errtxt
local res, err = pcall(function()
local connected = databaseconnect()
@@ -1049,6 +1085,14 @@ update_param = function(param, create)
success = false
param.value.seq.errtxt = "Must be an integer"
end
+ local s,e = validateluacode(param.value.validate.value)
+ if not s then
+ success = false
+ param.value.validate.errtxt = "Invalid Lua code"
+ if e and e ~= "" then
+ param.value.validate.errtxt = param.value.validate.errtxt.."\n"..e
+ end
+ end
if success then
local res, err = pcall(function()
local connected = databaseconnect()
@@ -1064,7 +1108,7 @@ update_param = function(param, create)
local sql = "BEGIN TRANSACTION"
runsqlcommand(sql)
if create then
- sql = "INSERT INTO provisioning_params VALUES(DEFAULT, '"..escape(param.value.name.value).."', '"..escape(param.value.type.value).."', '"..escape(param.value.label.value).."', '"..escape(param.value.descr.value).."', '"..escape(param.value.value.value).."', '"..escape(param.value.seq.value).."', '"..escape(param.value.regexp.value).."')"
+ sql = "INSERT INTO provisioning_params VALUES(DEFAULT, '"..escape(param.value.name.value).."', '"..escape(param.value.type.value).."', '"..escape(param.value.label.value).."', '"..escape(param.value.descr.value).."', '"..escape(param.value.value.value).."', '"..escape(param.value.seq.value).."', '"..escape(param.value.regexp.value).."', '"..escape(param.value.validate.value).."')"
runsqlcommand(sql, true)
sql = "SELECT param_id FROM provisioning_params WHERE name='"..escape(param.value.name.value).."' AND label='"..escape(param.value.label.value).."'"
local tmp = getselectresponse(sql, true)
@@ -1072,7 +1116,7 @@ update_param = function(param, create)
param.value.param_id.value = tmp[1].param_id
end
else
- sql = "UPDATE provisioning_params SET (name, type, label, descr, value, seq, regexp) = ('"..escape(param.value.name.value).."', '"..escape(param.value.type.value).."', '"..escape(param.value.label.value).."', '"..escape(param.value.descr.value).."', '"..escape(param.value.value.value).."', '"..escape(param.value.seq.value).."', '"..escape(param.value.regexp.value).."') WHERE param_id='"..escape(param.value.param_id.value).."'"
+ sql = "UPDATE provisioning_params SET (name, type, label, descr, value, seq, regexp, validate) = ('"..escape(param.value.name.value).."', '"..escape(param.value.type.value).."', '"..escape(param.value.label.value).."', '"..escape(param.value.descr.value).."', '"..escape(param.value.value.value).."', '"..escape(param.value.seq.value).."', '"..escape(param.value.regexp.value).."', '"..escape(param.value.validate.value).."') WHERE param_id='"..escape(param.value.param_id.value).."'"
runsqlcommand(sql, true)
end
@@ -1323,7 +1367,7 @@ get_device_params = function(device_id, editable)
retval[g.name].type="group"
end
-- Then, get all of the parameters for this device
- sql = "SELECT g.name AS group, g.label AS grouplabel, p.param_id, p.name, p.type, p.label, p.descr, p.seq, p.regexp, CASE WHEN v.value IS NOT NULL THEN v.value WHEN g2p.value IS NOT NULL THEN g2p.value ELSE p.value END AS value, CASE WHEN g2p.value IS NOT NULL THEN g2p.value ELSE p.value END AS default "..
+ sql = "SELECT g.name AS group, g.label AS grouplabel, p.param_id, p.name, p.type, p.label, p.descr, p.seq, p.regexp, p.validate, CASE WHEN v.value IS NOT NULL THEN v.value WHEN g2p.value IS NOT NULL THEN g2p.value ELSE p.value END AS value, CASE WHEN g2p.value IS NOT NULL THEN g2p.value ELSE p.value END AS default "..
"FROM (devices_to_classes d2t JOIN provisioning_classes t USING(class_id) JOIN classes_to_param_groups t2g USING (class_id) JOIN provisioning_groups g USING(group_id) "..
"JOIN param_groups_to_params g2p USING(group_id) JOIN provisioning_params p USING(param_id)) LEFT JOIN provisioning_values v ON(d2t.device_id=v.device_id AND p.param_id=v.param_id AND g.name=v.group_name ) "..
"WHERE d2t.device_id='"..escape(device_id).."'"
@@ -1380,6 +1424,7 @@ set_device_params = function(params, editable)
if success then
local res, err = pcall(function()
local connected = databaseconnect()
+ success = validateparamcoded(params)
if not create then
local sql = "SELECT * FROM devices_to_classes WHERE device_id='"..escape(params.value.device_id.value).."' LIMIT 1"
local tmp = getselectresponse(sql)
diff --git a/provisioning-scripts.lua b/provisioning-scripts.lua
index 2f16a19..4200e8b 100755
--- a/provisioning-scripts.lua
+++ b/provisioning-scripts.lua
@@ -522,36 +522,36 @@ param_groups_to_params = {
-- List of each parameter used in any way for any device - mostly for how to display
provisioning_params = {
- "CREATE TABLE provisioning_params (param_id SERIAL PRIMARY KEY, name VARCHAR(255) UNIQUE, type VARCHAR(255), label VARCHAR(255), descr VARCHAR(255), value VARCHAR(255), seq INTEGER, regexp VARCHAR(255))",
+ "CREATE TABLE provisioning_params (param_id SERIAL PRIMARY KEY, name VARCHAR(255) UNIQUE, type VARCHAR(255), label VARCHAR(255), descr VARCHAR(255), value VARCHAR(255), seq INTEGER, regexp VARCHAR(255), validate text)",
"CREATE INDEX params_name_idx ON provisioning_params (name)",
- "INSERT INTO provisioning_params VALUES(default, 'mac', 'text', 'MAC Address', 'Capitalized hex digits with no puncuation', '', '1', '^[0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F]$')",
- "INSERT INTO provisioning_params VALUES(default, 'template', 'select', 'Template', '', '', '2', '')",
- "INSERT INTO provisioning_params VALUES(default, 'registrar', 'text', 'SIP Registrar', '', '', '3', '')",
- "INSERT INTO provisioning_params VALUES(default, 'digitmap', 'text', 'Digit Map', 'Phone dial pattern based on section 2.1.5 of RFC 3435, plus a comma to turn dialtone back on', '', '4', '^[*#0-9xT|,.%[%]-]*$')",
- "INSERT INTO provisioning_params VALUES(default, 'digitmaptimeout', 'text', 'Digit Map Timeout', 'Timeout in seconds for each segment of digit map (separated by \'|\')', '', '5', '^[0-9|]*$')",
- "INSERT INTO provisioning_params VALUES(default, 'sntpserver', 'text', 'SNTP Server', '', '', '6', '')",
- "INSERT INTO provisioning_params VALUES(default, 'timezone', 'text', 'Posix Time Zone', 'See the expanded form (no colon) of TZ variable at http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html', '', '7', '^%a+[+-]?%d+')",
- "INSERT INTO provisioning_params VALUES(default, 'musiconhold', 'text', 'Music-on-hold URI', '', '', '8', '')",
- "INSERT INTO provisioning_params VALUES(default, 'homepage', 'text', 'Browser Homepage', '', '', '9', '')",
- "INSERT INTO provisioning_params VALUES(default, 'adminpassword', 'text', 'Administration Password', '', '', '10', '')",
- "INSERT INTO provisioning_params VALUES(default, 'pcportenable', 'boolean', 'PC Ethernet Port Enable', '', 'true', '11', '')",
- "INSERT INTO provisioning_params VALUES(default, 'urldialingenable', 'boolean', 'URL Dialing Enable', '', 'false', '12', '')",
+ "INSERT INTO provisioning_params VALUES(default, 'mac', 'text', 'MAC Address', 'Capitalized hex digits with no puncuation', '', '1', '^%x%x%x%x%x%x%x%x%x%x%x%x$', 'local value, functions, params = ...\nvalue = string.upper(value)\nlocal others = functions.getselectresponse(\"SELECT count(*) FROM provisioning_values WHERE param_id=\\'\"..params.value.device.value.mac.param_id..\"\\' AND device_id!=\\'\"..params.value.device_id.value..\"\\' AND value=\\'\"..value..\"\\'\")\nif tonumber(others[1].count) > 0 then\n\treturn value, \"MAC Address must be unique\"\nend\nreturn value')",
+ "INSERT INTO provisioning_params VALUES(default, 'template', 'select', 'Template', '', '', '2', '', null)",
+ "INSERT INTO provisioning_params VALUES(default, 'registrar', 'text', 'SIP Registrar', '', '', '3', '', null)",
+ "INSERT INTO provisioning_params VALUES(default, 'digitmap', 'text', 'Digit Map', 'Phone dial pattern based on section 2.1.5 of RFC 3435, plus a comma to turn dialtone back on', '', '4', '^[*#0-9xT|,.%[%]-]*$', null)",
+ "INSERT INTO provisioning_params VALUES(default, 'digitmaptimeout', 'text', 'Digit Map Timeout', 'Timeout in seconds for each segment of digit map (separated by \\'|\\')', '', '5', '^[0-9|]*$', null)",
+ "INSERT INTO provisioning_params VALUES(default, 'sntpserver', 'text', 'SNTP Server', '', '', '6', '', null)",
+ "INSERT INTO provisioning_params VALUES(default, 'timezone', 'text', 'Posix Time Zone', 'See the expanded form (no colon) of TZ variable at http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html', '', '7', '^%a+[+-]?%d+', null)",
+ "INSERT INTO provisioning_params VALUES(default, 'musiconhold', 'text', 'Music-on-hold URI', '', '', '8', '', null)",
+ "INSERT INTO provisioning_params VALUES(default, 'homepage', 'text', 'Browser Homepage', '', '', '9', '', null)",
+ "INSERT INTO provisioning_params VALUES(default, 'adminpassword', 'text', 'Administration Password', '', '', '10', '', null)",
+ "INSERT INTO provisioning_params VALUES(default, 'pcportenable', 'boolean', 'PC Ethernet Port Enable', '', 'true', '11', '', null)",
+ "INSERT INTO provisioning_params VALUES(default, 'urldialingenable', 'boolean', 'URL Dialing Enable', '', 'false', '12', '', null)",
- "INSERT INTO provisioning_params VALUES(default, 'extension', 'text', 'Extension', '', '', '101', '^%d*$')",
- "INSERT INTO provisioning_params VALUES(default, 'password', 'text', 'Password', '', '', '102', '')",
- "INSERT INTO provisioning_params VALUES(default, 'forwardnoanswerenable', 'boolean', 'Forward on No-answer Enable', '', 'false', '103', '')",
- "INSERT INTO provisioning_params VALUES(default, 'forwardnoanswer', 'text', 'Forward on No-answer Destination', 'Callers will be transferred to this extension when you don''t answer after 20 seconds', '', '104', '')",
- "INSERT INTO provisioning_params VALUES(default, 'forwardbusyenable', 'boolean', 'Forward on Busy Enable', '', 'false', '105', '')",
- "INSERT INTO provisioning_params VALUES(default, 'forwardbusy', 'text', 'Forward on Busy Destination', 'Callers will be transferred to this extension when the line is busy', '', '106', '')",
- "INSERT INTO provisioning_params VALUES(default, 'forwardallenable', 'boolean', 'Forward All Calls Enable', '', 'false', '107', '')",
- "INSERT INTO provisioning_params VALUES(default, 'forwardall', 'text', 'Forward All Calls Destination', 'All calls will be transferred to this extension', '', '108', '')",
- "INSERT INTO provisioning_params VALUES(default, 'callerid', 'text', 'Caller ID String', '', '', '109', '')",
- "INSERT INTO provisioning_params VALUES(default, 'forwarding', 'boolean', 'Forwarding Enable', '', 'true', '201', '')",
- "INSERT INTO provisioning_params VALUES(default, 'hotlineenable', 'boolean', 'Hotline Enable', '', 'false', '202', '')",
- "INSERT INTO provisioning_params VALUES(default, 'hotlinedestination', 'text', 'Hotline Destination', '', '', '203', '')",
- "INSERT INTO provisioning_params VALUES(default, 'callhistoryenable', 'boolean', 'Call History Enable', '', 'true', '204', '')",
- "INSERT INTO provisioning_params VALUES(default, 'callwaitingenable', 'boolean', 'Call Waiting Enable', '', 'true', '205', '')",
- "INSERT INTO provisioning_params VALUES(default, 'speeddialenable', 'boolean', 'Speed Dial Enable', '', 'true', '206', '')",
+ "INSERT INTO provisioning_params VALUES(default, 'extension', 'text', 'Extension', '', '', '101', '^%d*$', null)",
+ "INSERT INTO provisioning_params VALUES(default, 'password', 'text', 'Password', '', '', '102', '', null)",
+ "INSERT INTO provisioning_params VALUES(default, 'forwardnoanswerenable', 'boolean', 'Forward on No-answer Enable', '', 'false', '103', '', null)",
+ "INSERT INTO provisioning_params VALUES(default, 'forwardnoanswer', 'text', 'Forward on No-answer Destination', 'Callers will be transferred to this extension when you don''t answer after 20 seconds', '', '104', '', null)",
+ "INSERT INTO provisioning_params VALUES(default, 'forwardbusyenable', 'boolean', 'Forward on Busy Enable', '', 'false', '105', '', null)",
+ "INSERT INTO provisioning_params VALUES(default, 'forwardbusy', 'text', 'Forward on Busy Destination', 'Callers will be transferred to this extension when the line is busy', '', '106', '', null)",
+ "INSERT INTO provisioning_params VALUES(default, 'forwardallenable', 'boolean', 'Forward All Calls Enable', '', 'false', '107', '', null)",
+ "INSERT INTO provisioning_params VALUES(default, 'forwardall', 'text', 'Forward All Calls Destination', 'All calls will be transferred to this extension', '', '108', '', null)",
+ "INSERT INTO provisioning_params VALUES(default, 'callerid', 'text', 'Caller ID String', '', '', '109', '', null)",
+ "INSERT INTO provisioning_params VALUES(default, 'forwarding', 'boolean', 'Forwarding Enable', '', 'true', '201', '', null)",
+ "INSERT INTO provisioning_params VALUES(default, 'hotlineenable', 'boolean', 'Hotline Enable', '', 'false', '202', '', null)",
+ "INSERT INTO provisioning_params VALUES(default, 'hotlinedestination', 'text', 'Hotline Destination', '', '', '203', '', null)",
+ "INSERT INTO provisioning_params VALUES(default, 'callhistoryenable', 'boolean', 'Call History Enable', '', 'true', '204', '', null)",
+ "INSERT INTO provisioning_params VALUES(default, 'callwaitingenable', 'boolean', 'Call Waiting Enable', '', 'true', '205', '', null)",
+ "INSERT INTO provisioning_params VALUES(default, 'speeddialenable', 'boolean', 'Speed Dial Enable', '', 'true', '206', '', null)",
}
-- All of the (non-default) parameter values for all devices are stored here