comparison mod_http_admin_api/mod_http_admin_api.lua @ 5783:6c8cf3ecb885

mod_http_admin_api: Support for setting user account enabled status
author Matthew Wild <mwild1@gmail.com>
date Thu, 07 Dec 2023 15:41:18 +0000
parents 5621dece3ee4
children 5c589fab6f53
comparison
equal deleted inserted replaced
5782:5621dece3ee4 5783:6c8cf3ecb885
423 423
424 event.response.headers["Content-Type"] = json_content_type; 424 event.response.headers["Content-Type"] = json_content_type;
425 return json.encode(user_info); 425 return json.encode(user_info);
426 end 426 end
427 427
428 local user_attribute_writers = {
429 enabled = function (username, enabled)
430 local ok, err;
431 if enabled == true then
432 ok, err = usermanager.enable_user(username, module.host);
433 elseif enabled == false then
434 ok, err = usermanager.disable_user(username, module.host);
435 else
436 ok, err = nil, "Invalid value provided for 'enabled'";
437 end
438 if not ok then
439 module:log("error", "Unable to %s user '%s': %s", enabled and "enable" or "disable", username, err);
440 return nil, err;
441 end
442 return true;
443 end;
444 };
445 local writable_user_attributes = set.new(array.collect(it.keys(user_attribute_writers)));
446
428 function update_user(event, username) 447 function update_user(event, username)
429 local current_user = get_user_info(username); 448 local current_user = get_user_info(username);
430 449
431 local request = event.request; 450 local request = event.request;
432 if request.headers.content_type ~= json_content_type 451 if request.headers.content_type ~= json_content_type
478 end 497 end
479 local jid = username.."@"..module.host; 498 local jid = username.."@"..module.host;
480 if not usermanager.set_user_roles(username, module.host, backend_roles) then 499 if not usermanager.set_user_roles(username, module.host, backend_roles) then
481 module:log("error", "failed to set roles %q for %s", backend_roles, jid) 500 module:log("error", "failed to set roles %q for %s", backend_roles, jid)
482 return 500 501 return 500
502 end
503 end
504
505 if new_user.enabled ~= nil then
506 if not user_attribute_writers.enabled(username, new_user.enabled) then
507 return 500;
483 end 508 end
484 end 509 end
485 510
486 return 200; 511 return 200;
487 end 512 end