Diff

plugins/mod_admin_shell.lua @ 12012:71d799a8638f

mod_admin_shell: Allow setting roles when creating user
author Kim Alvefur <zash@zash.se>
date Sat, 04 Dec 2021 02:25:01 +0100
parent 11991:bef2a59b00d1
child 12013:ae45f052b34b
line wrap: on
line diff
--- a/plugins/mod_admin_shell.lua	Mon Dec 06 10:59:14 2021 +0100
+++ b/plugins/mod_admin_shell.lua	Sat Dec 04 02:25:01 2021 +0100
@@ -248,7 +248,7 @@
 		print [[host:deactivate(hostname) - Disconnects all clients on this host and deactivates]]
 		print [[host:list() - List the currently-activated hosts]]
 	elseif section == "user" then
-		print [[user:create(jid, password) - Create the specified user account]]
+		print [[user:create(jid, password, roles) - Create the specified user account]]
 		print [[user:password(jid, password) - Set the password for the specified user account]]
 		print [[user:delete(jid) - Permanently remove the specified user account]]
 		print [[user:list(hostname, pattern) - List users on the specified host, optionally filtering with a pattern]]
@@ -1270,7 +1270,7 @@
 local um = require"core.usermanager";
 
 def_env.user = {};
-function def_env.user:create(jid, password)
+function def_env.user:create(jid, password, roles)
 	local username, host = jid_split(jid);
 	if not prosody.hosts[host] then
 		return nil, "No such host: "..host;
@@ -1279,6 +1279,13 @@
 	end
 	local ok, err = um.create_user(username, password, host);
 	if ok then
+		if ok and roles then
+			if roles == "admin" then roles = "prosody:admin"; end
+			if type(roles) == "string" then roles = { [roles] = true }; end
+			if roles[1] then for i, role in ipairs(roles) do roles[role], roles[i] = true, nil; end end
+			local roles_ok, rerr = um.set_roles(jid, host, roles);
+			if not roles_ok then return nil, "User created, but could not set roles: " .. tostring(rerr); end
+		end
 		return true, "User created";
 	else
 		return nil, "Could not create user: "..err;
@@ -1315,6 +1322,9 @@
 	end
 end
 
+-- TODO user:roles(jid, new_roles)
+
+-- TODO switch to table view, include roles
 function def_env.user:list(host, pat)
 	if not host then
 		return nil, "No host given";