mod_admin_web: Use util.dataforms' own error checking

This commit is contained in:
Florian Zeitz 2012-07-05 00:15:49 +02:00
parent d1a89e8f9c
commit bb7fd8bb30

View file

@ -27,6 +27,14 @@ local modulemanager = require "modulemanager";
module:depends("adhoc"); module:depends("adhoc");
local adhoc_new = module:require "adhoc".new; local adhoc_new = module:require "adhoc".new;
local function generate_error_message(errors)
local errmsg = {};
for name, err in pairs(errors) do
errmsg[#errmsg + 1] = name .. ": " .. err;
end
return { status = "completed", error = { message = t_concat(errmsg, "\n") } };
end
function add_user_command_handler(self, data, state) function add_user_command_handler(self, data, state)
local add_user_layout = dataforms_new{ local add_user_layout = dataforms_new{
title = "Adding a User"; title = "Adding a User";
@ -42,9 +50,9 @@ function add_user_command_handler(self, data, state)
if data.action == "cancel" then if data.action == "cancel" then
return { status = "canceled" }; return { status = "canceled" };
end end
local fields = add_user_layout:data(data.form); local fields, err = add_user_layout:data(data.form);
if not fields.accountjid then if err then
return { status = "completed", error = { message = "You need to specify a JID." } }; return generate_error_message(err);
end end
local username, host, resource = jid.split(fields.accountjid); local username, host, resource = jid.split(fields.accountjid);
if data.to ~= host then if data.to ~= host then
@ -85,9 +93,9 @@ function change_user_password_command_handler(self, data, state)
if data.action == "cancel" then if data.action == "cancel" then
return { status = "canceled" }; return { status = "canceled" };
end end
local fields = change_user_password_layout:data(data.form); local fields, err = change_user_password_layout:data(data.form);
if not fields.accountjid or fields.accountjid == "" or not fields.password then if err then
return { status = "completed", error = { message = "Please specify username and password" } }; return generate_error_message(err);
end end
local username, host, resource = jid.split(fields.accountjid); local username, host, resource = jid.split(fields.accountjid);
if data.to ~= host then if data.to ~= host then
@ -126,7 +134,10 @@ function delete_user_command_handler(self, data, state)
if data.action == "cancel" then if data.action == "cancel" then
return { status = "canceled" }; return { status = "canceled" };
end end
local fields = delete_user_layout:data(data.form); local fields, err = delete_user_layout:data(data.form);
if err then
return generate_error_message(err);
end
local failed = {}; local failed = {};
local succeeded = {}; local succeeded = {};
for _, aJID in ipairs(fields.accountjids) do for _, aJID in ipairs(fields.accountjids) do
@ -175,7 +186,10 @@ function end_user_session_handler(self, data, state)
return { status = "canceled" }; return { status = "canceled" };
end end
local fields = end_user_session_layout:data(data.form); local fields, err = end_user_session_layout:data(data.form);
if err then
return generate_error_message(err);
end
local failed = {}; local failed = {};
local succeeded = {}; local succeeded = {};
for _, aJID in ipairs(fields.accountjids) do for _, aJID in ipairs(fields.accountjids) do
@ -223,9 +237,9 @@ function get_user_password_handler(self, data, state)
if data.action == "cancel" then if data.action == "cancel" then
return { status = "canceled" }; return { status = "canceled" };
end end
local fields = get_user_password_layout:data(data.form); local fields, err = get_user_password_layout:data(data.form);
if not fields.accountjid then if err then
return { status = "completed", error = { message = "Please specify a JID." } }; return generate_error_message(err);
end end
local user, host, resource = jid.split(fields.accountjid); local user, host, resource = jid.split(fields.accountjid);
local accountjid = ""; local accountjid = "";
@ -261,10 +275,10 @@ function get_user_roster_handler(self, data, state)
return { status = "canceled" }; return { status = "canceled" };
end end
local fields = get_user_roster_layout:data(data.form); local fields, err = get_user_roster_layout:data(data.form);
if not fields.accountjid then if err then
return { status = "completed", error = { message = "Please specify a JID" } }; return generate_error_message(err);
end end
local user, host, resource = jid.split(fields.accountjid); local user, host, resource = jid.split(fields.accountjid);
@ -323,10 +337,10 @@ function get_user_stats_handler(self, data, state)
return { status = "canceled" }; return { status = "canceled" };
end end
local fields = get_user_stats_layout:data(data.form); local fields, err = get_user_stats_layout:data(data.form);
if not fields.accountjid then if err then
return { status = "completed", error = { message = "Please specify a JID." } }; return generate_error_message(err);
end end
local user, host, resource = jid.split(fields.accountjid); local user, host, resource = jid.split(fields.accountjid);
@ -376,7 +390,11 @@ function get_online_users_command_handler(self, data, state)
return { status = "canceled" }; return { status = "canceled" };
end end
local fields = get_online_users_layout:data(data.form); local fields, err = get_online_users_layout:data(data.form);
if err then
return generate_error_message(err);
end
local max_items = nil local max_items = nil
if fields.max_items ~= "all" then if fields.max_items ~= "all" then
@ -436,11 +454,9 @@ function load_module_handler(self, data, state)
if data.action == "cancel" then if data.action == "cancel" then
return { status = "canceled" }; return { status = "canceled" };
end end
local fields = layout:data(data.form); local fields, err = layout:data(data.form);
if (not fields.module) or (fields.module == "") then if err then
return { status = "completed", error = { return generate_error_message(err);
message = "Please specify a module."
} };
end end
if modulemanager.is_loaded(data.to, fields.module) then if modulemanager.is_loaded(data.to, fields.module) then
return { status = "completed", info = "Module already loaded" }; return { status = "completed", info = "Module already loaded" };
@ -470,11 +486,9 @@ function reload_modules_handler(self, data, state)
if data.action == "cancel" then if data.action == "cancel" then
return { status = "canceled" }; return { status = "canceled" };
end end
local fields = layout:data(data.form); local fields, err = layout:data(data.form);
if #fields.modules == 0 then if err then
return { status = "completed", error = { return generate_error_message(err);
message = "Please specify a module. (This means your client misbehaved, as this field is required)"
} };
end end
local ok_list, err_list = {}, {}; local ok_list, err_list = {}, {};
for _, module in ipairs(fields.modules) do for _, module in ipairs(fields.modules) do
@ -538,7 +552,11 @@ function shut_down_service_handler(self, data, state)
return { status = "canceled" }; return { status = "canceled" };
end end
local fields = shut_down_service_layout:data(data.form); local fields, err = shut_down_service_layout:data(data.form);
if err then
return generate_error_message(err);
end
if fields.announcement and #fields.announcement > 0 then if fields.announcement and #fields.announcement > 0 then
local message = st.message({type = "headline"}, fields.announcement):up() local message = st.message({type = "headline"}, fields.announcement):up()
@ -566,11 +584,9 @@ function unload_modules_handler(self, data, state)
if data.action == "cancel" then if data.action == "cancel" then
return { status = "canceled" }; return { status = "canceled" };
end end
local fields = layout:data(data.form); local fields, err = layout:data(data.form);
if #fields.modules == 0 then if err then
return { status = "completed", error = { return generate_error_message(err);
message = "Please specify a module. (This means your client misbehaved, as this field is required)"
} };
end end
local ok_list, err_list = {}, {}; local ok_list, err_list = {}, {};
for _, module in ipairs(fields.modules) do for _, module in ipairs(fields.modules) do