Mercurial > prosody-modules
comparison mod_http_admin_api/mod_http_admin_api.lua @ 4345:1bb08e9ffa82
mod_http_admin_api: Add methods for managing users
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 17 Jan 2021 13:58:52 +0000 |
parents | ee313922b8d1 |
children | 5ca36c36ab05 |
comparison
equal
deleted
inserted
replaced
4344:844cfc8c4039 | 4345:1bb08e9ffa82 |
---|---|
1 local usermanager = require "core.usermanager"; | |
2 | |
1 local json = require "util.json"; | 3 local json = require "util.json"; |
2 | 4 |
3 module:depends("http"); | 5 module:depends("http"); |
4 | 6 |
5 local invites = module:depends("invites"); | 7 local invites = module:depends("invites"); |
6 local tokens = module:depends("tokenauth"); | 8 local tokens = module:depends("tokenauth"); |
9 local mod_pep = module:depends("pep"); | |
7 | 10 |
8 local json_content_type = "application/json"; | 11 local json_content_type = "application/json"; |
9 | 12 |
10 local www_authenticate_header = ("Bearer realm=%q"):format(module.host.."/"..module.name); | 13 local www_authenticate_header = ("Bearer realm=%q"):format(module.host.."/"..module.name); |
11 | 14 |
100 return 404; | 103 return 404; |
101 end | 104 end |
102 return 200; | 105 return 200; |
103 end | 106 end |
104 | 107 |
108 local function get_user_info(username) | |
109 if not usermanager.user_exists(username, module.host) then | |
110 return nil; | |
111 end | |
112 local display_name; | |
113 do | |
114 local pep_service = mod_pep.get_pep_service(username); | |
115 local ok, _, nick_item = pep_service:get_last_item("http://jabber.org/protocol/nick", true); | |
116 if ok and nick_item then | |
117 display_name = nick_item:get_child_text("nick", "http://jabber.org/protocol/nick"); | |
118 end | |
119 end | |
120 | |
121 return { | |
122 username = username; | |
123 display_name = display_name; | |
124 }; | |
125 end | |
126 | |
127 function list_users(event) | |
128 local user_list = {}; | |
129 for username in usermanager.users(module.host) do | |
130 table.insert(user_list, get_user_info(username)); | |
131 end | |
132 | |
133 event.response.headers["Content-Type"] = json_content_type; | |
134 return json.encode(user_list); | |
135 end | |
136 | |
137 function get_user_by_name(event, username) | |
138 local user_info = get_user_info(username); | |
139 if not user_info then | |
140 return 404; | |
141 end | |
142 | |
143 event.response.headers["Content-Type"] = json_content_type; | |
144 return json.encode(user_info); | |
145 end | |
146 | |
147 function delete_user(event, username) --luacheck: ignore 212/event | |
148 if not usermanager.delete_user(username, module.host) then | |
149 return 404; | |
150 end | |
151 return 200; | |
152 end | |
153 | |
105 module:provides("http", { | 154 module:provides("http", { |
106 route = check_auth { | 155 route = check_auth { |
107 ["GET /invites"] = list_invites; | 156 ["GET /invites"] = list_invites; |
108 ["GET /invites/*"] = get_invite_by_id; | 157 ["GET /invites/*"] = get_invite_by_id; |
109 ["PUT /invites"] = create_invite; | 158 ["PUT /invites"] = create_invite; |
110 ["DELETE /invites/*"] = delete_invite; | 159 ["DELETE /invites/*"] = delete_invite; |
160 | |
161 ["GET /users"] = list_users; | |
162 ["GET /users/*"] = get_user_by_name; | |
163 ["DELETE /users/*"] = delete_user; | |
111 }; | 164 }; |
112 }); | 165 }); |