Commit 5d25b383 authored by Ted Trask's avatar Ted Trask

Some cleanup of bulk creation

parent a7ac20b5
......@@ -111,7 +111,7 @@ mymodule.createuser = function( self )
end
mymodule.createmanyusers = function( self )
return self.handle_form(self, self.model.get_bunchsettings, self.model.set_bunchsettings, self.clientdata, "Create Many", "Create Users", "User Created")
return self.handle_form(self, self.model.get_bunchsettings, self.model.set_bunchsettings, self.clientdata, "Create Many", "Create Users", "Users Created")
end
mymodule.processdialplanxml = function( self )
......
......@@ -701,41 +701,46 @@ mymodule.get_bunchsettings = function (self)
return cfe({ type="group", value={bunch=bunch}, label="Voicemail Users List" })
end
mymodule.set_bunchsettings = function (self, clientdata)
local connected = databaseconnect()
local nExts = {}
tFile = assert(io.tmpfile())
tFile:write(clientdata.value.bunch.value)
tFile:seek("set",0)
for line in tFile:lines() do
for ext, fname, lname, pass in string.gmatch(line, "(%w+):(%w+):(%w+):(%w+)") do
nExts.username = ext or nil
nExts.firstname = fname or nil
nExts.lastname = lname or nil
nExts.password = pass or nil
sql = "INSERT INTO voicemail_users VALUES(null, '"..escape(nExts.username).."')"
runsqlcommand(sql)
sql = "SELECT uid FROM voicemail_users where username ='"..escape(nExts.username).."'";
uid = getselectresponse(sql)
nExts.uid = uid[1].uid or nil
sql = "INSERT INTO voicemail_values VALUES('"..uid[1].uid.."', '2', '"..escape(tostring(nExts.firstname)).."')"
runsqlcommand(sql)
sql = "INSERT INTO voicemail_values VALUES('"..uid[1].uid.."', '3', '"..escape(tostring(nExts.lastname)).."')"
runsqlcommand(sql)
sql = "INSERT INTO voicemail_prefs (username, domain, password) VALUES ('"..escape(nExts.username).."', '"..config.domain.."', '"..escape(nExts.password).."')"
runsqlcommand(sql)
mymodule.set_bunchsettings = function (self, bunchdata)
local res, err = pcall(function()
local connected = vmaildb.databaseconnect()
local sql = "BEGIN TRANSACTION"
vmaildb.runsqlcommand(sql)
for line in string.gmatch(bunchdata.value.bunch.value, "[^\n]+") do
local username, firstname, lastname, password = string.match(line, "(%w+):(%w+):(%w+):(%w+)")
if username then
sql = "INSERT INTO voicemail_users VALUES(null, '"..vmaildb.escape(username).."')"
vmaildb.runsqlcommand(sql)
sql = "SELECT uid FROM voicemail_users where username ='"..vmaildb.escape(username).."'";
uid = vmaildb.getselectresponse(sql)
sql = "INSERT INTO voicemail_values VALUES('"..uid[1].uid.."', '2', '"..vmaildb.escape(tostring(firstname)).."')"
vmaildb.runsqlcommand(sql)
sql = "INSERT INTO voicemail_values VALUES('"..uid[1].uid.."', '3', '"..vmaildb.escape(tostring(lastname)).."')"
vmaildb.runsqlcommand(sql)
sql = "INSERT INTO voicemail_prefs (username, domain, password) VALUES ('"..vmaildb.escape(username).."', '"..config.domain.."', '"..vmaildb.escape(password).."')"
vmaildb.runsqlcommand(sql)
else
bunchdata.value.bunch.errtxt = "Invalid syntax"
bunchdata.errtxt = "Failed to create users"
vmaildb.runsqlcommand("ROLLBACK")
break
end
end
end
vmaildb.runsqlcommand("COMMIT")
if connected then databasedisconnect() end
if connected then vmaildb.databasedisconnect() end
end)
if not res and err then
bunchdata.errtxt = err
end
return clientdata
return bunchdata
end
mymodule.get_usersettings = function(self, clientdata)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment