comparison mod_http_admin_api/mod_http_admin_api.lua @ 4353:535d80be110d

mod_http_admin_api: Add groups management endpoints
author Matthew Wild <mwild1@gmail.com>
date Sun, 17 Jan 2021 17:52:03 +0000
parents f6da234b21b8
children d61d7d30f38d
comparison
equal deleted inserted replaced
4352:f6da234b21b8 4353:535d80be110d
6 6
7 local invites = module:depends("invites"); 7 local invites = module:depends("invites");
8 local tokens = module:depends("tokenauth"); 8 local tokens = module:depends("tokenauth");
9 local mod_pep = module:depends("pep"); 9 local mod_pep = module:depends("pep");
10 10
11 local group_store = module:open_store("groups");
11 local group_memberships = module:open_store("groups", "map"); 12 local group_memberships = module:open_store("groups", "map");
12 13
13 local json_content_type = "application/json"; 14 local json_content_type = "application/json";
14 15
15 local www_authenticate_header = ("Bearer realm=%q"):format(module.host.."/"..module.name); 16 local www_authenticate_header = ("Bearer realm=%q"):format(module.host.."/"..module.name);
194 return 404; 195 return 404;
195 end 196 end
196 return 200; 197 return 200;
197 end 198 end
198 199
200 function list_groups(event)
201 local group_list = {};
202 for group_id in group_store:users() do
203 table.insert(group_list, {
204 id = group_id;
205 name = group_id;
206 });
207 end
208
209 event.response.headers["Content-Type"] = json_content_type;
210 return json.encode_array(group_list);
211 end
212
213 function get_group_by_id(event, group_id)
214 local property;
215 do
216 local id, sub_path = group_id:match("^[^/]+/(%w+)$");
217 if id then
218 group_id = id;
219 property = sub_path;
220 end
221 end
222
223 local group = group_store:get(group_id);
224 if not group then
225 return 404;
226 end
227
228 event.response.headers["Content-Type"] = json_content_type;
229
230 if property == "members" then
231 return json.encode(group);
232 end
233
234 return json.encode({
235 id = group_id;
236 name = group_id;
237 });
238 end
239
240 function create_group(event)
241 local request = event.request;
242 if request.headers.content_type ~= json_content_type
243 or (not request.body or #request.body == 0) then
244 return 400;
245 end
246 local group = json.decode(event.request.body);
247 if not group then
248 return 400;
249 end
250
251 local ok = group_store:set(group.id, {});
252 if not ok then
253 return 500;
254 end
255 return 200;
256 end
257
258 function delete_group(event, group_id) --luacheck: ignore 212/event
259 if not group_id then
260 return 400;
261 end
262 if not group_store:set(group_id, nil) then
263 return 500;
264 end
265 return 200;
266 end
267
199 module:provides("http", { 268 module:provides("http", {
200 route = check_auth { 269 route = check_auth {
201 ["GET /invites"] = list_invites; 270 ["GET /invites"] = list_invites;
202 ["GET /invites/*"] = get_invite_by_id; 271 ["GET /invites/*"] = get_invite_by_id;
203 ["PUT /invites"] = create_invite; 272 ["PUT /invites"] = create_invite;
204 ["DELETE /invites/*"] = delete_invite; 273 ["DELETE /invites/*"] = delete_invite;
205 274
206 ["GET /users"] = list_users; 275 ["GET /users"] = list_users;
207 ["GET /users/*"] = get_user_by_name; 276 ["GET /users/*"] = get_user_by_name;
208 ["DELETE /users/*"] = delete_user; 277 ["DELETE /users/*"] = delete_user;
278
279 ["GET /groups"] = list_groups;
280 ["GET /groups/*"] = get_group_by_id;
281 ["PUT /groups"] = create_group;
282 ["DELETE /groups/*"] = delete_group;
209 }; 283 };
210 }); 284 });