Changeset

4517:d6a3201a65c0

mod_http_admin_api: support for updating user roles via the API Requires the most recent of the recent prosody-trunks.
author Jonas Schäfer <jonas@wielicki.name>
date Mon, 22 Mar 2021 21:32:43 +0100
parents 4516:5bc706c2db8f
children 4518:073f5397c1d2
files mod_http_admin_api/mod_http_admin_api.lua mod_http_admin_api/openapi.yaml
diffstat 2 files changed, 31 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/mod_http_admin_api/mod_http_admin_api.lua	Mon Mar 22 17:52:28 2021 +0100
+++ b/mod_http_admin_api/mod_http_admin_api.lua	Mon Mar 22 21:32:43 2021 +0100
@@ -2,6 +2,7 @@
 
 local json = require "util.json";
 local st = require "util.stanza";
+local array = require "util.array";
 
 module:depends("http");
 
@@ -174,9 +175,21 @@
 		end
 	end
 
+	local roles = nil;
+	if usermanager.get_roles then
+		local roles_map = usermanager.get_roles(username.."@"..module.host, module.host)
+		roles = array()
+		if roles_map then
+			for role in pairs(roles_map) do
+				roles:push(role)
+			end
+		end
+	end
+
 	return {
 		username = username;
 		display_name = display_name;
+		roles = roles;
 	};
 end
 
@@ -392,6 +405,23 @@
 			final_user.display_name = new_user.display_name;
 		end
 	end
+
+	if new_user.roles then
+		if not usermanager.set_roles then
+			return 500, "feature-not-implemented"
+		end
+
+		local backend_roles = {};
+		for _, role in ipairs(new_user.roles) do
+			backend_roles[role] = true;
+		end
+		local jid = username.."@"..module.host;
+		if not usermanager.set_roles(jid, module.host, backend_roles) then
+			module:log("error", "failed to set roles %q for %s", backend_roles, jid)
+			return 500
+		end
+	end
+
 	return 200;
 end
 
--- a/mod_http_admin_api/openapi.yaml	Mon Mar 22 17:52:28 2021 +0100
+++ b/mod_http_admin_api/openapi.yaml	Mon Mar 22 21:32:43 2021 +0100
@@ -61,7 +61,7 @@
     put:
       tags:
       - user
-      summary: Updated user (NYI)
+      summary: Updated user
       description: Update a user
       operationId: updateUser
       parameters: