Software / code / prosody
Comparison
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 |
comparison
equal
deleted
inserted
replaced
| 12011:9dc36fdbdba1 | 12012:71d799a8638f |
|---|---|
| 246 elseif section == "host" then | 246 elseif section == "host" then |
| 247 print [[host:activate(hostname) - Activates the specified host]] | 247 print [[host:activate(hostname) - Activates the specified host]] |
| 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) - 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:delete(jid) - Permanently remove the specified user account]] | 253 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]] | 254 print [[user:list(hostname, pattern) - List users on the specified host, optionally filtering with a pattern]] |
| 255 elseif section == "muc" then | 255 elseif section == "muc" then |
| 256 -- TODO `muc:room():foo()` commands | 256 -- TODO `muc:room():foo()` commands |
| 1268 end | 1268 end |
| 1269 | 1269 |
| 1270 local um = require"core.usermanager"; | 1270 local um = require"core.usermanager"; |
| 1271 | 1271 |
| 1272 def_env.user = {}; | 1272 def_env.user = {}; |
| 1273 function def_env.user:create(jid, password) | 1273 function def_env.user:create(jid, password, roles) |
| 1274 local username, host = jid_split(jid); | 1274 local username, host = jid_split(jid); |
| 1275 if not prosody.hosts[host] then | 1275 if not prosody.hosts[host] then |
| 1276 return nil, "No such host: "..host; | 1276 return nil, "No such host: "..host; |
| 1277 elseif um.user_exists(username, host) then | 1277 elseif um.user_exists(username, host) then |
| 1278 return nil, "User exists"; | 1278 return nil, "User exists"; |
| 1279 end | 1279 end |
| 1280 local ok, err = um.create_user(username, password, host); | 1280 local ok, err = um.create_user(username, password, host); |
| 1281 if ok then | 1281 if ok then |
| 1282 if ok and roles then | |
| 1283 if roles == "admin" then roles = "prosody:admin"; end | |
| 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); | |
| 1287 if not roles_ok then return nil, "User created, but could not set roles: " .. tostring(rerr); end | |
| 1288 end | |
| 1282 return true, "User created"; | 1289 return true, "User created"; |
| 1283 else | 1290 else |
| 1284 return nil, "Could not create user: "..err; | 1291 return nil, "Could not create user: "..err; |
| 1285 end | 1292 end |
| 1286 end | 1293 end |
| 1313 else | 1320 else |
| 1314 return nil, "Could not change password for user: "..err; | 1321 return nil, "Could not change password for user: "..err; |
| 1315 end | 1322 end |
| 1316 end | 1323 end |
| 1317 | 1324 |
| 1325 -- TODO user:roles(jid, new_roles) | |
| 1326 | |
| 1327 -- TODO switch to table view, include roles | |
| 1318 function def_env.user:list(host, pat) | 1328 function def_env.user:list(host, pat) |
| 1319 if not host then | 1329 if not host then |
| 1320 return nil, "No host given"; | 1330 return nil, "No host given"; |
| 1321 elseif not prosody.hosts[host] then | 1331 elseif not prosody.hosts[host] then |
| 1322 return nil, "No such host"; | 1332 return nil, "No such host"; |