Software /
code /
prosody-modules
Annotate
mod_http_admin_api/mod_http_admin_api.lua @ 5917:5db53d619e66
mod_csi_battery_saver: add xep number to mds comment
author | tmolitor <thilo@eightysoft.de> |
---|---|
date | Mon, 29 Apr 2024 01:31:40 +0200 |
parent | 5915:512f912fdfa5 |
child | 5919:095030677ae6 |
rev | line source |
---|---|
4345
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
1 local usermanager = require "core.usermanager"; |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
2 |
5807
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
3 local array = require "util.array"; |
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
4 local it = require "util.iterators"; |
5284
5178c13deb78
mod_http_admin_api: Fix missing import
Kim Alvefur <zash@zash.se>
parents:
5283
diff
changeset
|
5 local jid = require "util.jid"; |
4343
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 local json = require "util.json"; |
5807
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
7 local set = require "util.set"; |
4516
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
8 local st = require "util.stanza"; |
4576
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
9 local statsmanager = require "core.statsmanager"; |
4343
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 module:depends("http"); |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 |
4576
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
13 local announce = module:depends("announce"); |
4343
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 local invites = module:depends("invites"); |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 local tokens = module:depends("tokenauth"); |
4345
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
16 local mod_pep = module:depends("pep"); |
4382
0d6b69777bc1
mod_http_admin_api: port to use new mod_groups_internal module
Jonas Schäfer <jonas@wielicki.name>
parents:
4378
diff
changeset
|
17 local mod_groups = module:depends("groups_internal"); |
5798
4c3216d9b118
mod_http_admin_api: Include user account status and activity in get_user_info
Matthew Wild <mwild1@gmail.com>
parents:
5710
diff
changeset
|
18 local mod_lastlog2 = module:depends("lastlog2"); |
4343
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 |
4363
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
20 local push_errors = module:shared("cloud_notify/push_errors"); |
4352
f6da234b21b8
mod_http_admin_api: Add groups property to users
Matthew Wild <mwild1@gmail.com>
parents:
4351
diff
changeset
|
21 |
4371
3d01bc4547b2
mod_http_admin_api: Add /server/info endpoint for site_name and version
Matthew Wild <mwild1@gmail.com>
parents:
4369
diff
changeset
|
22 local site_name = module:get_option_string("site_name", module.host); |
3d01bc4547b2
mod_http_admin_api: Add /server/info endpoint for site_name and version
Matthew Wild <mwild1@gmail.com>
parents:
4369
diff
changeset
|
23 |
4776
13e913471b75
mod_http_admin_api: Ensure freshness of metrics when in manual mode
Kim Alvefur <zash@zash.se>
parents:
4714
diff
changeset
|
24 local manual_stats_collection = module:context("*"):get_option("statistics_interval") == "manual"; |
13e913471b75
mod_http_admin_api: Ensure freshness of metrics when in manual mode
Kim Alvefur <zash@zash.se>
parents:
4714
diff
changeset
|
25 |
4343
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 local json_content_type = "application/json"; |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 local www_authenticate_header = ("Bearer realm=%q"):format(module.host.."/"..module.name); |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 |
4516
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
30 local xmlns_pubsub = "http://jabber.org/protocol/pubsub"; |
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
31 local xmlns_nick = "http://jabber.org/protocol/nick"; |
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
32 |
5798
4c3216d9b118
mod_http_admin_api: Include user account status and activity in get_user_info
Matthew Wild <mwild1@gmail.com>
parents:
5710
diff
changeset
|
33 assert(mod_lastlog2.get_last_active, "Newer version of mod_lastlog2 is required to use this module"); |
4c3216d9b118
mod_http_admin_api: Include user account status and activity in get_user_info
Matthew Wild <mwild1@gmail.com>
parents:
5710
diff
changeset
|
34 |
5802
6ab9e1763f35
mod_http_admin_api: Include information about pending deletion request, if any
Matthew Wild <mwild1@gmail.com>
parents:
5798
diff
changeset
|
35 local deleted_users = module:open_store("accounts_cleanup"); |
6ab9e1763f35
mod_http_admin_api: Include information about pending deletion request, if any
Matthew Wild <mwild1@gmail.com>
parents:
5798
diff
changeset
|
36 |
4343
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 local function check_credentials(request) |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 local auth_type, auth_data = string.match(request.headers.authorization or "", "^(%S+)%s(.+)$"); |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 if not (auth_type and auth_data) then |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 return false; |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 end |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 if auth_type == "Bearer" then |
4997
1b5869c34026
mod_http_admin_api: Updates for new role auth API in Prosody (trunk/0.13 only)
Matthew Wild <mwild1@gmail.com>
parents:
4956
diff
changeset
|
44 return tokens.get_token_session(auth_data); |
4343
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 end |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 return nil; |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 end |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 |
4997
1b5869c34026
mod_http_admin_api: Updates for new role auth API in Prosody (trunk/0.13 only)
Matthew Wild <mwild1@gmail.com>
parents:
4956
diff
changeset
|
49 module:default_permission("prosody:admin", ":access-admin-api"); |
1b5869c34026
mod_http_admin_api: Updates for new role auth API in Prosody (trunk/0.13 only)
Matthew Wild <mwild1@gmail.com>
parents:
4956
diff
changeset
|
50 |
4343
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 function check_auth(routes) |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 local function check_request_auth(event) |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 local session = check_credentials(event.request); |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 if not session then |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 event.response.headers.authorization = www_authenticate_header; |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 return false, 401; |
4997
1b5869c34026
mod_http_admin_api: Updates for new role auth API in Prosody (trunk/0.13 only)
Matthew Wild <mwild1@gmail.com>
parents:
4956
diff
changeset
|
57 end |
5283
cc89c97befe7
mod_http_admin_api: Tweak token session to please module:may()
Kim Alvefur <zash@zash.se>
parents:
5005
diff
changeset
|
58 -- FIXME this should probably live in mod_tokenauth or similar |
cc89c97befe7
mod_http_admin_api: Tweak token session to please module:may()
Kim Alvefur <zash@zash.se>
parents:
5005
diff
changeset
|
59 session.type = "c2s"; |
cc89c97befe7
mod_http_admin_api: Tweak token session to please module:may()
Kim Alvefur <zash@zash.se>
parents:
5005
diff
changeset
|
60 session.full_jid = jid.join(session.username, session.host, session.resource); |
4997
1b5869c34026
mod_http_admin_api: Updates for new role auth API in Prosody (trunk/0.13 only)
Matthew Wild <mwild1@gmail.com>
parents:
4956
diff
changeset
|
61 event.session = session; |
1b5869c34026
mod_http_admin_api: Updates for new role auth API in Prosody (trunk/0.13 only)
Matthew Wild <mwild1@gmail.com>
parents:
4956
diff
changeset
|
62 if not module:may(":access-admin-api", event) then |
4343
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 return false, 403; |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 end |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 return true; |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 end |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 for route, handler in pairs(routes) do |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 routes[route] = function (event, ...) |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 local permit, code = check_request_auth(event); |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 if not permit then |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 return code; |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 end |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 return handler(event, ...); |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 end; |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 end |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 return routes; |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 end |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 local function token_info_to_invite_info(token_info) |
4349
5ca36c36ab05
mod_http_admin_api: Expose new invite properties: groups, source and reusable
Matthew Wild <mwild1@gmail.com>
parents:
4345
diff
changeset
|
81 local additional_data = token_info.additional_data; |
5ca36c36ab05
mod_http_admin_api: Expose new invite properties: groups, source and reusable
Matthew Wild <mwild1@gmail.com>
parents:
4345
diff
changeset
|
82 local groups = additional_data and additional_data.groups or nil; |
5912
432efc39572f
mod_http_admin_api: Return roles for existing invites, if any
Matthew Wild <mwild1@gmail.com>
parents:
5911
diff
changeset
|
83 local roles = additional_data and additional_data.roles or nil; |
4349
5ca36c36ab05
mod_http_admin_api: Expose new invite properties: groups, source and reusable
Matthew Wild <mwild1@gmail.com>
parents:
4345
diff
changeset
|
84 local source = additional_data and additional_data.source or nil; |
4378
d4e0e4d22fc7
mod_http_admin_api: Add 'reset' property to returned invite objects
Matthew Wild <mwild1@gmail.com>
parents:
4375
diff
changeset
|
85 local reset = not not (additional_data and additional_data.allow_reset or nil); |
4343
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 return { |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 id = token_info.token; |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 type = token_info.type; |
4358
d3e0fe470877
mod_http_admin_api: Ensure 'reusable' flag is always present on an invite
Matthew Wild <mwild1@gmail.com>
parents:
4357
diff
changeset
|
89 reusable = not not token_info.reusable; |
4343
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 inviter = token_info.inviter; |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 jid = token_info.jid; |
4354
d61d7d30f38d
mod_http_admin_api: Add XMPP URI into invite objects
Matthew Wild <mwild1@gmail.com>
parents:
4353
diff
changeset
|
92 uri = token_info.uri; |
4343
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 landing_page = token_info.landing_page; |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 created_at = token_info.created_at; |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 expires = token_info.expires; |
4349
5ca36c36ab05
mod_http_admin_api: Expose new invite properties: groups, source and reusable
Matthew Wild <mwild1@gmail.com>
parents:
4345
diff
changeset
|
96 groups = groups; |
5912
432efc39572f
mod_http_admin_api: Return roles for existing invites, if any
Matthew Wild <mwild1@gmail.com>
parents:
5911
diff
changeset
|
97 roles = roles; |
4349
5ca36c36ab05
mod_http_admin_api: Expose new invite properties: groups, source and reusable
Matthew Wild <mwild1@gmail.com>
parents:
4345
diff
changeset
|
98 source = source; |
4378
d4e0e4d22fc7
mod_http_admin_api: Add 'reset' property to returned invite objects
Matthew Wild <mwild1@gmail.com>
parents:
4375
diff
changeset
|
99 reset = reset; |
4343
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 }; |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 end |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 function list_invites(event) |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 local invites_list = {}; |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 for token, invite in invites.pending_account_invites() do --luacheck: ignore 213/token |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 table.insert(invites_list, token_info_to_invite_info(invite)); |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 end |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 table.sort(invites_list, function (a, b) |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 return a.created_at < b.created_at; |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 end); |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 event.response.headers["Content-Type"] = json_content_type; |
4350
270025e76bf8
mod_http_admin_api: Use json.encode_array() when returning an array
Matthew Wild <mwild1@gmail.com>
parents:
4349
diff
changeset
|
113 return json.encode_array(invites_list); |
4343
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 end |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 function get_invite_by_id(event, invite_id) |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 local invite = invites.get_account_invite_info(invite_id); |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 if not invite then |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 return 404; |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 end |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 event.response.headers["Content-Type"] = json_content_type; |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 return json.encode(token_info_to_invite_info(invite)); |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 end |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 |
4374
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
126 function create_invite_type(event, invite_type) |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
127 local options; |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
128 |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
129 local request = event.request; |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
130 if request.body and #request.body > 0 then |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
131 if request.headers.content_type ~= json_content_type then |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
132 module:log("warn", "Invalid content type"); |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
133 return 400; |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
134 end |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
135 options = json.decode(event.request.body); |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
136 if not options then |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
137 module:log("warn", "Invalid JSON"); |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
138 return 400; |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
139 end |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
140 else |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
141 options = {}; |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
142 end |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
143 |
4396
de55e1475808
mod_http_admin_api: re-add information about invite creator
Jonas Schäfer <jonas@wielicki.name>
parents:
4393
diff
changeset
|
144 local source = event.session.username .. "@" .. module.host .. "/admin_api"; |
de55e1475808
mod_http_admin_api: re-add information about invite creator
Jonas Schäfer <jonas@wielicki.name>
parents:
4393
diff
changeset
|
145 |
4374
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
146 local invite; |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
147 if invite_type == "reset" then |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
148 if not options.username then |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
149 return 400; |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
150 end |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
151 invite = invites.create_account_reset(options.username, options.ttl); |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
152 elseif invite_type == "group" then |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
153 if not options.groups then |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
154 return 400; |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
155 end |
4396
de55e1475808
mod_http_admin_api: re-add information about invite creator
Jonas Schäfer <jonas@wielicki.name>
parents:
4393
diff
changeset
|
156 invite = invites.create_group(options.groups, { |
de55e1475808
mod_http_admin_api: re-add information about invite creator
Jonas Schäfer <jonas@wielicki.name>
parents:
4393
diff
changeset
|
157 source = source; |
5911
2aa66e928aa0
mod_http_admin_api: Allow specifying roles for invitations
Matthew Wild <mwild1@gmail.com>
parents:
5907
diff
changeset
|
158 roles = options.roles; |
4396
de55e1475808
mod_http_admin_api: re-add information about invite creator
Jonas Schäfer <jonas@wielicki.name>
parents:
4393
diff
changeset
|
159 }, options.ttl); |
4374
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
160 elseif invite_type == "account" then |
4375
03cf0d41b272
mod_http_admin_api: Allow specifying groups in account invite
Matthew Wild <mwild1@gmail.com>
parents:
4374
diff
changeset
|
161 invite = invites.create_account(options.username, { |
4396
de55e1475808
mod_http_admin_api: re-add information about invite creator
Jonas Schäfer <jonas@wielicki.name>
parents:
4393
diff
changeset
|
162 source = source; |
4375
03cf0d41b272
mod_http_admin_api: Allow specifying groups in account invite
Matthew Wild <mwild1@gmail.com>
parents:
4374
diff
changeset
|
163 groups = options.groups; |
5911
2aa66e928aa0
mod_http_admin_api: Allow specifying roles for invitations
Matthew Wild <mwild1@gmail.com>
parents:
5907
diff
changeset
|
164 roles = options.roles; |
4375
03cf0d41b272
mod_http_admin_api: Allow specifying groups in account invite
Matthew Wild <mwild1@gmail.com>
parents:
4374
diff
changeset
|
165 }, options.ttl); |
4374
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
166 else |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
167 return 400; |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
168 end |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
169 if not invite then |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
170 return 500; |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
171 end |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
172 event.response.headers["Content-Type"] = json_content_type; |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
173 return json.encode(token_info_to_invite_info(invite)); |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
174 end |
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
175 |
4343
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
176 function delete_invite(event, invite_id) --luacheck: ignore 212/event |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
177 if not invites.delete_account_invite(invite_id) then |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
178 return 404; |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
179 end |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 return 200; |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
181 end |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 |
5804
cb25d341745f
mod_http_admin_api: Return avatar metadata from get_user_info()
Matthew Wild <mwild1@gmail.com>
parents:
5802
diff
changeset
|
183 local function get_user_avatar_info(username) |
cb25d341745f
mod_http_admin_api: Return avatar metadata from get_user_info()
Matthew Wild <mwild1@gmail.com>
parents:
5802
diff
changeset
|
184 local pep_service = mod_pep.get_pep_service(username); |
cb25d341745f
mod_http_admin_api: Return avatar metadata from get_user_info()
Matthew Wild <mwild1@gmail.com>
parents:
5802
diff
changeset
|
185 local ok, _, avatar_item = pep_service:get_last_item("urn:xmpp:avatar:metadata", true); |
cb25d341745f
mod_http_admin_api: Return avatar metadata from get_user_info()
Matthew Wild <mwild1@gmail.com>
parents:
5802
diff
changeset
|
186 avatar_item = avatar_item and avatar_item:get_child("metadata", "urn:xmpp:avatar:metadata"); |
cb25d341745f
mod_http_admin_api: Return avatar metadata from get_user_info()
Matthew Wild <mwild1@gmail.com>
parents:
5802
diff
changeset
|
187 if not ok or not avatar_item then return; end |
cb25d341745f
mod_http_admin_api: Return avatar metadata from get_user_info()
Matthew Wild <mwild1@gmail.com>
parents:
5802
diff
changeset
|
188 |
cb25d341745f
mod_http_admin_api: Return avatar metadata from get_user_info()
Matthew Wild <mwild1@gmail.com>
parents:
5802
diff
changeset
|
189 local avatar_info = {}; |
cb25d341745f
mod_http_admin_api: Return avatar metadata from get_user_info()
Matthew Wild <mwild1@gmail.com>
parents:
5802
diff
changeset
|
190 |
cb25d341745f
mod_http_admin_api: Return avatar metadata from get_user_info()
Matthew Wild <mwild1@gmail.com>
parents:
5802
diff
changeset
|
191 for avatar in avatar_item:childtags("info") do |
cb25d341745f
mod_http_admin_api: Return avatar metadata from get_user_info()
Matthew Wild <mwild1@gmail.com>
parents:
5802
diff
changeset
|
192 table.insert(avatar_info, { |
cb25d341745f
mod_http_admin_api: Return avatar metadata from get_user_info()
Matthew Wild <mwild1@gmail.com>
parents:
5802
diff
changeset
|
193 bytes = tonumber(avatar.attr.bytes); |
cb25d341745f
mod_http_admin_api: Return avatar metadata from get_user_info()
Matthew Wild <mwild1@gmail.com>
parents:
5802
diff
changeset
|
194 hash = avatar.attr.id; |
cb25d341745f
mod_http_admin_api: Return avatar metadata from get_user_info()
Matthew Wild <mwild1@gmail.com>
parents:
5802
diff
changeset
|
195 type = avatar.attr.type; |
cb25d341745f
mod_http_admin_api: Return avatar metadata from get_user_info()
Matthew Wild <mwild1@gmail.com>
parents:
5802
diff
changeset
|
196 width = tonumber(avatar.attr.width); |
cb25d341745f
mod_http_admin_api: Return avatar metadata from get_user_info()
Matthew Wild <mwild1@gmail.com>
parents:
5802
diff
changeset
|
197 height = tonumber(avatar.attr.height); |
cb25d341745f
mod_http_admin_api: Return avatar metadata from get_user_info()
Matthew Wild <mwild1@gmail.com>
parents:
5802
diff
changeset
|
198 }); |
cb25d341745f
mod_http_admin_api: Return avatar metadata from get_user_info()
Matthew Wild <mwild1@gmail.com>
parents:
5802
diff
changeset
|
199 end |
cb25d341745f
mod_http_admin_api: Return avatar metadata from get_user_info()
Matthew Wild <mwild1@gmail.com>
parents:
5802
diff
changeset
|
200 |
cb25d341745f
mod_http_admin_api: Return avatar metadata from get_user_info()
Matthew Wild <mwild1@gmail.com>
parents:
5802
diff
changeset
|
201 return avatar_info; |
cb25d341745f
mod_http_admin_api: Return avatar metadata from get_user_info()
Matthew Wild <mwild1@gmail.com>
parents:
5802
diff
changeset
|
202 end |
cb25d341745f
mod_http_admin_api: Return avatar metadata from get_user_info()
Matthew Wild <mwild1@gmail.com>
parents:
5802
diff
changeset
|
203 |
4345
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
204 local function get_user_info(username) |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
205 if not usermanager.user_exists(username, module.host) then |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
206 return nil; |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
207 end |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
208 local display_name; |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
209 do |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
210 local pep_service = mod_pep.get_pep_service(username); |
4516
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
211 local ok, _, nick_item = pep_service:get_last_item(xmlns_nick, true); |
4345
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
212 if ok and nick_item then |
4516
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
213 display_name = nick_item:get_child_text("nick", xmlns_nick); |
4345
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
214 end |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
215 end |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
216 |
5005
d68348323406
mod_http_admin_api: Update for new new role API
Matthew Wild <mwild1@gmail.com>
parents:
4997
diff
changeset
|
217 local primary_role, secondary_roles, legacy_roles; |
d68348323406
mod_http_admin_api: Update for new new role API
Matthew Wild <mwild1@gmail.com>
parents:
4997
diff
changeset
|
218 if usermanager.get_user_role then |
d68348323406
mod_http_admin_api: Update for new new role API
Matthew Wild <mwild1@gmail.com>
parents:
4997
diff
changeset
|
219 primary_role = usermanager.get_user_role(username, module.host); |
d68348323406
mod_http_admin_api: Update for new new role API
Matthew Wild <mwild1@gmail.com>
parents:
4997
diff
changeset
|
220 secondary_roles = array.collect(it.keys(usermanager.get_user_secondary_roles(username, module.host))); |
d68348323406
mod_http_admin_api: Update for new new role API
Matthew Wild <mwild1@gmail.com>
parents:
4997
diff
changeset
|
221 elseif usermanager.get_user_roles then -- COMPAT w/0.12 |
d68348323406
mod_http_admin_api: Update for new new role API
Matthew Wild <mwild1@gmail.com>
parents:
4997
diff
changeset
|
222 legacy_roles = array(); |
d68348323406
mod_http_admin_api: Update for new new role API
Matthew Wild <mwild1@gmail.com>
parents:
4997
diff
changeset
|
223 local roles_map = usermanager.get_user_roles(username, module.host); |
d68348323406
mod_http_admin_api: Update for new new role API
Matthew Wild <mwild1@gmail.com>
parents:
4997
diff
changeset
|
224 for role_name in pairs(roles_map) do |
d68348323406
mod_http_admin_api: Update for new new role API
Matthew Wild <mwild1@gmail.com>
parents:
4997
diff
changeset
|
225 legacy_roles:push(role_name); |
d68348323406
mod_http_admin_api: Update for new new role API
Matthew Wild <mwild1@gmail.com>
parents:
4997
diff
changeset
|
226 end |
4517
d6a3201a65c0
mod_http_admin_api: support for updating user roles via the API
Jonas Schäfer <jonas@wielicki.name>
parents:
4516
diff
changeset
|
227 end |
d6a3201a65c0
mod_http_admin_api: support for updating user roles via the API
Jonas Schäfer <jonas@wielicki.name>
parents:
4516
diff
changeset
|
228 |
5798
4c3216d9b118
mod_http_admin_api: Include user account status and activity in get_user_info
Matthew Wild <mwild1@gmail.com>
parents:
5710
diff
changeset
|
229 local enabled = true; -- Assume all enabled if on a version without is_enabled |
4c3216d9b118
mod_http_admin_api: Include user account status and activity in get_user_info
Matthew Wild <mwild1@gmail.com>
parents:
5710
diff
changeset
|
230 if usermanager.user_is_enabled then |
4c3216d9b118
mod_http_admin_api: Include user account status and activity in get_user_info
Matthew Wild <mwild1@gmail.com>
parents:
5710
diff
changeset
|
231 enabled = usermanager.user_is_enabled(username, module.host); |
4c3216d9b118
mod_http_admin_api: Include user account status and activity in get_user_info
Matthew Wild <mwild1@gmail.com>
parents:
5710
diff
changeset
|
232 end |
4c3216d9b118
mod_http_admin_api: Include user account status and activity in get_user_info
Matthew Wild <mwild1@gmail.com>
parents:
5710
diff
changeset
|
233 |
4362
116c88c28532
mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents:
4361
diff
changeset
|
234 return { |
116c88c28532
mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents:
4361
diff
changeset
|
235 username = username; |
116c88c28532
mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents:
4361
diff
changeset
|
236 display_name = display_name; |
5005
d68348323406
mod_http_admin_api: Update for new new role API
Matthew Wild <mwild1@gmail.com>
parents:
4997
diff
changeset
|
237 role = primary_role and primary_role.name or nil; |
d68348323406
mod_http_admin_api: Update for new new role API
Matthew Wild <mwild1@gmail.com>
parents:
4997
diff
changeset
|
238 secondary_roles = secondary_roles; |
d68348323406
mod_http_admin_api: Update for new new role API
Matthew Wild <mwild1@gmail.com>
parents:
4997
diff
changeset
|
239 roles = legacy_roles; -- COMPAT w/0.12 |
5798
4c3216d9b118
mod_http_admin_api: Include user account status and activity in get_user_info
Matthew Wild <mwild1@gmail.com>
parents:
5710
diff
changeset
|
240 enabled = enabled; |
4c3216d9b118
mod_http_admin_api: Include user account status and activity in get_user_info
Matthew Wild <mwild1@gmail.com>
parents:
5710
diff
changeset
|
241 last_active = mod_lastlog2.get_last_active(username); |
5805
5621dece3ee4
mod_http_admin_api: Only include user deletion_request if account is disabled
Matthew Wild <mwild1@gmail.com>
parents:
5804
diff
changeset
|
242 deletion_request = not enabled and deleted_users:get(username) or nil; |
5804
cb25d341745f
mod_http_admin_api: Return avatar metadata from get_user_info()
Matthew Wild <mwild1@gmail.com>
parents:
5802
diff
changeset
|
243 avatar_info = get_user_avatar_info(username); |
4362
116c88c28532
mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents:
4361
diff
changeset
|
244 }; |
116c88c28532
mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents:
4361
diff
changeset
|
245 end |
116c88c28532
mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents:
4361
diff
changeset
|
246 |
4363
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
247 local function get_session_debug_info(session) |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
248 local info = { |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
249 full_jid = session.full_jid; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
250 ip = session.ip; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
251 since = math.floor(session.conntime); |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
252 status = { |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
253 connected = not not session.conn; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
254 hibernating = not not session.hibernating; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
255 }; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
256 features = { |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
257 carbons = not not session.want_carbons; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
258 encrypted = not not session.secure; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
259 acks = not not session.smacks; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
260 resumption = not not session.resumption_token; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
261 mobile_optimization = not not session.csi_counter; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
262 push_notifications = not not session.push_identifier; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
263 history = not not session.mam_requested; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
264 }; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
265 queues = {}; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
266 }; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
267 -- CSI |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
268 if session.state then |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
269 info.status.active = session.state == "active"; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
270 info.queues.held_stanzas = session.csi_counter or 0; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
271 end |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
272 -- Smacks queue |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
273 if session.last_requested_h and session.last_acknowledged_stanza then |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
274 info.queues.awaiting_acks = session.last_requested_h - session.last_acknowledged_stanza; |
4956
65870d42a7b1
mod_http_admin_api: Update for 0.12 changes to mod_smacks
Kim Alvefur <zash@zash.se>
parents:
4925
diff
changeset
|
275 elseif session.outgoing_stanza_queue then |
65870d42a7b1
mod_http_admin_api: Update for 0.12 changes to mod_smacks
Kim Alvefur <zash@zash.se>
parents:
4925
diff
changeset
|
276 -- New mod_smacks |
65870d42a7b1
mod_http_admin_api: Update for 0.12 changes to mod_smacks
Kim Alvefur <zash@zash.se>
parents:
4925
diff
changeset
|
277 info.queues.awaiting_acks = session.outgoing_stanza_queue:count_unacked(); |
4363
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
278 end |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
279 if session.push_identifier then |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
280 info.push_info = { |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
281 id = session.push_identifier; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
282 wakeup_push_sent = session.first_hibernated_push; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
283 }; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
284 end |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
285 return info; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
286 end |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
287 |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
288 local function get_user_omemo_info(username) |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
289 local everything_valid = true; |
4366
83370df0ce4a
mod_http_admin_api: do not report OMEMO as ok if no devices exist
Jonas Schäfer <jonas@wielicki.name>
parents:
4365
diff
changeset
|
290 local any_device = false; |
4363
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
291 local omemo_status = {}; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
292 local omemo_devices; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
293 local pep_service = mod_pep.get_pep_service(username); |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
294 if pep_service and pep_service.nodes then |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
295 local ok, _, device_list = pep_service:get_last_item("eu.siacs.conversations.axolotl.devicelist", true); |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
296 if ok and device_list then |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
297 device_list = device_list:get_child("list", "eu.siacs.conversations.axolotl"); |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
298 end |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
299 if device_list then |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
300 omemo_devices = {}; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
301 for device_entry in device_list:childtags("device") do |
4366
83370df0ce4a
mod_http_admin_api: do not report OMEMO as ok if no devices exist
Jonas Schäfer <jonas@wielicki.name>
parents:
4365
diff
changeset
|
302 any_device = true; |
4363
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
303 local device_info = {}; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
304 local device_id = tonumber(device_entry.attr.id or ""); |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
305 if device_id then |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
306 device_info.id = device_id; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
307 local bundle_id = ("eu.siacs.conversations.axolotl.bundles:%d"):format(device_id); |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
308 local have_bundle, _, bundle = pep_service:get_last_item(bundle_id, true); |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
309 if have_bundle and bundle and bundle:get_child("bundle", "eu.siacs.conversations.axolotl") then |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
310 device_info.have_bundle = true; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
311 local config_ok, bundle_config = pep_service:get_node_config(bundle_id, true); |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
312 if config_ok and bundle_config then |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
313 device_info.bundle_config = bundle_config; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
314 if bundle_config.max_items == 1 |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
315 and bundle_config.access_model == "open" |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
316 and bundle_config.persist_items == true |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
317 and bundle_config.publish_model == "publishers" then |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
318 device_info.valid = true; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
319 end |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
320 end |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
321 end |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
322 end |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
323 if device_info.valid == nil then |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
324 device_info.valid = false; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
325 everything_valid = false; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
326 end |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
327 table.insert(omemo_devices, device_info); |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
328 end |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
329 |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
330 local config_ok, list_config = pep_service:get_node_config("eu.siacs.conversations.axolotl.devicelist", true); |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
331 if config_ok and list_config then |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
332 omemo_status.config = list_config; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
333 if list_config.max_items == 1 |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
334 and list_config.access_model == "open" |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
335 and list_config.persist_items == true |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
336 and list_config.publish_model == "publishers" then |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
337 omemo_status.config_valid = true; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
338 end |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
339 end |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
340 if omemo_status.config_valid == nil then |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
341 omemo_status.config_valid = false; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
342 everything_valid = false; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
343 end |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
344 end |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
345 end |
4366
83370df0ce4a
mod_http_admin_api: do not report OMEMO as ok if no devices exist
Jonas Schäfer <jonas@wielicki.name>
parents:
4365
diff
changeset
|
346 omemo_status.valid = everything_valid and any_device; |
4363
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
347 return { |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
348 status = omemo_status; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
349 devices = omemo_devices; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
350 }; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
351 end |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
352 |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
353 local function get_user_debug_info(username) |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
354 local debug_info = { |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
355 time = os.time(); |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
356 }; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
357 -- Online sessions |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
358 do |
5005
d68348323406
mod_http_admin_api: Update for new new role API
Matthew Wild <mwild1@gmail.com>
parents:
4997
diff
changeset
|
359 local user_sessions = prosody.hosts[module.host].sessions[username]; |
4365
f975a4d31f35
mod_http_admin_api: make the api a bit less untested
Jonas Schäfer <jonas@wielicki.name>
parents:
4364
diff
changeset
|
360 if user_sessions then |
f975a4d31f35
mod_http_admin_api: make the api a bit less untested
Jonas Schäfer <jonas@wielicki.name>
parents:
4364
diff
changeset
|
361 user_sessions = user_sessions.sessions |
f975a4d31f35
mod_http_admin_api: make the api a bit less untested
Jonas Schäfer <jonas@wielicki.name>
parents:
4364
diff
changeset
|
362 end |
4363
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
363 local sessions = {}; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
364 if user_sessions then |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
365 for _, session in pairs(user_sessions) do |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
366 table.insert(sessions, get_session_debug_info(session)); |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
367 end |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
368 end |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
369 debug_info.sessions = sessions; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
370 end |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
371 -- Push registrations |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
372 do |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
373 local store = module:open_store("cloud_notify"); |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
374 local services = store:get(username); |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
375 local push_registrations = {}; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
376 if services then |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
377 for identifier, push_info in pairs(services) do |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
378 push_registrations[identifier] = { |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
379 since = push_info.timestamp; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
380 service = push_info.jid; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
381 node = push_info.node; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
382 error_count = push_errors[identifier] or 0; |
4924
55e3ca6bdfd1
mod_http_admin_api: Fix client -> client_id (the actual field name)
Matthew Wild <mwild1@gmail.com>
parents:
4777
diff
changeset
|
383 client_id = push_info.client_id; |
4925
404a22d52376
mod_http_admin_api: Indicate whether a given push registration uses encryption
Matthew Wild <mwild1@gmail.com>
parents:
4924
diff
changeset
|
384 encryption = not not push_info.encryption; |
4363
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
385 }; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
386 end |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
387 end |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
388 debug_info.push_registrations = push_registrations; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
389 end |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
390 -- OMEMO |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
391 debug_info.omemo = get_user_omemo_info(username); |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
392 |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
393 return debug_info; |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
394 end |
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
395 |
4345
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
396 function list_users(event) |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
397 local user_list = {}; |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
398 for username in usermanager.users(module.host) do |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
399 table.insert(user_list, get_user_info(username)); |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
400 end |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
401 |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
402 event.response.headers["Content-Type"] = json_content_type; |
4350
270025e76bf8
mod_http_admin_api: Use json.encode_array() when returning an array
Matthew Wild <mwild1@gmail.com>
parents:
4349
diff
changeset
|
403 return json.encode_array(user_list); |
4345
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
404 end |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
405 |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
406 function get_user_by_name(event, username) |
4362
116c88c28532
mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents:
4361
diff
changeset
|
407 local property |
116c88c28532
mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents:
4361
diff
changeset
|
408 do |
116c88c28532
mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents:
4361
diff
changeset
|
409 local name, sub_path = username:match("^([^/]+)/(%w+)$"); |
116c88c28532
mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents:
4361
diff
changeset
|
410 if name then |
116c88c28532
mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents:
4361
diff
changeset
|
411 username = name; |
116c88c28532
mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents:
4361
diff
changeset
|
412 property = sub_path; |
116c88c28532
mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents:
4361
diff
changeset
|
413 end |
116c88c28532
mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents:
4361
diff
changeset
|
414 end |
116c88c28532
mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents:
4361
diff
changeset
|
415 |
116c88c28532
mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents:
4361
diff
changeset
|
416 if property == "groups" then |
4364
49cf9d188b26
mod_http_admin_api: set content-type for debug API
Jonas Schäfer <jonas@wielicki.name>
parents:
4363
diff
changeset
|
417 event.response.headers["Content-Type"] = json_content_type; |
4382
0d6b69777bc1
mod_http_admin_api: port to use new mod_groups_internal module
Jonas Schäfer <jonas@wielicki.name>
parents:
4378
diff
changeset
|
418 return json.encode(mod_groups.get_user_groups(username)); |
4363
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
419 elseif property == "debug" then |
4364
49cf9d188b26
mod_http_admin_api: set content-type for debug API
Jonas Schäfer <jonas@wielicki.name>
parents:
4363
diff
changeset
|
420 event.response.headers["Content-Type"] = json_content_type; |
4363
636d56bbad97
mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents:
4362
diff
changeset
|
421 return json.encode(get_user_debug_info(username)); |
4362
116c88c28532
mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents:
4361
diff
changeset
|
422 end |
116c88c28532
mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents:
4361
diff
changeset
|
423 |
4345
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
424 local user_info = get_user_info(username); |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
425 if not user_info then |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
426 return 404; |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
427 end |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
428 |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
429 event.response.headers["Content-Type"] = json_content_type; |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
430 return json.encode(user_info); |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
431 end |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
432 |
5806
6c8cf3ecb885
mod_http_admin_api: Support for setting user account enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5805
diff
changeset
|
433 local user_attribute_writers = { |
6c8cf3ecb885
mod_http_admin_api: Support for setting user account enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5805
diff
changeset
|
434 enabled = function (username, enabled) |
6c8cf3ecb885
mod_http_admin_api: Support for setting user account enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5805
diff
changeset
|
435 local ok, err; |
6c8cf3ecb885
mod_http_admin_api: Support for setting user account enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5805
diff
changeset
|
436 if enabled == true then |
6c8cf3ecb885
mod_http_admin_api: Support for setting user account enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5805
diff
changeset
|
437 ok, err = usermanager.enable_user(username, module.host); |
6c8cf3ecb885
mod_http_admin_api: Support for setting user account enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5805
diff
changeset
|
438 elseif enabled == false then |
6c8cf3ecb885
mod_http_admin_api: Support for setting user account enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5805
diff
changeset
|
439 ok, err = usermanager.disable_user(username, module.host); |
6c8cf3ecb885
mod_http_admin_api: Support for setting user account enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5805
diff
changeset
|
440 else |
6c8cf3ecb885
mod_http_admin_api: Support for setting user account enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5805
diff
changeset
|
441 ok, err = nil, "Invalid value provided for 'enabled'"; |
6c8cf3ecb885
mod_http_admin_api: Support for setting user account enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5805
diff
changeset
|
442 end |
6c8cf3ecb885
mod_http_admin_api: Support for setting user account enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5805
diff
changeset
|
443 if not ok then |
6c8cf3ecb885
mod_http_admin_api: Support for setting user account enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5805
diff
changeset
|
444 module:log("error", "Unable to %s user '%s': %s", enabled and "enable" or "disable", username, err); |
6c8cf3ecb885
mod_http_admin_api: Support for setting user account enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5805
diff
changeset
|
445 return nil, err; |
6c8cf3ecb885
mod_http_admin_api: Support for setting user account enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5805
diff
changeset
|
446 end |
6c8cf3ecb885
mod_http_admin_api: Support for setting user account enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5805
diff
changeset
|
447 return true; |
6c8cf3ecb885
mod_http_admin_api: Support for setting user account enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5805
diff
changeset
|
448 end; |
6c8cf3ecb885
mod_http_admin_api: Support for setting user account enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5805
diff
changeset
|
449 }; |
6c8cf3ecb885
mod_http_admin_api: Support for setting user account enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5805
diff
changeset
|
450 local writable_user_attributes = set.new(array.collect(it.keys(user_attribute_writers))); |
6c8cf3ecb885
mod_http_admin_api: Support for setting user account enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5805
diff
changeset
|
451 |
5807
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
452 function patch_user(event, username) |
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
453 if not username then return; end |
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
454 |
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
455 local current_user = get_user_info(username); |
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
456 if not current_user then return 404; end |
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
457 |
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
458 local request = event.request; |
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
459 if request.headers.content_type ~= json_content_type |
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
460 or (not request.body or #request.body == 0) then |
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
461 return 400; |
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
462 end |
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
463 local new_user = json.decode(event.request.body); |
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
464 if not new_user then |
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
465 return 400; |
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
466 end |
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
467 |
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
468 local updated_attributes = set.new(array.collect(it.keys(new_user))); |
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
469 if not (updated_attributes - writable_user_attributes):empty() then |
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
470 module:log("warn", "Unable to service PATCH user request, unsupported attributes: %s", (updated_attributes - writable_user_attributes)); |
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
471 return 400; |
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
472 end |
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
473 |
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
474 if new_user.enabled ~= nil and new_user.enabled ~= current_user.enabled then |
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
475 if not user_attribute_writers.enabled(username, new_user.enabled) then |
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
476 return 500; |
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
477 end |
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
478 end |
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
479 |
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
480 return 200; |
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
481 end |
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
482 |
4516
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
483 function update_user(event, username) |
5809
076b79eb747f
mod_http_admin_api: Abort request if no valid username
Matthew Wild <mwild1@gmail.com>
parents:
5808
diff
changeset
|
484 if not username then |
076b79eb747f
mod_http_admin_api: Abort request if no valid username
Matthew Wild <mwild1@gmail.com>
parents:
5808
diff
changeset
|
485 return 400; |
076b79eb747f
mod_http_admin_api: Abort request if no valid username
Matthew Wild <mwild1@gmail.com>
parents:
5808
diff
changeset
|
486 end |
4516
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
487 |
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
488 local request = event.request; |
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
489 if request.headers.content_type ~= json_content_type |
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
490 or (not request.body or #request.body == 0) then |
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
491 return 400; |
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
492 end |
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
493 local new_user = json.decode(event.request.body); |
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
494 if not new_user then |
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
495 return 400; |
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
496 end |
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
497 |
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
498 if new_user.username and new_user.username ~= username then |
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
499 return 400; |
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
500 end |
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
501 |
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
502 if new_user.display_name then |
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
503 local pep_service = mod_pep.get_pep_service(username); |
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
504 -- TODO: publish |
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
505 local nick_item = st.stanza("item", { xmlns = xmlns_pubsub, id = "current" }) |
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
506 :text_tag("nick", new_user.display_name, { xmlns = xmlns_nick }); |
5808
671a6ad1f026
mod_http_admin_api: Fix some luacheck warnings and code style issues
Matthew Wild <mwild1@gmail.com>
parents:
5807
diff
changeset
|
507 pep_service:publish(xmlns_nick, true, "current", nick_item, { |
4516
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
508 access_model = "open"; |
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
509 _defaults_only = true; |
5808
671a6ad1f026
mod_http_admin_api: Fix some luacheck warnings and code style issues
Matthew Wild <mwild1@gmail.com>
parents:
5807
diff
changeset
|
510 }); |
4516
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
511 end |
4517
d6a3201a65c0
mod_http_admin_api: support for updating user roles via the API
Jonas Schäfer <jonas@wielicki.name>
parents:
4516
diff
changeset
|
512 |
5005
d68348323406
mod_http_admin_api: Update for new new role API
Matthew Wild <mwild1@gmail.com>
parents:
4997
diff
changeset
|
513 if new_user.role then |
d68348323406
mod_http_admin_api: Update for new new role API
Matthew Wild <mwild1@gmail.com>
parents:
4997
diff
changeset
|
514 if not usermanager.set_user_role then |
d68348323406
mod_http_admin_api: Update for new new role API
Matthew Wild <mwild1@gmail.com>
parents:
4997
diff
changeset
|
515 return 500, "feature-not-implemented"; |
d68348323406
mod_http_admin_api: Update for new new role API
Matthew Wild <mwild1@gmail.com>
parents:
4997
diff
changeset
|
516 end |
d68348323406
mod_http_admin_api: Update for new new role API
Matthew Wild <mwild1@gmail.com>
parents:
4997
diff
changeset
|
517 if not usermanager.set_user_role(username, module.host, new_user.role) then |
d68348323406
mod_http_admin_api: Update for new new role API
Matthew Wild <mwild1@gmail.com>
parents:
4997
diff
changeset
|
518 module:log("error", "failed to set role %s for %s", new_user.role, username); |
d68348323406
mod_http_admin_api: Update for new new role API
Matthew Wild <mwild1@gmail.com>
parents:
4997
diff
changeset
|
519 return 500; |
d68348323406
mod_http_admin_api: Update for new new role API
Matthew Wild <mwild1@gmail.com>
parents:
4997
diff
changeset
|
520 end |
d68348323406
mod_http_admin_api: Update for new new role API
Matthew Wild <mwild1@gmail.com>
parents:
4997
diff
changeset
|
521 end |
d68348323406
mod_http_admin_api: Update for new new role API
Matthew Wild <mwild1@gmail.com>
parents:
4997
diff
changeset
|
522 |
d68348323406
mod_http_admin_api: Update for new new role API
Matthew Wild <mwild1@gmail.com>
parents:
4997
diff
changeset
|
523 if new_user.roles then -- COMPAT w/0.12 |
4997
1b5869c34026
mod_http_admin_api: Updates for new role auth API in Prosody (trunk/0.13 only)
Matthew Wild <mwild1@gmail.com>
parents:
4956
diff
changeset
|
524 if not usermanager.set_user_roles then |
4517
d6a3201a65c0
mod_http_admin_api: support for updating user roles via the API
Jonas Schäfer <jonas@wielicki.name>
parents:
4516
diff
changeset
|
525 return 500, "feature-not-implemented" |
d6a3201a65c0
mod_http_admin_api: support for updating user roles via the API
Jonas Schäfer <jonas@wielicki.name>
parents:
4516
diff
changeset
|
526 end |
d6a3201a65c0
mod_http_admin_api: support for updating user roles via the API
Jonas Schäfer <jonas@wielicki.name>
parents:
4516
diff
changeset
|
527 |
d6a3201a65c0
mod_http_admin_api: support for updating user roles via the API
Jonas Schäfer <jonas@wielicki.name>
parents:
4516
diff
changeset
|
528 local backend_roles = {}; |
d6a3201a65c0
mod_http_admin_api: support for updating user roles via the API
Jonas Schäfer <jonas@wielicki.name>
parents:
4516
diff
changeset
|
529 for _, role in ipairs(new_user.roles) do |
d6a3201a65c0
mod_http_admin_api: support for updating user roles via the API
Jonas Schäfer <jonas@wielicki.name>
parents:
4516
diff
changeset
|
530 backend_roles[role] = true; |
d6a3201a65c0
mod_http_admin_api: support for updating user roles via the API
Jonas Schäfer <jonas@wielicki.name>
parents:
4516
diff
changeset
|
531 end |
5808
671a6ad1f026
mod_http_admin_api: Fix some luacheck warnings and code style issues
Matthew Wild <mwild1@gmail.com>
parents:
5807
diff
changeset
|
532 local user_jid = username.."@"..module.host; |
4997
1b5869c34026
mod_http_admin_api: Updates for new role auth API in Prosody (trunk/0.13 only)
Matthew Wild <mwild1@gmail.com>
parents:
4956
diff
changeset
|
533 if not usermanager.set_user_roles(username, module.host, backend_roles) then |
5808
671a6ad1f026
mod_http_admin_api: Fix some luacheck warnings and code style issues
Matthew Wild <mwild1@gmail.com>
parents:
5807
diff
changeset
|
534 module:log("error", "failed to set roles %q for %s", backend_roles, user_jid) |
4517
d6a3201a65c0
mod_http_admin_api: support for updating user roles via the API
Jonas Schäfer <jonas@wielicki.name>
parents:
4516
diff
changeset
|
535 return 500 |
d6a3201a65c0
mod_http_admin_api: support for updating user roles via the API
Jonas Schäfer <jonas@wielicki.name>
parents:
4516
diff
changeset
|
536 end |
d6a3201a65c0
mod_http_admin_api: support for updating user roles via the API
Jonas Schäfer <jonas@wielicki.name>
parents:
4516
diff
changeset
|
537 end |
d6a3201a65c0
mod_http_admin_api: support for updating user roles via the API
Jonas Schäfer <jonas@wielicki.name>
parents:
4516
diff
changeset
|
538 |
5806
6c8cf3ecb885
mod_http_admin_api: Support for setting user account enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5805
diff
changeset
|
539 if new_user.enabled ~= nil then |
6c8cf3ecb885
mod_http_admin_api: Support for setting user account enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5805
diff
changeset
|
540 if not user_attribute_writers.enabled(username, new_user.enabled) then |
6c8cf3ecb885
mod_http_admin_api: Support for setting user account enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5805
diff
changeset
|
541 return 500; |
6c8cf3ecb885
mod_http_admin_api: Support for setting user account enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5805
diff
changeset
|
542 end |
6c8cf3ecb885
mod_http_admin_api: Support for setting user account enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5805
diff
changeset
|
543 end |
6c8cf3ecb885
mod_http_admin_api: Support for setting user account enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5805
diff
changeset
|
544 |
4516
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
545 return 200; |
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
546 end |
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
547 |
4345
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
548 function delete_user(event, username) --luacheck: ignore 212/event |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
549 if not usermanager.delete_user(username, module.host) then |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
550 return 404; |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
551 end |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
552 return 200; |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
553 end |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
554 |
4353
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
555 function list_groups(event) |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
556 local group_list = {}; |
4382
0d6b69777bc1
mod_http_admin_api: port to use new mod_groups_internal module
Jonas Schäfer <jonas@wielicki.name>
parents:
4378
diff
changeset
|
557 for group_id in mod_groups.groups() do |
0d6b69777bc1
mod_http_admin_api: port to use new mod_groups_internal module
Jonas Schäfer <jonas@wielicki.name>
parents:
4378
diff
changeset
|
558 local group_info = mod_groups.get_info(group_id); |
4353
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
559 table.insert(group_list, { |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
560 id = group_id; |
4368
e0c8d866d58c
mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents:
4366
diff
changeset
|
561 name = group_info.name; |
4382
0d6b69777bc1
mod_http_admin_api: port to use new mod_groups_internal module
Jonas Schäfer <jonas@wielicki.name>
parents:
4378
diff
changeset
|
562 members = mod_groups.get_members(group_id); |
5710
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
563 chats = mod_groups.get_group_chats(group_id); |
4353
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
564 }); |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
565 end |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
566 |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
567 event.response.headers["Content-Type"] = json_content_type; |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
568 return json.encode_array(group_list); |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
569 end |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
570 |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
571 function get_group_by_id(event, group_id) |
4382
0d6b69777bc1
mod_http_admin_api: port to use new mod_groups_internal module
Jonas Schäfer <jonas@wielicki.name>
parents:
4378
diff
changeset
|
572 local group = mod_groups.get_info(group_id); |
4353
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
573 if not group then |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
574 return 404; |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
575 end |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
576 |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
577 event.response.headers["Content-Type"] = json_content_type; |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
578 |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
579 return json.encode({ |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
580 id = group_id; |
4368
e0c8d866d58c
mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents:
4366
diff
changeset
|
581 name = group.name; |
4382
0d6b69777bc1
mod_http_admin_api: port to use new mod_groups_internal module
Jonas Schäfer <jonas@wielicki.name>
parents:
4378
diff
changeset
|
582 members = mod_groups.get_members(group_id); |
5710
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
583 chats = mod_groups.get_group_chats(group_id); |
4353
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
584 }); |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
585 end |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
586 |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
587 function create_group(event) |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
588 local request = event.request; |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
589 if request.headers.content_type ~= json_content_type |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
590 or (not request.body or #request.body == 0) then |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
591 return 400; |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
592 end |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
593 local group = json.decode(event.request.body); |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
594 if not group then |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
595 return 400; |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
596 end |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
597 |
4368
e0c8d866d58c
mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents:
4366
diff
changeset
|
598 if not group.name then |
e0c8d866d58c
mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents:
4366
diff
changeset
|
599 module:log("warn", "Group missing name property"); |
e0c8d866d58c
mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents:
4366
diff
changeset
|
600 return 400; |
e0c8d866d58c
mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents:
4366
diff
changeset
|
601 end |
e0c8d866d58c
mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents:
4366
diff
changeset
|
602 |
4390
17d44ba8fde2
mod_http_admin_api: support for creating groups with MUCs
Jonas Schäfer <jonas@wielicki.name>
parents:
4382
diff
changeset
|
603 local create_muc = group.create_muc and true or false; |
17d44ba8fde2
mod_http_admin_api: support for creating groups with MUCs
Jonas Schäfer <jonas@wielicki.name>
parents:
4382
diff
changeset
|
604 |
17d44ba8fde2
mod_http_admin_api: support for creating groups with MUCs
Jonas Schäfer <jonas@wielicki.name>
parents:
4382
diff
changeset
|
605 local group_id = mod_groups.create( |
17d44ba8fde2
mod_http_admin_api: support for creating groups with MUCs
Jonas Schäfer <jonas@wielicki.name>
parents:
4382
diff
changeset
|
606 { |
17d44ba8fde2
mod_http_admin_api: support for creating groups with MUCs
Jonas Schäfer <jonas@wielicki.name>
parents:
4382
diff
changeset
|
607 name = group.name; |
17d44ba8fde2
mod_http_admin_api: support for creating groups with MUCs
Jonas Schäfer <jonas@wielicki.name>
parents:
4382
diff
changeset
|
608 }, |
17d44ba8fde2
mod_http_admin_api: support for creating groups with MUCs
Jonas Schäfer <jonas@wielicki.name>
parents:
4382
diff
changeset
|
609 create_muc |
17d44ba8fde2
mod_http_admin_api: support for creating groups with MUCs
Jonas Schäfer <jonas@wielicki.name>
parents:
4382
diff
changeset
|
610 ); |
4382
0d6b69777bc1
mod_http_admin_api: port to use new mod_groups_internal module
Jonas Schäfer <jonas@wielicki.name>
parents:
4378
diff
changeset
|
611 if not group_id then |
4353
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
612 return 500; |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
613 end |
4361
7f1f3b79d991
mod_http_admin_api: Return new group object on successful creation
Matthew Wild <mwild1@gmail.com>
parents:
4360
diff
changeset
|
614 |
7f1f3b79d991
mod_http_admin_api: Return new group object on successful creation
Matthew Wild <mwild1@gmail.com>
parents:
4360
diff
changeset
|
615 event.response.headers["Content-Type"] = json_content_type; |
4390
17d44ba8fde2
mod_http_admin_api: support for creating groups with MUCs
Jonas Schäfer <jonas@wielicki.name>
parents:
4382
diff
changeset
|
616 |
17d44ba8fde2
mod_http_admin_api: support for creating groups with MUCs
Jonas Schäfer <jonas@wielicki.name>
parents:
4382
diff
changeset
|
617 local info = mod_groups.get_info(group_id); |
4361
7f1f3b79d991
mod_http_admin_api: Return new group object on successful creation
Matthew Wild <mwild1@gmail.com>
parents:
4360
diff
changeset
|
618 return json.encode({ |
4368
e0c8d866d58c
mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents:
4366
diff
changeset
|
619 id = group_id; |
4390
17d44ba8fde2
mod_http_admin_api: support for creating groups with MUCs
Jonas Schäfer <jonas@wielicki.name>
parents:
4382
diff
changeset
|
620 name = info.name; |
4368
e0c8d866d58c
mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents:
4366
diff
changeset
|
621 members = {}; |
5710
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
622 chats = {}; |
4361
7f1f3b79d991
mod_http_admin_api: Return new group object on successful creation
Matthew Wild <mwild1@gmail.com>
parents:
4360
diff
changeset
|
623 }); |
4353
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
624 end |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
625 |
4368
e0c8d866d58c
mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents:
4366
diff
changeset
|
626 function update_group(event, group) --luacheck: ignore 212/event |
4382
0d6b69777bc1
mod_http_admin_api: port to use new mod_groups_internal module
Jonas Schäfer <jonas@wielicki.name>
parents:
4378
diff
changeset
|
627 -- Add member |
5808
671a6ad1f026
mod_http_admin_api: Fix some luacheck warnings and code style issues
Matthew Wild <mwild1@gmail.com>
parents:
5807
diff
changeset
|
628 do |
671a6ad1f026
mod_http_admin_api: Fix some luacheck warnings and code style issues
Matthew Wild <mwild1@gmail.com>
parents:
5807
diff
changeset
|
629 local group_id, member_name = group:match("^([^/]+)/members/([^/]+)$"); |
671a6ad1f026
mod_http_admin_api: Fix some luacheck warnings and code style issues
Matthew Wild <mwild1@gmail.com>
parents:
5807
diff
changeset
|
630 if group_id and member_name then |
671a6ad1f026
mod_http_admin_api: Fix some luacheck warnings and code style issues
Matthew Wild <mwild1@gmail.com>
parents:
5807
diff
changeset
|
631 if not mod_groups.add_member(group_id, member_name) then |
671a6ad1f026
mod_http_admin_api: Fix some luacheck warnings and code style issues
Matthew Wild <mwild1@gmail.com>
parents:
5807
diff
changeset
|
632 return 500; |
671a6ad1f026
mod_http_admin_api: Fix some luacheck warnings and code style issues
Matthew Wild <mwild1@gmail.com>
parents:
5807
diff
changeset
|
633 end |
671a6ad1f026
mod_http_admin_api: Fix some luacheck warnings and code style issues
Matthew Wild <mwild1@gmail.com>
parents:
5807
diff
changeset
|
634 return 204; |
4368
e0c8d866d58c
mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents:
4366
diff
changeset
|
635 end |
4369
29b7f445aec5
mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents:
4368
diff
changeset
|
636 end |
29b7f445aec5
mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents:
4368
diff
changeset
|
637 |
29b7f445aec5
mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents:
4368
diff
changeset
|
638 local group_id = group:match("^([^/]+)$") |
5808
671a6ad1f026
mod_http_admin_api: Fix some luacheck warnings and code style issues
Matthew Wild <mwild1@gmail.com>
parents:
5807
diff
changeset
|
639 if not group_id then return 404; end |
4369
29b7f445aec5
mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents:
4368
diff
changeset
|
640 |
5808
671a6ad1f026
mod_http_admin_api: Fix some luacheck warnings and code style issues
Matthew Wild <mwild1@gmail.com>
parents:
5807
diff
changeset
|
641 local request = event.request; |
671a6ad1f026
mod_http_admin_api: Fix some luacheck warnings and code style issues
Matthew Wild <mwild1@gmail.com>
parents:
5807
diff
changeset
|
642 if request.headers.content_type ~= json_content_type or (not request.body or #request.body == 0) then |
671a6ad1f026
mod_http_admin_api: Fix some luacheck warnings and code style issues
Matthew Wild <mwild1@gmail.com>
parents:
5807
diff
changeset
|
643 return 400; |
671a6ad1f026
mod_http_admin_api: Fix some luacheck warnings and code style issues
Matthew Wild <mwild1@gmail.com>
parents:
5807
diff
changeset
|
644 end |
671a6ad1f026
mod_http_admin_api: Fix some luacheck warnings and code style issues
Matthew Wild <mwild1@gmail.com>
parents:
5807
diff
changeset
|
645 |
671a6ad1f026
mod_http_admin_api: Fix some luacheck warnings and code style issues
Matthew Wild <mwild1@gmail.com>
parents:
5807
diff
changeset
|
646 local update = json.decode(event.request.body); |
671a6ad1f026
mod_http_admin_api: Fix some luacheck warnings and code style issues
Matthew Wild <mwild1@gmail.com>
parents:
5807
diff
changeset
|
647 if not update then |
671a6ad1f026
mod_http_admin_api: Fix some luacheck warnings and code style issues
Matthew Wild <mwild1@gmail.com>
parents:
5807
diff
changeset
|
648 return 400; |
671a6ad1f026
mod_http_admin_api: Fix some luacheck warnings and code style issues
Matthew Wild <mwild1@gmail.com>
parents:
5807
diff
changeset
|
649 end |
4369
29b7f445aec5
mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents:
4368
diff
changeset
|
650 |
5808
671a6ad1f026
mod_http_admin_api: Fix some luacheck warnings and code style issues
Matthew Wild <mwild1@gmail.com>
parents:
5807
diff
changeset
|
651 local group_info = mod_groups.get_info(group_id); |
671a6ad1f026
mod_http_admin_api: Fix some luacheck warnings and code style issues
Matthew Wild <mwild1@gmail.com>
parents:
5807
diff
changeset
|
652 if not group_info then |
671a6ad1f026
mod_http_admin_api: Fix some luacheck warnings and code style issues
Matthew Wild <mwild1@gmail.com>
parents:
5807
diff
changeset
|
653 return 404; |
671a6ad1f026
mod_http_admin_api: Fix some luacheck warnings and code style issues
Matthew Wild <mwild1@gmail.com>
parents:
5807
diff
changeset
|
654 end |
4369
29b7f445aec5
mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents:
4368
diff
changeset
|
655 |
5808
671a6ad1f026
mod_http_admin_api: Fix some luacheck warnings and code style issues
Matthew Wild <mwild1@gmail.com>
parents:
5807
diff
changeset
|
656 if update.name then |
671a6ad1f026
mod_http_admin_api: Fix some luacheck warnings and code style issues
Matthew Wild <mwild1@gmail.com>
parents:
5807
diff
changeset
|
657 group_info["name"] = update.name; |
4368
e0c8d866d58c
mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents:
4366
diff
changeset
|
658 end |
5808
671a6ad1f026
mod_http_admin_api: Fix some luacheck warnings and code style issues
Matthew Wild <mwild1@gmail.com>
parents:
5807
diff
changeset
|
659 if not mod_groups.set_info(group_id, group_info) then |
671a6ad1f026
mod_http_admin_api: Fix some luacheck warnings and code style issues
Matthew Wild <mwild1@gmail.com>
parents:
5807
diff
changeset
|
660 return 500; |
671a6ad1f026
mod_http_admin_api: Fix some luacheck warnings and code style issues
Matthew Wild <mwild1@gmail.com>
parents:
5807
diff
changeset
|
661 end |
671a6ad1f026
mod_http_admin_api: Fix some luacheck warnings and code style issues
Matthew Wild <mwild1@gmail.com>
parents:
5807
diff
changeset
|
662 return 204; |
4368
e0c8d866d58c
mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents:
4366
diff
changeset
|
663 end |
e0c8d866d58c
mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents:
4366
diff
changeset
|
664 |
5710
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
665 function extend_group(event, subpath) |
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
666 -- Add group chat |
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
667 local group_id = subpath:match("^([^/]+)/chats$"); |
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
668 if group_id then |
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
669 local muc_params = json.decode(event.request.body); |
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
670 if not muc_params then |
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
671 return 400; |
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
672 end |
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
673 local muc = mod_groups.add_group_chat(group_id, muc_params.name); |
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
674 if not muc then |
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
675 return 500; |
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
676 end |
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
677 return json.encode(muc); |
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
678 end |
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
679 |
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
680 return 404; |
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
681 end |
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
682 |
4368
e0c8d866d58c
mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents:
4366
diff
changeset
|
683 function delete_group(event, subpath) --luacheck: ignore 212/event |
e0c8d866d58c
mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents:
4366
diff
changeset
|
684 -- Check if this is a membership deletion and handle it |
5710
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
685 local group_id, sub_resource_type, sub_resource_id = subpath:match("^([^/]+)/([^/]+)/([^/]+)$"); |
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
686 if group_id then |
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
687 -- Operation is on a sub-resource |
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
688 if sub_resource_type == "members" then |
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
689 if mod_groups.remove_member(group_id, sub_resource_id) then |
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
690 return 204; |
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
691 else |
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
692 return 500; |
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
693 end |
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
694 elseif sub_resource_type == "chats" then |
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
695 if mod_groups.remove_group_chat(group_id, sub_resource_id) then |
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
696 return 204; |
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
697 else |
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
698 return 500; |
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
699 end |
4368
e0c8d866d58c
mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents:
4366
diff
changeset
|
700 else |
5710
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
701 return 404; |
4368
e0c8d866d58c
mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents:
4366
diff
changeset
|
702 end |
e0c8d866d58c
mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents:
4366
diff
changeset
|
703 else |
4382
0d6b69777bc1
mod_http_admin_api: port to use new mod_groups_internal module
Jonas Schäfer <jonas@wielicki.name>
parents:
4378
diff
changeset
|
704 -- Action refers to the group |
4368
e0c8d866d58c
mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents:
4366
diff
changeset
|
705 group_id = subpath; |
e0c8d866d58c
mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents:
4366
diff
changeset
|
706 end |
e0c8d866d58c
mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents:
4366
diff
changeset
|
707 |
4353
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
708 if not group_id then |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
709 return 400; |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
710 end |
4368
e0c8d866d58c
mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents:
4366
diff
changeset
|
711 |
4382
0d6b69777bc1
mod_http_admin_api: port to use new mod_groups_internal module
Jonas Schäfer <jonas@wielicki.name>
parents:
4378
diff
changeset
|
712 if not mod_groups.exists(group_id) then |
4368
e0c8d866d58c
mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents:
4366
diff
changeset
|
713 return 404; |
e0c8d866d58c
mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents:
4366
diff
changeset
|
714 end |
e0c8d866d58c
mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents:
4366
diff
changeset
|
715 |
4382
0d6b69777bc1
mod_http_admin_api: port to use new mod_groups_internal module
Jonas Schäfer <jonas@wielicki.name>
parents:
4378
diff
changeset
|
716 if not mod_groups.delete(group_id) then |
4353
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
717 return 500; |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
718 end |
4382
0d6b69777bc1
mod_http_admin_api: port to use new mod_groups_internal module
Jonas Schäfer <jonas@wielicki.name>
parents:
4378
diff
changeset
|
719 return 204; |
4353
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
720 end |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
721 |
4371
3d01bc4547b2
mod_http_admin_api: Add /server/info endpoint for site_name and version
Matthew Wild <mwild1@gmail.com>
parents:
4369
diff
changeset
|
722 local function get_server_info(event) |
3d01bc4547b2
mod_http_admin_api: Add /server/info endpoint for site_name and version
Matthew Wild <mwild1@gmail.com>
parents:
4369
diff
changeset
|
723 event.response.headers["Content-Type"] = json_content_type; |
3d01bc4547b2
mod_http_admin_api: Add /server/info endpoint for site_name and version
Matthew Wild <mwild1@gmail.com>
parents:
4369
diff
changeset
|
724 return json.encode({ |
3d01bc4547b2
mod_http_admin_api: Add /server/info endpoint for site_name and version
Matthew Wild <mwild1@gmail.com>
parents:
4369
diff
changeset
|
725 site_name = site_name; |
3d01bc4547b2
mod_http_admin_api: Add /server/info endpoint for site_name and version
Matthew Wild <mwild1@gmail.com>
parents:
4369
diff
changeset
|
726 version = prosody.version; |
3d01bc4547b2
mod_http_admin_api: Add /server/info endpoint for site_name and version
Matthew Wild <mwild1@gmail.com>
parents:
4369
diff
changeset
|
727 }); |
3d01bc4547b2
mod_http_admin_api: Add /server/info endpoint for site_name and version
Matthew Wild <mwild1@gmail.com>
parents:
4369
diff
changeset
|
728 end |
3d01bc4547b2
mod_http_admin_api: Add /server/info endpoint for site_name and version
Matthew Wild <mwild1@gmail.com>
parents:
4369
diff
changeset
|
729 |
4576
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
730 local function maybe_export_plain_gauge(mf) |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
731 if mf == nil then |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
732 return nil |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
733 end |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
734 return mf.data.value |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
735 end |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
736 |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
737 local function maybe_export_plain_counter(mf) |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
738 if mf == nil then |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
739 return nil |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
740 end |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
741 return { |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
742 since = mf.data._created, |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
743 value = mf.data.value, |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
744 } |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
745 end |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
746 |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
747 local function maybe_export_summed_gauge(mf) |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
748 if mf == nil then |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
749 return nil |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
750 end |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
751 local sum = 0; |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
752 for _, metric in mf:iter_metrics() do |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
753 sum = sum + metric.value; |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
754 end |
5850
f76909ec1300
mod_http_admin_api: metrics: Filter out a value that is commonly nan at startup
Matthew Wild <mwild1@gmail.com>
parents:
5809
diff
changeset
|
755 return (sum == sum) and sum or nil; -- Filter out nan |
4576
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
756 end |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
757 |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
758 local function get_server_metrics(event) |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
759 event.response.headers["Content-Type"] = json_content_type; |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
760 local result = {}; |
4776
13e913471b75
mod_http_admin_api: Ensure freshness of metrics when in manual mode
Kim Alvefur <zash@zash.se>
parents:
4714
diff
changeset
|
761 if manual_stats_collection then |
13e913471b75
mod_http_admin_api: Ensure freshness of metrics when in manual mode
Kim Alvefur <zash@zash.se>
parents:
4714
diff
changeset
|
762 statsmanager.collect(); |
13e913471b75
mod_http_admin_api: Ensure freshness of metrics when in manual mode
Kim Alvefur <zash@zash.se>
parents:
4714
diff
changeset
|
763 end |
4576
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
764 local families = statsmanager.get_metric_registry():get_metric_families(); |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
765 result.memory = maybe_export_plain_gauge(families.process_resident_memory_bytes); |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
766 result.cpu = maybe_export_plain_counter(families.process_cpu_seconds); |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
767 result.c2s = maybe_export_summed_gauge(families["prosody_mod_c2s/connections"]) |
4777
883ad8b0a7c0
mod_http_admin_api: Export total storage use via mod_http_file_share
Kim Alvefur <zash@zash.se>
parents:
4776
diff
changeset
|
768 result.uploads = maybe_export_summed_gauge(families["prosody_mod_http_file_share/total_storage_bytes"]); |
5886
9f2816287556
mod_http_admin_api: Include active user counts in metrics response
Matthew Wild <mwild1@gmail.com>
parents:
5850
diff
changeset
|
769 result.users = { |
5907
6c11e1b4066d
mod_http_admin_api: User activity gauges need to be summed (labels by host)
Matthew Wild <mwild1@gmail.com>
parents:
5886
diff
changeset
|
770 active_1d = maybe_export_summed_gauge(families["prosody_mod_measure_active_users/active_users_1d"]); |
6c11e1b4066d
mod_http_admin_api: User activity gauges need to be summed (labels by host)
Matthew Wild <mwild1@gmail.com>
parents:
5886
diff
changeset
|
771 active_7d = maybe_export_summed_gauge(families["prosody_mod_measure_active_users/active_users_7d"]); |
6c11e1b4066d
mod_http_admin_api: User activity gauges need to be summed (labels by host)
Matthew Wild <mwild1@gmail.com>
parents:
5886
diff
changeset
|
772 active_30d = maybe_export_summed_gauge(families["prosody_mod_measure_active_users/active_users_30d"]); |
5886
9f2816287556
mod_http_admin_api: Include active user counts in metrics response
Matthew Wild <mwild1@gmail.com>
parents:
5850
diff
changeset
|
773 }; |
4576
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
774 return json.encode(result); |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
775 end |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
776 |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
777 local function post_server_announcement(event) |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
778 local request = event.request; |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
779 if request.headers.content_type ~= json_content_type |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
780 or (not request.body or #request.body == 0) then |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
781 return 400; |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
782 end |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
783 local body = json.decode(event.request.body); |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
784 if not body then |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
785 return 400; |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
786 end |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
787 |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
788 if type(body.recipients) ~= "table" and body.recipients ~= "online" and body.recipients ~= "all" then |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
789 return 400; |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
790 end |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
791 |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
792 if not body.body or #body.body == 0 then |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
793 return 400; |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
794 end |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
795 |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
796 local message = st.message():tag("body"):text(body.body):up(); |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
797 local host = module.host |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
798 message.attr.from = host |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
799 if body.recipients == "online" then |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
800 announce.send_to_online(message, host); |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
801 elseif body.recipients == "all" then |
5915
512f912fdfa5
mod_http_admin_api: Use new API in mod_announce to send announcements
Matthew Wild <mwild1@gmail.com>
parents:
5912
diff
changeset
|
802 if announce.send_to_all then |
512f912fdfa5
mod_http_admin_api: Use new API in mod_announce to send announcements
Matthew Wild <mwild1@gmail.com>
parents:
5912
diff
changeset
|
803 announce.send_to_all(message, host); |
512f912fdfa5
mod_http_admin_api: Use new API in mod_announce to send announcements
Matthew Wild <mwild1@gmail.com>
parents:
5912
diff
changeset
|
804 else -- COMPAT w/ 0.12 and trunk before e22609460975 |
512f912fdfa5
mod_http_admin_api: Use new API in mod_announce to send announcements
Matthew Wild <mwild1@gmail.com>
parents:
5912
diff
changeset
|
805 for username in usermanager.users(host) do |
512f912fdfa5
mod_http_admin_api: Use new API in mod_announce to send announcements
Matthew Wild <mwild1@gmail.com>
parents:
5912
diff
changeset
|
806 message.attr.to = username .. "@" .. host |
512f912fdfa5
mod_http_admin_api: Use new API in mod_announce to send announcements
Matthew Wild <mwild1@gmail.com>
parents:
5912
diff
changeset
|
807 module:send(st.clone(message)) |
512f912fdfa5
mod_http_admin_api: Use new API in mod_announce to send announcements
Matthew Wild <mwild1@gmail.com>
parents:
5912
diff
changeset
|
808 end |
4576
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
809 end |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
810 else |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
811 for _, addr in ipairs(body.recipients) do |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
812 message.attr.to = addr |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
813 module:send(message) |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
814 end |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
815 end |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
816 |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
817 return 201; |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
818 end |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
819 |
4343
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
820 module:provides("http", { |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
821 route = check_auth { |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
822 ["GET /invites"] = list_invites; |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
823 ["GET /invites/*"] = get_invite_by_id; |
4374
e707810a943e
mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents:
4371
diff
changeset
|
824 ["POST /invites/*"] = create_invite_type; |
4343
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
825 ["DELETE /invites/*"] = delete_invite; |
4345
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
826 |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
827 ["GET /users"] = list_users; |
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
828 ["GET /users/*"] = get_user_by_name; |
4516
5bc706c2db8f
mod_http_admin_api: allow updating the user nickname via API
Jonas Schäfer <jonas@wielicki.name>
parents:
4396
diff
changeset
|
829 ["PUT /users/*"] = update_user; |
5807
5c589fab6f53
mod_http_admin_api: Support PATCH for user enabled status
Matthew Wild <mwild1@gmail.com>
parents:
5806
diff
changeset
|
830 ["PATCH /users/*"] = patch_user; |
4345
1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents:
4343
diff
changeset
|
831 ["DELETE /users/*"] = delete_user; |
4353
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
832 |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
833 ["GET /groups"] = list_groups; |
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
834 ["GET /groups/*"] = get_group_by_id; |
4360
76bec3f66b24
mod_http_admin_api: Switch PUT to POST where appropriate
Matthew Wild <mwild1@gmail.com>
parents:
4359
diff
changeset
|
835 ["POST /groups"] = create_group; |
5710
4c84cfb586c1
mod_http_admin_api: Support for adding/removing group MUCs
Matthew Wild <mwild1@gmail.com>
parents:
5284
diff
changeset
|
836 ["POST /groups/*"] = extend_group; |
4368
e0c8d866d58c
mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents:
4366
diff
changeset
|
837 ["PUT /groups/*"] = update_group; |
4353
535d80be110d
mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
838 ["DELETE /groups/*"] = delete_group; |
4371
3d01bc4547b2
mod_http_admin_api: Add /server/info endpoint for site_name and version
Matthew Wild <mwild1@gmail.com>
parents:
4369
diff
changeset
|
839 |
3d01bc4547b2
mod_http_admin_api: Add /server/info endpoint for site_name and version
Matthew Wild <mwild1@gmail.com>
parents:
4369
diff
changeset
|
840 ["GET /server/info"] = get_server_info; |
4576
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
841 |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
842 ["GET /server/metrics"] = get_server_metrics; |
cade5dac1003
mod_http_admin_api: Add endpoints for server maintenance
Jonas Schäfer <jonas@wielicki.name>
parents:
4517
diff
changeset
|
843 ["POST /server/announcement"] = post_server_announcement; |
4343
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
844 }; |
ee313922b8d1
mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
845 }); |