aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/mod_admin_shell.lua
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2021-12-06 21:55:57 +0100
committerKim Alvefur <zash@zash.se>2021-12-06 21:55:57 +0100
commitefa1f442ae1cce4bfd537ab3a9ab27acd05a6573 (patch)
tree9af86db0080affb85cf3b5f32cf39144329d4a68 /plugins/mod_admin_shell.lua
parent95fae62ac5eb3b2a5d5a5035a3994d98e6af47df (diff)
downloadprosody-efa1f442ae1cce4bfd537ab3a9ab27acd05a6573.tar.gz
prosody-efa1f442ae1cce4bfd537ab3a9ab27acd05a6573.zip
mod_admin_shell: Add command for updating roles user:roles(jid, roles)
This would allow e.g. granting admin status without changing the config and without a restart.
Diffstat (limited to 'plugins/mod_admin_shell.lua')
-rw-r--r--plugins/mod_admin_shell.lua23
1 files changed, 19 insertions, 4 deletions
diff --git a/plugins/mod_admin_shell.lua b/plugins/mod_admin_shell.lua
index 2455cbcb..5e021173 100644
--- a/plugins/mod_admin_shell.lua
+++ b/plugins/mod_admin_shell.lua
@@ -250,6 +250,7 @@ function commands.help(session, data)
elseif section == "user" then
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:roles(jid, roles) - Set roles for an user]]
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]]
elseif section == "muc" then
@@ -1269,6 +1270,13 @@ end
local um = require"core.usermanager";
+local function coerce_roles(roles)
+ 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
+ return roles;
+end
+
def_env.user = {};
function def_env.user:create(jid, password, roles)
local username, host = jid_split(jid);
@@ -1280,9 +1288,7 @@ function def_env.user:create(jid, password, roles)
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
+ roles = coerce_roles(roles);
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
@@ -1322,7 +1328,16 @@ function def_env.user:password(jid, password)
end
end
--- TODO user:roles(jid, new_roles)
+-- user:roles("someone@example.com", {"prosody:admin"})
+function def_env.user:roles(jid, new_roles)
+ local username, host = jid_split(jid);
+ if not prosody.hosts[host] then
+ return nil, "No such host: "..host;
+ elseif not um.user_exists(username, host) then
+ return nil, "No such user";
+ end
+ return um.set_roles(jid, host, coerce_roles(new_roles));
+end
-- TODO switch to table view, include roles
function def_env.user:list(host, pat)