Comparison

plugins/mod_invites_adhoc.lua @ 12145:212bac94aedd

mod_invites_adhoc: Import from prosody-modules@5001104f0275
author Kim Alvefur <zash@zash.se>
date Wed, 05 Jan 2022 04:37:14 +0100
child 12491:dc0c20753d6c
comparison
equal deleted inserted replaced
12144:3e292e2a1e02 12145:212bac94aedd
1 -- XEP-0401: Easy User Onboarding
2 local dataforms = require "util.dataforms";
3 local datetime = require "util.datetime";
4 local split_jid = require "util.jid".split;
5 local usermanager = require "core.usermanager";
6
7 local new_adhoc = module:require("adhoc").new;
8
9 -- Whether local users can invite other users to create an account on this server
10 local allow_user_invites = module:get_option_boolean("allow_user_invites", false);
11 -- Who can see and use the contact invite command. It is strongly recommended to
12 -- keep this available to all local users. To allow/disallow invite-registration
13 -- on the server, use the option above instead.
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");
17 local deny_user_invite_roles = module:get_option_set("deny_user_invites_by_roles");
18
19 local invites;
20 if prosody.shutdown then -- COMPAT hack to detect prosodyctl
21 invites = module:depends("invites");
22 end
23
24 local invite_result_form = dataforms.new({
25 title = "Your invite has been created",
26 {
27 name = "url" ;
28 var = "landing-url";
29 label = "Invite web page";
30 desc = "Share this link";
31 },
32 {
33 name = "uri";
34 label = "Invite URI";
35 desc = "This alternative link can be opened with some XMPP clients";
36 },
37 {
38 name = "expire";
39 label = "Invite valid until";
40 },
41 });
42
43 -- This is for checking if the specified JID may create invites
44 -- that allow people to register accounts on this host.
45 local function may_invite_new_users(jid)
46 if usermanager.get_roles then
47 local user_roles = usermanager.get_roles(jid, module.host);
48 if not user_roles then return; end
49 if user_roles["prosody:admin"] then
50 return true;
51 end
52 if allow_user_invite_roles then
53 for allowed_role in allow_user_invite_roles do
54 if user_roles[allowed_role] then
55 return true;
56 end
57 end
58 end
59 if deny_user_invite_roles then
60 for denied_role in deny_user_invite_roles do
61 if user_roles[denied_role] then
62 return false;
63 end
64 end
65 end
66 elseif usermanager.is_admin(jid, module.host) then -- COMPAT w/0.11
67 return true; -- Admins may always create invitations
68 end
69 -- No role matches, so whatever the default is
70 return allow_user_invites;
71 end
72
73 module:depends("adhoc");
74
75 -- This command is available to all local users, even if allow_user_invites = false
76 -- If allow_user_invites is false, creating an invite still works, but the invite will
77 -- not be valid for registration on the current server, only for establishing a roster
78 -- subscription.
79 module:provides("adhoc", new_adhoc("Create new contact invite", "urn:xmpp:invite#invite",
80 function (_, data)
81 local username, host = split_jid(data.from);
82 if host ~= module.host then
83 return {
84 status = "completed";
85 error = {
86 message = "This command is only available to users of "..module.host;
87 };
88 };
89 end
90 local invite = invites.create_contact(username, may_invite_new_users(data.from), {
91 source = data.from
92 });
93 --TODO: check errors
94 return {
95 status = "completed";
96 form = {
97 layout = invite_result_form;
98 values = {
99 uri = invite.uri;
100 url = invite.landing_page;
101 expire = datetime.datetime(invite.expires);
102 };
103 };
104 };
105 end, allow_contact_invites and "local_user" or "admin"));
106
107 -- This is an admin-only command that creates a new invitation suitable for registering
108 -- a new account. It does not add the new user to the admin's roster.
109 module:provides("adhoc", new_adhoc("Create new account invite", "urn:xmpp:invite#create-account",
110 function (_, data)
111 local invite = invites.create_account(nil, {
112 source = data.from
113 });
114 --TODO: check errors
115 return {
116 status = "completed";
117 form = {
118 layout = invite_result_form;
119 values = {
120 uri = invite.uri;
121 url = invite.landing_page;
122 expire = datetime.datetime(invite.expires);
123 };
124 };
125 };
126 end, "admin"));