mod_adhoc_cmd_admin/mod_adhoc_cmd_admin.lua
changeset 34 fc374b724270
parent 33 f3225c55288f
child 35 3c49411d4aa3
equal deleted inserted replaced
33:f3225c55288f 34:fc374b724270
    11 
    11 
    12 local is_admin = require "core.usermanager".is_admin;
    12 local is_admin = require "core.usermanager".is_admin;
    13 local admins = set.new(config.get(module:get_host(), "core", "admins"));
    13 local admins = set.new(config.get(module:get_host(), "core", "admins"));
    14 
    14 
    15 local sessions = {};
    15 local sessions = {};
       
    16 
       
    17 local add_user_layout = {
       
    18 	title= "Adding a User";
       
    19 	instructions = "Fill out this form to add a user.";
       
    20 
       
    21 	{ name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
       
    22 	{ name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for the account to be added" };
       
    23 	{ name = "password", type = "text-private", label = "The password for this account" };
       
    24 	{ name = "password-verify", type = "text-private", label = "Retype password" };
       
    25 };
       
    26 dataforms_new(add_user_layout)
    16 
    27 
    17 function add_user_command_handler(item, origin, stanza)
    28 function add_user_command_handler(item, origin, stanza)
    18 	if not is_admin(stanza.attr.from) then
    29 	if not is_admin(stanza.attr.from) then
    19 		module:log("warn", "Non-admin %s tried to add a user", tostring(jid.bare(stanza.attr.from)));
    30 		module:log("warn", "Non-admin %s tried to add a user", tostring(jid.bare(stanza.attr.from)));
    20 		origin.send(st.error_reply(stanza, "auth", "forbidden", "You don't have permission to add a user"):up()
    31 		origin.send(st.error_reply(stanza, "auth", "forbidden", "You don't have permission to add a user"):up()
    35 			if tag.name == "x" and tag.attr.xmlns == "jabber:x:data" then
    46 			if tag.name == "x" and tag.attr.xmlns == "jabber:x:data" then
    36 				form = tag;
    47 				form = tag;
    37 				break;
    48 				break;
    38 			end
    49 			end
    39 		end
    50 		end
    40 		local layout = {
    51 		local fields = add_user_layout:data(form);
    41 			{ name = "accountjid", type = "jid-single" };
       
    42 			{ name = "password", type = "text-private" };
       
    43 			{ name = "password-verify", type = "text-private" };
       
    44 		};
       
    45 		dataforms_new(layout);
       
    46 		local fields = layout:data(form);
       
    47 		local username, host, resource = jid.split(fields.accountjid);
    52 		local username, host, resource = jid.split(fields.accountjid);
    48 		if (fields.password == fields["password-verify"]) and username and host and host == stanza.attr.to then
    53 		if (fields.password == fields["password-verify"]) and username and host and host == stanza.attr.to then
    49 			if usermanager_user_exists(username, host) then
    54 			if usermanager_user_exists(username, host) then
    50 				origin.send(st.error_reply(stanza, "cancel", "conflict", "Account already exists"):up()
    55 				origin.send(st.error_reply(stanza, "cancel", "conflict", "Account already exists"):up()
    51 					:tag("command", {xmlns="http://jabber.org/protocol/commands",
    56 					:tag("command", {xmlns="http://jabber.org/protocol/commands",
    83 			return true;
    88 			return true;
    84 		end
    89 		end
    85 	else
    90 	else
    86 		local sessionid=uuid.generate();
    91 		local sessionid=uuid.generate();
    87 		sessions[sessionid] = "executing";
    92 		sessions[sessionid] = "executing";
    88 		local form = {
       
    89 			title= "Adding a User";
       
    90 			instructions = "Fill out this form to add a user.";
       
    91 
       
    92 			{ name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
       
    93 			{ name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for the account to be added" };
       
    94 			{ name = "password", type = "text-private", label = "The password for this account" };
       
    95 			{ name = "password-verify", type = "text-private", label = "Retype password" };
       
    96 		};
       
    97 		dataforms_new(form);
       
    98 		origin.send(st.reply(stanza):tag("command", {xmlns="http://jabber.org/protocol/commands",
    93 		origin.send(st.reply(stanza):tag("command", {xmlns="http://jabber.org/protocol/commands",
    99 			node="http://jabber.org/protocol/admin#add-user", sessionid=sessionid,
    94 			node="http://jabber.org/protocol/admin#add-user", sessionid=sessionid,
   100 			status="executing"}):add_child(form:form()));
    95 			status="executing"}):add_child(add_user_layout:form()));
   101 	end
    96 	end
   102 	return true;
    97 	return true;
   103 end
    98 end
   104 
    99 
   105 local descriptor = { name="Add User", node="http://jabber.org/protocol/admin#add-user", handler=add_user_command_handler };
   100 local descriptor = { name="Add User", node="http://jabber.org/protocol/admin#add-user", handler=add_user_command_handler };