comparison mod_adhoc_account_management/mod_adhoc_account_management.lua @ 3416:c6dd65354db0

mod_adhoc_account_management: Reduce indentation of one in the main function.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 23 Dec 2018 18:33:32 +0100
parents 1aa48916eb8b
children 6ce42aacad42
comparison
equal deleted inserted replaced
3415:6c806a99f802 3416:c6dd65354db0
43 change_password_layout = dataforms_new(change_password_layout); 43 change_password_layout = dataforms_new(change_password_layout);
44 44
45 function change_password_command_handler(self, data, state) 45 function change_password_command_handler(self, data, state)
46 if not state then -- New session, send the form 46 if not state then -- New session, send the form
47 return { status = "executing", actions = { "complete" }, form = change_password_layout }, true; 47 return { status = "executing", actions = { "complete" }, form = change_password_layout }, true;
48 else 48 end
49 if data.action == "cancel" then 49
50 return { status = "canceled" }; 50 if data.action == "cancel" then
51 return { status = "canceled" };
52 end
53
54 -- Who are we talking to?
55 local username, hostname = jid_split(data.from);
56 if not username or hostname ~= module.host then
57 return { status = "error", error = { type = "cancel",
58 condition = "forbidden", message = "Invalid user or hostname." } };
59 end
60
61 -- Extract data from the form
62 local fields = change_password_layout:data(data.form);
63
64 -- Validate
65 if require_current then
66 if not fields["password-current"] or #fields["password-current"] == 0 then
67 return { status = "error", error = { type = "modify",
68 condition = "bad-request", message = "Please enter your current password" } };
69 elseif not usermanager_test_password(username, hostname, fields["password-current"]) then
70 return { status = "error", error = { type = "modify",
71 condition = "bad-request", message = "Your current password was incorrect" } };
51 end 72 end
73 end
52 74
53 -- Who are we talking to? 75 if require_confirm and fields["password-confirm"] ~= fields["password"] then
54 local username, hostname = jid_split(data.from); 76 return { status = "error", error = { type = "modify",
55 if not username or hostname ~= module.host then 77 condition = "bad-request", message = "New password didn't match the confirmation" } };
56 return { status = "error", error = { type = "cancel", 78 end
57 condition = "forbidden", message = "Invalid user or hostname." } };
58 end
59 79
60 -- Extract data from the form 80 if not fields.password or #fields.password == 0 then
61 local fields = change_password_layout:data(data.form); 81 return { status = "error", error = { type = "modify",
82 condition = "bad-request", message = "Please enter a new password" } };
83 end
62 84
63 -- Validate 85 -- All is good, so change password.
64 if require_current then 86 module:log("debug", "About to usermanager.set_password(%q, password, %q)", username, hostname);
65 if not fields["password-current"] or #fields["password-current"] == 0 then 87 local ok, err = usermanager_set_password(username, fields.password, hostname);
66 return { status = "error", error = { type = "modify", 88 if ok then
67 condition = "bad-request", message = "Please enter your current password" } }; 89 if close_others then
68 elseif not usermanager_test_password(username, hostname, fields["password-current"]) then 90 for _, sess in pairs(hosts[hostname].sessions[username].sessions) do
69 return { status = "error", error = { type = "modify", 91 if sess.full_jid ~= data.from then
70 condition = "bad-request", message = "Your current password was incorrect" } }; 92 sess:close{ condition = "reset", text = "Password changed" }
93 end
71 end 94 end
72 end 95 end
73 96 return { status = "completed", info = "Password successfully changed" };
74 if require_confirm and fields["password-confirm"] ~= fields["password"] then 97 else
75 return { status = "error", error = { type = "modify", 98 module:log("warn", "%s@%s could not change password: %s", username, hostname, tostring(err));
76 condition = "bad-request", message = "New password didn't match the confirmation" } }; 99 return { status = "error", error = { type = "cancel",
77 end 100 condition = "internal-server-error", message = "Could not save new password: "..tostring(err) } };
78
79 if not fields.password or #fields.password == 0 then
80 return { status = "error", error = { type = "modify",
81 condition = "bad-request", message = "Please enter a new password" } };
82 end
83
84 -- All is good, so change password.
85 module:log("debug", "About to usermanager.set_password(%q, password, %q)", username, hostname);
86 local ok, err = usermanager_set_password(username, fields.password, hostname);
87 if ok then
88 if close_others then
89 for _, sess in pairs(hosts[hostname].sessions[username].sessions) do
90 if sess.full_jid ~= data.from then
91 sess:close{ condition = "reset", text = "Password changed" }
92 end
93 end
94 end
95 return { status = "completed", info = "Password successfully changed" };
96 else
97 module:log("warn", "%s@%s could not change password: %s", username, hostname, tostring(err));
98 return { status = "error", error = { type = "cancel",
99 condition = "internal-server-error", message = "Could not save new password: "..tostring(err) } };
100 end
101 end 101 end
102 end 102 end
103 103
104 -- Feature requests? What could fit under account management? 104 -- Feature requests? What could fit under account management?
105 105