comparison mod_http_admin_api/mod_http_admin_api.lua @ 5005:d68348323406

mod_http_admin_api: Update for new new role API
author Matthew Wild <mwild1@gmail.com>
date Thu, 18 Aug 2022 10:36:35 +0100
parents 1b5869c34026
children cc89c97befe7
comparison
equal deleted inserted replaced
5004:bc75fc9400ae 5005:d68348323406
1 local usermanager = require "core.usermanager"; 1 local usermanager = require "core.usermanager";
2 2
3 local it = require "util.iterators";
3 local json = require "util.json"; 4 local json = require "util.json";
4 local st = require "util.stanza"; 5 local st = require "util.stanza";
5 local array = require "util.array"; 6 local array = require "util.array";
6 local statsmanager = require "core.statsmanager"; 7 local statsmanager = require "core.statsmanager";
7 8
176 if ok and nick_item then 177 if ok and nick_item then
177 display_name = nick_item:get_child_text("nick", xmlns_nick); 178 display_name = nick_item:get_child_text("nick", xmlns_nick);
178 end 179 end
179 end 180 end
180 181
181 local roles = array(); 182 local primary_role, secondary_roles, legacy_roles;
182 local roles_map = usermanager.get_user_roles(username, module.host); 183 if usermanager.get_user_role then
183 for role_name in pairs(roles_map) do 184 primary_role = usermanager.get_user_role(username, module.host);
184 roles:push(role_name); 185 secondary_roles = array.collect(it.keys(usermanager.get_user_secondary_roles(username, module.host)));
186 elseif usermanager.get_user_roles then -- COMPAT w/0.12
187 legacy_roles = array();
188 local roles_map = usermanager.get_user_roles(username, module.host);
189 for role_name in pairs(roles_map) do
190 legacy_roles:push(role_name);
191 end
185 end 192 end
186 193
187 return { 194 return {
188 username = username; 195 username = username;
189 display_name = display_name; 196 display_name = display_name;
190 roles = roles; 197 role = primary_role and primary_role.name or nil;
198 secondary_roles = secondary_roles;
199 roles = legacy_roles; -- COMPAT w/0.12
191 }; 200 };
192 end 201 end
193 202
194 local function get_session_debug_info(session) 203 local function get_session_debug_info(session)
195 local info = { 204 local info = {
301 local debug_info = { 310 local debug_info = {
302 time = os.time(); 311 time = os.time();
303 }; 312 };
304 -- Online sessions 313 -- Online sessions
305 do 314 do
306 local user_sessions = hosts[module.host].sessions[username]; 315 local user_sessions = prosody.hosts[module.host].sessions[username];
307 if user_sessions then 316 if user_sessions then
308 user_sessions = user_sessions.sessions 317 user_sessions = user_sessions.sessions
309 end 318 end
310 local sessions = {}; 319 local sessions = {};
311 if user_sessions then 320 if user_sessions then
407 }) then 416 }) then
408 final_user.display_name = new_user.display_name; 417 final_user.display_name = new_user.display_name;
409 end 418 end
410 end 419 end
411 420
412 if new_user.roles then 421 if new_user.role then
422 if not usermanager.set_user_role then
423 return 500, "feature-not-implemented";
424 end
425 if not usermanager.set_user_role(username, module.host, new_user.role) then
426 module:log("error", "failed to set role %s for %s", new_user.role, username);
427 return 500;
428 end
429 end
430
431 if new_user.roles then -- COMPAT w/0.12
413 if not usermanager.set_user_roles then 432 if not usermanager.set_user_roles then
414 return 500, "feature-not-implemented" 433 return 500, "feature-not-implemented"
415 end 434 end
416 435
417 local backend_roles = {}; 436 local backend_roles = {};