Mercurial > prosody-modules
annotate mod_invites_register/mod_invites_register.lua @ 4386:6357ac65b4eb
mod_groups_internal: allow delay of roster sync on group change
This is useful when executing a bulk change (e.g. a creation of
a group with many members) to avoid O(n^3).
author | Jonas Schäfer <jonas@wielicki.name> |
---|---|
date | Mon, 25 Jan 2021 21:33:38 +0100 |
parents | 205a027d86f3 |
children | 52e1ab99d25e |
rev | line source |
---|---|
4106
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local st = require "util.stanza"; |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 local jid_split = require "util.jid".split; |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 local jid_bare = require "util.jid".bare; |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 local rostermanager = require "core.rostermanager"; |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 |
4384
205a027d86f3
mod_invites_register: port to mod_groups_internal
Jonas Schäfer <jonas@wielicki.name>
parents:
4348
diff
changeset
|
6 local mod_groups = module:depends("groups_internal"); |
205a027d86f3
mod_invites_register: port to mod_groups_internal
Jonas Schäfer <jonas@wielicki.name>
parents:
4348
diff
changeset
|
7 |
4106
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 local require_encryption = module:get_option_boolean("c2s_require_encryption", |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 module:get_option_boolean("require_encryption", false)); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 local invite_only = module:get_option_boolean("registration_invite_only", true); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 local invites; |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 if prosody.shutdown then -- COMPAT hack to detect prosodyctl |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 invites = module:depends("invites"); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 end |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 local invite_stream_feature = st.stanza("register", { xmlns = "urn:xmpp:invite" }):up(); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 module:hook("stream-features", function(event) |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 local session, features = event.origin, event.features; |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 -- Advertise to unauthorized clients only. |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 if session.type ~= "c2s_unauthed" or (require_encryption and not session.secure) then |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 return |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 end |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 features:add_child(invite_stream_feature); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 end); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 -- XEP-0379: Pre-Authenticated Roster Subscription |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 module:hook("presence/bare", function (event) |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 local stanza = event.stanza; |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 if stanza.attr.type ~= "subscribe" then return end |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 local preauth = stanza:get_child("preauth", "urn:xmpp:pars:0"); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 if not preauth then return end |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 local token = preauth.attr.token; |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 if not token then return end |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 local username, host = jid_split(stanza.attr.to); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 local invite, err = invites.get(token, username); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 if not invite then |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 module:log("debug", "Got invalid token, error: %s", err); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 return; |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 end |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 local contact = jid_bare(stanza.attr.from); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 module:log("debug", "Approving inbound subscription to %s from %s", username, contact); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 if rostermanager.set_contact_pending_in(username, host, contact, stanza) then |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 if rostermanager.subscribed(username, host, contact) then |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 invite:use(); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 rostermanager.roster_push(username, host, contact); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 -- Send back a subscription request (goal is mutual subscription) |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 if not rostermanager.is_user_subscribed(username, host, contact) |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 and not rostermanager.is_contact_pending_out(username, host, contact) then |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 module:log("debug", "Sending automatic subscription request to %s from %s", contact, username); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 if rostermanager.set_contact_pending_out(username, host, contact) then |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 rostermanager.roster_push(username, host, contact); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 module:send(st.presence({type = "subscribe", to = contact })); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 else |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 module:log("warn", "Failed to set contact pending out for %s", username); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 end |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 end |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 end |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 end |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 end, 1); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 -- Client is submitting a preauth token to allow registration |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 module:hook("stanza/iq/urn:xmpp:pars:0:preauth", function(event) |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 local preauth = event.stanza.tags[1]; |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 local token = preauth.attr.token; |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 local validated_invite = invites.get(token); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 if not validated_invite then |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 local reply = st.error_reply(event.stanza, "cancel", "forbidden", "The invite token is invalid or expired"); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 event.origin.send(reply); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 return true; |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 end |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 event.origin.validated_invite = validated_invite; |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 local reply = st.reply(event.stanza); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 event.origin.send(reply); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 return true; |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 end); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 -- Registration attempt - ensure a valid preauth token has been supplied |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 module:hook("user-registering", function (event) |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 local validated_invite = event.validated_invite or (event.session and event.session.validated_invite); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 if invite_only and not validated_invite then |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 event.allowed = false; |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 event.reason = "Registration on this server is through invitation only"; |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 return; |
4121
ee8e7f5196b4
mod_invites_register: Fix traceback for non-invite registrations
Matthew Wild <mwild1@gmail.com>
parents:
4120
diff
changeset
|
94 elseif not validated_invite then |
ee8e7f5196b4
mod_invites_register: Fix traceback for non-invite registrations
Matthew Wild <mwild1@gmail.com>
parents:
4120
diff
changeset
|
95 -- This registration is not using an invite, but |
ee8e7f5196b4
mod_invites_register: Fix traceback for non-invite registrations
Matthew Wild <mwild1@gmail.com>
parents:
4120
diff
changeset
|
96 -- the server is not in invite-only mode, so nothing |
ee8e7f5196b4
mod_invites_register: Fix traceback for non-invite registrations
Matthew Wild <mwild1@gmail.com>
parents:
4120
diff
changeset
|
97 -- for this module to do... |
ee8e7f5196b4
mod_invites_register: Fix traceback for non-invite registrations
Matthew Wild <mwild1@gmail.com>
parents:
4120
diff
changeset
|
98 return; |
4106
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 end |
4120
d3c7be9e36d9
mod_invites_register: Fix traceback on registration via other module (thanks franck)
Kim Alvefur <zash@zash.se>
parents:
4106
diff
changeset
|
100 if validated_invite and validated_invite.additional_data and validated_invite.additional_data.allow_reset then |
4106
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 event.allow_reset = validated_invite.additional_data.allow_reset; |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 end |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 end); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 -- Make a *one-way* subscription. User will see when contact is online, |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 -- contact will not see when user is online. |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 function subscribe(host, user_username, contact_username) |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 local user_jid = user_username.."@"..host; |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 local contact_jid = contact_username.."@"..host; |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 -- Update user's roster to say subscription request is pending... |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 rostermanager.set_contact_pending_out(user_username, host, contact_jid); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 -- Update contact's roster to say subscription request is pending... |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 rostermanager.set_contact_pending_in(contact_username, host, user_jid); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 -- Update contact's roster to say subscription request approved... |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 rostermanager.subscribed(contact_username, host, user_jid); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 -- Update user's roster to say subscription request approved... |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 rostermanager.process_inbound_subscription_approval(user_username, host, contact_jid); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 end |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 -- Make a mutual subscription between jid1 and jid2. Each JID will see |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 -- when the other one is online. |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 function subscribe_both(host, user1, user2) |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 subscribe(host, user1, user2); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 subscribe(host, user2, user1); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 end |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 -- Registration successful, if there was a preauth token, mark it as used |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 module:hook("user-registered", function (event) |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 local validated_invite = event.validated_invite or (event.session and event.session.validated_invite); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 if not validated_invite then |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 return; |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
132 end |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 local inviter_username = validated_invite.inviter; |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 local contact_username = event.username; |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 validated_invite:use(); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 if inviter_username then |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 module:log("debug", "Creating mutual subscription between %s and %s", inviter_username, contact_username); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 subscribe_both(module.host, inviter_username, contact_username); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 end |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 if validated_invite.additional_data then |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 module:log("debug", "Importing roles from invite"); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 local roles = validated_invite.additional_data.roles; |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 if roles then |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 module:open_store("roles"):set(contact_username, roles); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
147 end |
4348
46fb40d7f65a
mod_invites_register: Add user to groups included in an invite
Matthew Wild <mwild1@gmail.com>
parents:
4121
diff
changeset
|
148 |
46fb40d7f65a
mod_invites_register: Add user to groups included in an invite
Matthew Wild <mwild1@gmail.com>
parents:
4121
diff
changeset
|
149 local groups = validated_invite.additional_data.groups; |
46fb40d7f65a
mod_invites_register: Add user to groups included in an invite
Matthew Wild <mwild1@gmail.com>
parents:
4121
diff
changeset
|
150 if groups then |
46fb40d7f65a
mod_invites_register: Add user to groups included in an invite
Matthew Wild <mwild1@gmail.com>
parents:
4121
diff
changeset
|
151 module:log("Adding to groups..."); |
46fb40d7f65a
mod_invites_register: Add user to groups included in an invite
Matthew Wild <mwild1@gmail.com>
parents:
4121
diff
changeset
|
152 for _, group in ipairs(groups) do |
4384
205a027d86f3
mod_invites_register: port to mod_groups_internal
Jonas Schäfer <jonas@wielicki.name>
parents:
4348
diff
changeset
|
153 mod_groups.add_member(group, contact_username); |
4348
46fb40d7f65a
mod_invites_register: Add user to groups included in an invite
Matthew Wild <mwild1@gmail.com>
parents:
4121
diff
changeset
|
154 end |
46fb40d7f65a
mod_invites_register: Add user to groups included in an invite
Matthew Wild <mwild1@gmail.com>
parents:
4121
diff
changeset
|
155 end |
4106
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 end |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 end); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
158 |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 -- Equivalent of user-registered but for when the account already existed |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 -- (i.e. password reset) |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 module:hook("user-password-reset", function (event) |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 local validated_invite = event.validated_invite or (event.session and event.session.validated_invite); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
163 if not validated_invite then |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
164 return; |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 end |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
166 validated_invite:use(); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
167 end); |
d34572047488
mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
168 |