comparison mod_adhoc_cmd_admin/mod_adhoc_cmd_admin.lua @ 91:63bf00fd8f6e

mod_adhoc_cmd_admin: Implement Get User Password
author Florian Zeitz <florob@babelmonkeys.de>
date Thu, 12 Nov 2009 22:13:28 +0100
parents b47216512a1d
children 7dad958aad15
comparison
equal deleted inserted replaced
90:d6521ebea967 91:63bf00fd8f6e
10 local hosts = prosody.hosts; 10 local hosts = prosody.hosts;
11 11
12 local t_concat = table.concat; 12 local t_concat = table.concat;
13 13
14 local usermanager_user_exists = require "core.usermanager".user_exists; 14 local usermanager_user_exists = require "core.usermanager".user_exists;
15 local usermanager_get_password = require "core.usermanager".get_password;
15 local usermanager_create_user = require "core.usermanager".create_user; 16 local usermanager_create_user = require "core.usermanager".create_user;
16 local is_admin = require "core.usermanager".is_admin; 17 local is_admin = require "core.usermanager".is_admin;
17 18
18 local st, jid, uuid = require "util.stanza", require "util.jid", require "util.uuid"; 19 local st, jid, uuid = require "util.stanza", require "util.jid", require "util.uuid";
19 local dataforms_new = require "util.dataforms".new; 20 local dataforms_new = require "util.dataforms".new;
35 title = "Deleting a User"; 36 title = "Deleting a User";
36 instructions = "Fill out this form to delete a user."; 37 instructions = "Fill out this form to delete a user.";
37 38
38 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" }; 39 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
39 { name = "accountjids", type = "jid-multi", label = "The Jabber ID(s) to delete" }; 40 { name = "accountjids", type = "jid-multi", label = "The Jabber ID(s) to delete" };
41 };
42
43 local get_user_password_layout = dataforms_new{
44 title = "Getting Users' Passwords";
45 instructions = "Fill out this form to get users' passwords.";
46
47 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
48 { name = "accountjids", type = "jid-multi", label = "The Jabber ID(s) for which to retrieve the password" };
40 }; 49 };
41 50
42 local get_online_users_layout = dataforms_new{ 51 local get_online_users_layout = dataforms_new{
43 title = "Getting List of Online Users"; 52 title = "Getting List of Online Users";
44 instructions = "How many users should be returned at most?"; 53 instructions = "How many users should be returned at most?";
140 origin.send(st.reply(stanza):add_child(item:cmdtag("executing", sessionid):add_child(delete_user_layout:form()))); 149 origin.send(st.reply(stanza):add_child(item:cmdtag("executing", sessionid):add_child(delete_user_layout:form())));
141 end 150 end
142 return true; 151 return true;
143 end 152 end
144 153
154 function get_user_password_handler(item, origin, stanza)
155 if stanza.tags[1].attr.sessionid and sessions[stanza.tags[1].attr.sessionid] then
156 if stanza.tags[1].attr.action == "cancel" then
157 origin.send(st.reply(stanza):add_child(item:cmdtag("canceled", stanza.tags[1].attr.sessionid)));
158 sessions[stanza.tags[1].attr.sessionid] = nil;
159 return true;
160 end
161 local form = stanza.tags[1]:child_with_ns("jabber:x:data");
162 local fields = get_user_password_layout:data(form);
163 local accountjids = st.stanza("field", {var="accountjids", label = "JIDs", type="jid-multi"});
164 local passwords = st.stanza("field", {var="password", label = "Passwords", type="text-multi"});
165 for _, aJID in ipairs(fields.accountjids) do
166 user, host, resource = jid.split(aJID);
167 if usermanager_user_exists(user, host) then
168 accountjids:tag("value"):text(aJID):up();
169 passwords:tag("value"):text(usermanager_get_password(user, host)):up();
170 end
171 end
172 origin.send(st.reply(stanza):add_child(item:cmdtag("completed", stanza.tags[1].attr.sessionid)
173 :tag("x", {xmlns="jabber:x:data", type="result"})
174 :tag("field", {type="hidden", var="FORM_TYPE"})
175 :tag("value"):text("http://jabber.org/protocol/admin"):up():up()
176 :add_child(accountjids)
177 :add_child(passwords)));
178 sessions[stanza.tags[1].attr.sessionid] = nil;
179 return true;
180 else
181 local sessionid=uuid.generate();
182 sessions[sessionid] = "executing";
183 origin.send(st.reply(stanza):add_child(item:cmdtag("executing", sessionid):add_child(get_user_password_layout:form())));
184 end
185 return true;
186 end
187
145 function get_online_users_command_handler(item, origin, stanza) 188 function get_online_users_command_handler(item, origin, stanza)
146 if stanza.tags[1].attr.sessionid and sessions[stanza.tags[1].attr.sessionid] then 189 if stanza.tags[1].attr.sessionid and sessions[stanza.tags[1].attr.sessionid] then
147 if stanza.tags[1].attr.action == "cancel" then 190 if stanza.tags[1].attr.action == "cancel" then
148 origin.send(st.reply(stanza):add_child(item:cmdtag("canceled", stanza.tags[1].attr.sessionid))); 191 origin.send(st.reply(stanza):add_child(item:cmdtag("canceled", stanza.tags[1].attr.sessionid)));
149 sessions[stanza.tags[1].attr.sessionid] = nil; 192 sessions[stanza.tags[1].attr.sessionid] = nil;
220 return true; 263 return true;
221 end 264 end
222 265
223 local add_user_desc = adhoc_new("Add User", "http://jabber.org/protocol/admin#add-user", add_user_command_handler, "admin"); 266 local add_user_desc = adhoc_new("Add User", "http://jabber.org/protocol/admin#add-user", add_user_command_handler, "admin");
224 local delete_user_desc = adhoc_new("Delete User", "http://jabber.org/protocol/admin#delete-user", delete_user_command_handler, "admin"); 267 local delete_user_desc = adhoc_new("Delete User", "http://jabber.org/protocol/admin#delete-user", delete_user_command_handler, "admin");
268 local get_user_password_desc = adhoc_new("Get User Password", "http://jabber.org/protocol/admin#get-user-password", get_user_password_handler, "admin");
225 local get_online_users_desc = adhoc_new("Get List of Online Users", "http://jabber.org/protocol/admin#get-online-users", get_online_users_command_handler, "admin"); 269 local get_online_users_desc = adhoc_new("Get List of Online Users", "http://jabber.org/protocol/admin#get-online-users", get_online_users_command_handler, "admin");
226 local announce_desc = adhoc_new("Send Announcement to Online Users", "http://jabber.org/protocol/admin#announce", announce_handler, "admin"); 270 local announce_desc = adhoc_new("Send Announcement to Online Users", "http://jabber.org/protocol/admin#announce", announce_handler, "admin");
227 271
228 function module.unload() 272 function module.unload()
229 module:remove_item("adhoc", add_user_desc); 273 module:remove_item("adhoc", add_user_desc);
230 module:remove_item("adhoc", delete_user_desc); 274 module:remove_item("adhoc", delete_user_desc);
275 module:remove_item("adhoc", get_user_password_desc);
231 module:remove_item("adhoc", get_online_users_desc); 276 module:remove_item("adhoc", get_online_users_desc);
232 module:remove_item("adhoc", announce_desc); 277 module:remove_item("adhoc", announce_desc);
233 end 278 end
234 279
235 module:add_item("adhoc", add_user_desc); 280 module:add_item("adhoc", add_user_desc);
236 module:add_item("adhoc", delete_user_desc); 281 module:add_item("adhoc", delete_user_desc);
282 module:add_item("adhoc", get_user_password_desc);
237 module:add_item("adhoc", get_online_users_desc); 283 module:add_item("adhoc", get_online_users_desc);
238 module:add_item("adhoc", announce_desc); 284 module:add_item("adhoc", announce_desc);