comparison mod_adhoc_cmd_admin/mod_adhoc_cmd_admin.lua @ 128:bdd1641c159d

mod_adhoc_cmd_admin: Add "End User Session" command. Also end sessions before deleting a user
author Florian Zeitz <florob@babelmonkeys.de>
date Fri, 29 Jan 2010 02:49:28 +0100
parents 843cadf36306
children 8945153321a1
comparison
equal deleted inserted replaced
127:6c454d7208ae 128:bdd1641c159d
44 title = "Deleting a User"; 44 title = "Deleting a User";
45 instructions = "Fill out this form to delete a user."; 45 instructions = "Fill out this form to delete a user.";
46 46
47 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" }; 47 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
48 { name = "accountjids", type = "jid-multi", label = "The Jabber ID(s) to delete" }; 48 { name = "accountjids", type = "jid-multi", label = "The Jabber ID(s) to delete" };
49 };
50
51 local end_user_session_layout = dataforms_new{
52 title = "Ending a User Session";
53 instructions = "Fill out this form to end a user's session.";
54
55 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
56 { name = "accountjids", type = "jid-multi", label = "The Jabber ID(s) for which to end sessions" };
49 }; 57 };
50 58
51 local get_user_password_layout = dataforms_new{ 59 local get_user_password_layout = dataforms_new{
52 title = "Getting User's Password"; 60 title = "Getting User's Password";
53 instructions = "Fill out this form to get a user's password."; 61 instructions = "Fill out this form to get a user's password.";
129 else 137 else
130 return { status = "executing", form = change_user_password_layout }, "executing"; 138 return { status = "executing", form = change_user_password_layout }, "executing";
131 end 139 end
132 end 140 end
133 141
142 function disconnect_user(match_jid)
143 local node, hostname, givenResource = jid.split(match_jid);
144 local host = hosts[hostname];
145 local sessions = host.sessions[node] and host.sessions[node].sessions;
146 for resource, session in pairs(sessions or {}) do
147 if not givenResource or (resource == givenResource) then
148 module:log("debug", "Disconnecting "..node.."@"..hostname.."/"..resource);
149 session:close();
150 end
151 end
152 return true;
153 end
154
134 function delete_user_command_handler(self, data, state) 155 function delete_user_command_handler(self, data, state)
135 if state then 156 if state then
136 if data.action == "cancel" then 157 if data.action == "cancel" then
137 return { status = "canceled" }; 158 return { status = "canceled" };
138 end 159 end
139 local fields = delete_user_layout:data(data.form); 160 local fields = delete_user_layout:data(data.form);
140 local failed = {}; 161 local failed = {};
141 local succeeded = {}; 162 local succeeded = {};
142 for _, aJID in ipairs(fields.accountjids) do 163 for _, aJID in ipairs(fields.accountjids) do
143 local username, host, resource = jid.split(aJID); 164 local username, host, resource = jid.split(aJID);
144 if usermanager_user_exists(username, host) and usermanager_create_user(username, nil, host) then 165 if usermanager_user_exists(username, host) and disconnect_user(aJID) and usermanager_create_user(username, nil, host) then
145 module:log("debug", "User " .. aJID .. " has been deleted"); 166 module:log("debug", "User " .. aJID .. " has been deleted");
146 succeeded[#succeeded+1] = aJID; 167 succeeded[#succeeded+1] = aJID;
147 else 168 else
148 module:log("debug", "Tried to delete non-existant user "..aJID); 169 module:log("debug", "Tried to delete non-existant user "..aJID);
149 failed[#failed+1] = aJID; 170 failed[#failed+1] = aJID;
153 "The following accounts were successfully deleted:\n"..t_concat(succeeded, "\n").."\n" or "").. 174 "The following accounts were successfully deleted:\n"..t_concat(succeeded, "\n").."\n" or "")..
154 (#failed ~= 0 and 175 (#failed ~= 0 and
155 "The following accounts could not be deleted:\n"..t_concat(failed, "\n") or "") }; 176 "The following accounts could not be deleted:\n"..t_concat(failed, "\n") or "") };
156 else 177 else
157 return { status = "executing", form = delete_user_layout }, "executing"; 178 return { status = "executing", form = delete_user_layout }, "executing";
179 end
180 end
181
182 function end_user_session_handler(self, data, state)
183 if state then
184 if data.action == "cancel" then
185 return { status = "canceled" };
186 end
187
188 local fields = end_user_session_layout:data(data.form);
189
190 for _, aJID in ipairs(fields.accountjids) do
191 disconnect_user(aJID);
192 end
193 return { status = "completed", info = "User(s) have been disconnected" };
194 else
195 return { status = "executing", form = end_user_session_layout }, "executing";
158 end 196 end
159 end 197 end
160 198
161 function get_user_password_handler(self, data, state) 199 function get_user_password_handler(self, data, state)
162 if state then 200 if state then
236 end 274 end
237 275
238 local add_user_desc = adhoc_new("Add User", "http://jabber.org/protocol/admin#add-user", add_user_command_handler, "admin"); 276 local add_user_desc = adhoc_new("Add User", "http://jabber.org/protocol/admin#add-user", add_user_command_handler, "admin");
239 local change_user_password_desc = adhoc_new("Change User Password", "http://jabber.org/protocol/admin#change-user-password", change_user_password_command_handler, "admin"); 277 local change_user_password_desc = adhoc_new("Change User Password", "http://jabber.org/protocol/admin#change-user-password", change_user_password_command_handler, "admin");
240 local delete_user_desc = adhoc_new("Delete User", "http://jabber.org/protocol/admin#delete-user", delete_user_command_handler, "admin"); 278 local delete_user_desc = adhoc_new("Delete User", "http://jabber.org/protocol/admin#delete-user", delete_user_command_handler, "admin");
279 local end_user_session_desc = adhoc_new("End User Session", "http://jabber.org/protocol/admin#end-user-session", end_user_session_handler, "admin");
241 local get_user_password_desc = adhoc_new("Get User Password", "http://jabber.org/protocol/admin#get-user-password", get_user_password_handler, "admin"); 280 local get_user_password_desc = adhoc_new("Get User Password", "http://jabber.org/protocol/admin#get-user-password", get_user_password_handler, "admin");
242 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"); 281 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");
243 local announce_desc = adhoc_new("Send Announcement to Online Users", "http://jabber.org/protocol/admin#announce", announce_handler, "admin"); 282 local announce_desc = adhoc_new("Send Announcement to Online Users", "http://jabber.org/protocol/admin#announce", announce_handler, "admin");
244 283
245 module:add_item("adhoc", add_user_desc); 284 module:add_item("adhoc", add_user_desc);
246 module:add_item("adhoc", change_user_password_desc); 285 module:add_item("adhoc", change_user_password_desc);
247 module:add_item("adhoc", delete_user_desc); 286 module:add_item("adhoc", delete_user_desc);
287 module:add_item("adhoc", end_user_session_desc);
248 module:add_item("adhoc", get_user_password_desc); 288 module:add_item("adhoc", get_user_password_desc);
249 module:add_item("adhoc", get_online_users_desc); 289 module:add_item("adhoc", get_online_users_desc);
250 module:add_item("adhoc", announce_desc); 290 module:add_item("adhoc", announce_desc);