comparison mod_invites_adhoc/mod_invites_adhoc.lua @ 4410:d1230d32d709

mod_invites_adhoc: Add support for specifying roles that may invite users, admins may always invite
author Matthew Wild <mwild1@gmail.com>
date Thu, 28 Jan 2021 08:56:29 +0000
parents 44f6537f6427
children abac64f71698
comparison
equal deleted inserted replaced
4409:44f6537f6427 4410:d1230d32d709
1 -- XEP-0401: Easy User Onboarding 1 -- XEP-0401: Easy User Onboarding
2 local dataforms = require "util.dataforms"; 2 local dataforms = require "util.dataforms";
3 local datetime = require "util.datetime"; 3 local datetime = require "util.datetime";
4 local split_jid = require "util.jid".split; 4 local split_jid = require "util.jid".split;
5 local usermanager = require "core.usermanager";
5 6
6 local new_adhoc = module:require("adhoc").new; 7 local new_adhoc = module:require("adhoc").new;
7 8
8 -- Whether local users can invite other users to create an account on this server 9 -- Whether local users can invite other users to create an account on this server
9 local allow_user_invites = module:get_option_boolean("allow_user_invites", false); 10 local allow_user_invites = module:get_option_boolean("allow_user_invites", false);
10 -- Who can see and use the contact invite command. It is strongly recommended to 11 -- Who can see and use the contact invite command. It is strongly recommended to
11 -- keep this available to all local users. To allow/disallow invite-registration 12 -- keep this available to all local users. To allow/disallow invite-registration
12 -- on the server, use the option above instead. 13 -- on the server, use the option above instead.
13 local allow_contact_invites = module:get_option_boolean("allow_contact_invites", true); 14 local allow_contact_invites = module:get_option_boolean("allow_contact_invites", true);
15
16 local allow_user_invite_roles = module:get_option_set("allow_user_invites_by_roles");
14 17
15 local invites; 18 local invites;
16 if prosody.shutdown then -- COMPAT hack to detect prosodyctl 19 if prosody.shutdown then -- COMPAT hack to detect prosodyctl
17 invites = module:depends("invites"); 20 invites = module:depends("invites");
18 end 21 end
34 name = "expire"; 37 name = "expire";
35 label = "Invite valid until"; 38 label = "Invite valid until";
36 }, 39 },
37 }); 40 });
38 41
42 -- This is for checking if username (on the current host)
43 -- may create invites that allow people to register accounts
44 -- on this host.
45 local function may_invite_new_users(jid)
46 if allow_user_invites then
47 return true;
48 end
49 if usermanager.get_roles then
50 local user_roles = usermanager.get_roles(jid, module.host);
51 if not user_roles then return; end
52 if user_roles["prosody:admin"] then
53 return true;
54 elseif allow_user_invite_roles then
55 for allowed_role in allow_user_invite_roles do
56 if user_roles[allowed_role] then
57 return true;
58 end
59 end
60 end
61 elseif usermanager.is_admin(jid, module.host) then
62 return true;
63 end
64 return false;
65 end
66
39 module:depends("adhoc"); 67 module:depends("adhoc");
40 68
41 -- This command is available to all local users, even if allow_user_invites = false 69 -- This command is available to all local users, even if allow_user_invites = false
42 -- If allow_user_invites is false, creating an invite still works, but the invite will 70 -- If allow_user_invites is false, creating an invite still works, but the invite will
43 -- not be valid for registration on the current server, only for establishing a roster 71 -- not be valid for registration on the current server, only for establishing a roster
51 error = { 79 error = {
52 message = "This command is only available to users of "..module.host; 80 message = "This command is only available to users of "..module.host;
53 }; 81 };
54 }; 82 };
55 end 83 end
56 local invite = invites.create_contact(username, allow_user_invites, { 84 local invite = invites.create_contact(username, may_invite_new_users(data.from), {
57 source = data.from 85 source = data.from
58 }); 86 });
59 --TODO: check errors 87 --TODO: check errors
60 return { 88 return {
61 status = "completed"; 89 status = "completed";