Software / code / prosody
Comparison
plugins/mod_admin_shell.lua @ 12013:ae45f052b34b
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.
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Mon, 06 Dec 2021 21:55:57 +0100 |
| parent | 12012:71d799a8638f |
| child | 12014:efbf288b529e |
comparison
equal
deleted
inserted
replaced
| 12012:71d799a8638f | 12013:ae45f052b34b |
|---|---|
| 248 print [[host:deactivate(hostname) - Disconnects all clients on this host and deactivates]] | 248 print [[host:deactivate(hostname) - Disconnects all clients on this host and deactivates]] |
| 249 print [[host:list() - List the currently-activated hosts]] | 249 print [[host:list() - List the currently-activated hosts]] |
| 250 elseif section == "user" then | 250 elseif section == "user" then |
| 251 print [[user:create(jid, password, roles) - Create the specified user account]] | 251 print [[user:create(jid, password, roles) - Create the specified user account]] |
| 252 print [[user:password(jid, password) - Set the password for the specified user account]] | 252 print [[user:password(jid, password) - Set the password for the specified user account]] |
| 253 print [[user:roles(jid, roles) - Set roles for an user]] | |
| 253 print [[user:delete(jid) - Permanently remove the specified user account]] | 254 print [[user:delete(jid) - Permanently remove the specified user account]] |
| 254 print [[user:list(hostname, pattern) - List users on the specified host, optionally filtering with a pattern]] | 255 print [[user:list(hostname, pattern) - List users on the specified host, optionally filtering with a pattern]] |
| 255 elseif section == "muc" then | 256 elseif section == "muc" then |
| 256 -- TODO `muc:room():foo()` commands | 257 -- TODO `muc:room():foo()` commands |
| 257 print [[muc:create(roomjid, { config }) - Create the specified MUC room with the given config]] | 258 print [[muc:create(roomjid, { config }) - Create the specified MUC room with the given config]] |
| 1267 return true, c.." rooms"; | 1268 return true, c.." rooms"; |
| 1268 end | 1269 end |
| 1269 | 1270 |
| 1270 local um = require"core.usermanager"; | 1271 local um = require"core.usermanager"; |
| 1271 | 1272 |
| 1273 local function coerce_roles(roles) | |
| 1274 if roles == "admin" then roles = "prosody:admin"; end | |
| 1275 if type(roles) == "string" then roles = { [roles] = true }; end | |
| 1276 if roles[1] then for i, role in ipairs(roles) do roles[role], roles[i] = true, nil; end end | |
| 1277 return roles; | |
| 1278 end | |
| 1279 | |
| 1272 def_env.user = {}; | 1280 def_env.user = {}; |
| 1273 function def_env.user:create(jid, password, roles) | 1281 function def_env.user:create(jid, password, roles) |
| 1274 local username, host = jid_split(jid); | 1282 local username, host = jid_split(jid); |
| 1275 if not prosody.hosts[host] then | 1283 if not prosody.hosts[host] then |
| 1276 return nil, "No such host: "..host; | 1284 return nil, "No such host: "..host; |
| 1278 return nil, "User exists"; | 1286 return nil, "User exists"; |
| 1279 end | 1287 end |
| 1280 local ok, err = um.create_user(username, password, host); | 1288 local ok, err = um.create_user(username, password, host); |
| 1281 if ok then | 1289 if ok then |
| 1282 if ok and roles then | 1290 if ok and roles then |
| 1283 if roles == "admin" then roles = "prosody:admin"; end | 1291 roles = coerce_roles(roles); |
| 1284 if type(roles) == "string" then roles = { [roles] = true }; end | |
| 1285 if roles[1] then for i, role in ipairs(roles) do roles[role], roles[i] = true, nil; end end | |
| 1286 local roles_ok, rerr = um.set_roles(jid, host, roles); | 1292 local roles_ok, rerr = um.set_roles(jid, host, roles); |
| 1287 if not roles_ok then return nil, "User created, but could not set roles: " .. tostring(rerr); end | 1293 if not roles_ok then return nil, "User created, but could not set roles: " .. tostring(rerr); end |
| 1288 end | 1294 end |
| 1289 return true, "User created"; | 1295 return true, "User created"; |
| 1290 else | 1296 else |
| 1320 else | 1326 else |
| 1321 return nil, "Could not change password for user: "..err; | 1327 return nil, "Could not change password for user: "..err; |
| 1322 end | 1328 end |
| 1323 end | 1329 end |
| 1324 | 1330 |
| 1325 -- TODO user:roles(jid, new_roles) | 1331 -- user:roles("someone@example.com", {"prosody:admin"}) |
| 1332 function def_env.user:roles(jid, new_roles) | |
| 1333 local username, host = jid_split(jid); | |
| 1334 if not prosody.hosts[host] then | |
| 1335 return nil, "No such host: "..host; | |
| 1336 elseif not um.user_exists(username, host) then | |
| 1337 return nil, "No such user"; | |
| 1338 end | |
| 1339 return um.set_roles(jid, host, coerce_roles(new_roles)); | |
| 1340 end | |
| 1326 | 1341 |
| 1327 -- TODO switch to table view, include roles | 1342 -- TODO switch to table view, include roles |
| 1328 function def_env.user:list(host, pat) | 1343 function def_env.user:list(host, pat) |
| 1329 if not host then | 1344 if not host then |
| 1330 return nil, "No host given"; | 1345 return nil, "No host given"; |