# HG changeset patch # User Matthew Wild # Date 1688049106 -3600 # Node ID 7b6e7290265b4b5f3c4ccabc2301e88073f25596 # Parent 5360554769125d93da0490188518334577ffa299 usermanager: Add create_user_with_role() method to atomically set initial role diff -r 536055476912 -r 7b6e7290265b core/usermanager.lua --- a/core/usermanager.lua Wed Jun 28 17:17:20 2023 +0200 +++ b/core/usermanager.lua Thu Jun 29 15:31:46 2023 +0100 @@ -205,6 +205,31 @@ return role, err; end +local function create_user_with_role(username, password, host, role) + local ok, err = create_user(username, nil, host); + if not ok then return ok, err; end + + local role_ok, role_err = set_user_role(username, host, role); + if not role_ok then + delete_user(username, host); + return nil, "Failed to assign role: "..role_err; + end + + if password then + local pw_ok, pw_err = set_password(username, password, host); + if not pw_ok then + return nil, "Failed to set password: "..pw_err; + end + + local enable_ok, enable_err = enable_user(username, host); + if not enable_ok and enable_err ~= "method not implemented" then + return enable_ok, "Failed to enable account: "..enable_err; + end + end + + return true; +end + local function user_can_assume_role(user, host, role_name) if host and not hosts[host] then return false; end if type(user) ~= "string" then return false; end @@ -308,6 +333,7 @@ get_account_info = get_account_info; user_exists = user_exists; create_user = create_user; + create_user_with_role = create_user_with_role; delete_user = delete_user; user_is_enabled = user_is_enabled; enable_user = enable_user;