mod_invites_register: Allow roles to be an ordered list, first being primary
authorKim Alvefur <zash@zash.se>
Thu, 30 Mar 2023 11:05:40 +0200
changeset 13015 16b47c3b44f3
parent 13014 3e454af3615d
child 13016 c2616274bef7
mod_invites_register: Allow roles to be an ordered list, first being primary Part of an update to mod_invites and friends to the new authz and roles. Invites with roles in the old way will need to be migrated, but with invites often being short lived it is probably not a long-lived problem.
plugins/mod_invites_register.lua
--- a/plugins/mod_invites_register.lua	Wed Mar 29 17:15:33 2023 +0100
+++ b/plugins/mod_invites_register.lua	Thu Mar 30 11:05:40 2023 +0200
@@ -147,7 +147,20 @@
 	if validated_invite.additional_data then
 		module:log("debug", "Importing roles from invite");
 		local roles = validated_invite.additional_data.roles;
-		if roles then
+		if roles and roles[1] ~= nil then
+			local um = require "prosody.core.usermanager";
+			local ok, err = um.set_user_role(event.username, module.host, roles[1]);
+			if not ok then
+				module:log("error", "Could not set role %s for newly registered user %s: %s", roles[1], event.username, err);
+			end
+			for i = 2, #roles do
+				local ok, err = um.add_user_secondary_role(event.username, module.host, roles[i]);
+				if not ok then
+					module:log("warn", "Could not add secondary role %s for newly registered user %s: %s", roles[i], event.username, err);
+				end
+			end
+		elseif roles and type(next(roles)) == "string" then
+			module:log("warn", "Invite carries legacy, migration required for user '%s' for role set %q to take effect", event.username, roles);
 			module:open_store("roles"):set(contact_username, roles);
 		end
 	end