comparison mod_http_admin_api/mod_http_admin_api.lua @ 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 5bc706c2db8f
children cade5dac1003
comparison
equal deleted inserted replaced
4516:5bc706c2db8f 4517:d6a3201a65c0
1 local usermanager = require "core.usermanager"; 1 local usermanager = require "core.usermanager";
2 2
3 local json = require "util.json"; 3 local json = require "util.json";
4 local st = require "util.stanza"; 4 local st = require "util.stanza";
5 local array = require "util.array";
5 6
6 module:depends("http"); 7 module:depends("http");
7 8
8 local invites = module:depends("invites"); 9 local invites = module:depends("invites");
9 local tokens = module:depends("tokenauth"); 10 local tokens = module:depends("tokenauth");
172 if ok and nick_item then 173 if ok and nick_item then
173 display_name = nick_item:get_child_text("nick", xmlns_nick); 174 display_name = nick_item:get_child_text("nick", xmlns_nick);
174 end 175 end
175 end 176 end
176 177
178 local roles = nil;
179 if usermanager.get_roles then
180 local roles_map = usermanager.get_roles(username.."@"..module.host, module.host)
181 roles = array()
182 if roles_map then
183 for role in pairs(roles_map) do
184 roles:push(role)
185 end
186 end
187 end
188
177 return { 189 return {
178 username = username; 190 username = username;
179 display_name = display_name; 191 display_name = display_name;
192 roles = roles;
180 }; 193 };
181 end 194 end
182 195
183 local function get_session_debug_info(session) 196 local function get_session_debug_info(session)
184 local info = { 197 local info = {
390 _defaults_only = true; 403 _defaults_only = true;
391 }) then 404 }) then
392 final_user.display_name = new_user.display_name; 405 final_user.display_name = new_user.display_name;
393 end 406 end
394 end 407 end
408
409 if new_user.roles then
410 if not usermanager.set_roles then
411 return 500, "feature-not-implemented"
412 end
413
414 local backend_roles = {};
415 for _, role in ipairs(new_user.roles) do
416 backend_roles[role] = true;
417 end
418 local jid = username.."@"..module.host;
419 if not usermanager.set_roles(jid, module.host, backend_roles) then
420 module:log("error", "failed to set roles %q for %s", backend_roles, jid)
421 return 500
422 end
423 end
424
395 return 200; 425 return 200;
396 end 426 end
397 427
398 function delete_user(event, username) --luacheck: ignore 212/event 428 function delete_user(event, username) --luacheck: ignore 212/event
399 if not usermanager.delete_user(username, module.host) then 429 if not usermanager.delete_user(username, module.host) then