Mercurial > prosody-modules
comparison mod_http_admin_api/mod_http_admin_api.lua @ 4382:0d6b69777bc1
mod_http_admin_api: port to use new mod_groups_internal module
Matthew Wild contributed much of the code, so I can only take
partial credit.
author | Jonas Schäfer <jonas@wielicki.name> |
---|---|
date | Mon, 25 Jan 2021 18:33:49 +0100 |
parents | d4e0e4d22fc7 |
children | 17d44ba8fde2 |
comparison
equal
deleted
inserted
replaced
4381:a1256e376dca | 4382:0d6b69777bc1 |
---|---|
1 local usermanager = require "core.usermanager"; | 1 local usermanager = require "core.usermanager"; |
2 | 2 |
3 local id = require "util.id"; | |
4 local json = require "util.json"; | 3 local json = require "util.json"; |
5 | 4 |
6 module:depends("http"); | 5 module:depends("http"); |
7 | 6 |
8 local invites = module:depends("invites"); | 7 local invites = module:depends("invites"); |
9 local tokens = module:depends("tokenauth"); | 8 local tokens = module:depends("tokenauth"); |
10 local mod_pep = module:depends("pep"); | 9 local mod_pep = module:depends("pep"); |
11 | 10 local mod_groups = module:depends("groups_internal"); |
12 local group_info_store = module:open_store("group_info"); | 11 |
13 local group_members_store = module:open_store("groups"); | |
14 local group_memberships = module:open_store("groups", "map"); | |
15 local push_errors = module:shared("cloud_notify/push_errors"); | 12 local push_errors = module:shared("cloud_notify/push_errors"); |
16 | 13 |
17 local site_name = module:get_option_string("site_name", module.host); | 14 local site_name = module:get_option_string("site_name", module.host); |
18 | 15 |
19 local json_content_type = "application/json"; | 16 local json_content_type = "application/json"; |
353 debug_info.omemo = get_user_omemo_info(username); | 350 debug_info.omemo = get_user_omemo_info(username); |
354 | 351 |
355 return debug_info; | 352 return debug_info; |
356 end | 353 end |
357 | 354 |
358 local function get_user_groups(username) | |
359 local groups; | |
360 do | |
361 local group_set = group_memberships:get_all(username); | |
362 if group_set and next(group_set) then | |
363 groups = {}; | |
364 for group_id in pairs(group_set) do | |
365 table.insert(groups, group_id); | |
366 end | |
367 end | |
368 end | |
369 return groups; | |
370 end | |
371 | |
372 function list_users(event) | 355 function list_users(event) |
373 local user_list = {}; | 356 local user_list = {}; |
374 for username in usermanager.users(module.host) do | 357 for username in usermanager.users(module.host) do |
375 table.insert(user_list, get_user_info(username)); | 358 table.insert(user_list, get_user_info(username)); |
376 end | 359 end |
389 end | 372 end |
390 end | 373 end |
391 | 374 |
392 if property == "groups" then | 375 if property == "groups" then |
393 event.response.headers["Content-Type"] = json_content_type; | 376 event.response.headers["Content-Type"] = json_content_type; |
394 return json.encode(get_user_groups(username)); | 377 return json.encode(mod_groups.get_user_groups(username)); |
395 elseif property == "debug" then | 378 elseif property == "debug" then |
396 event.response.headers["Content-Type"] = json_content_type; | 379 event.response.headers["Content-Type"] = json_content_type; |
397 return json.encode(get_user_debug_info(username)); | 380 return json.encode(get_user_debug_info(username)); |
398 end | 381 end |
399 | 382 |
413 return 200; | 396 return 200; |
414 end | 397 end |
415 | 398 |
416 function list_groups(event) | 399 function list_groups(event) |
417 local group_list = {}; | 400 local group_list = {}; |
418 for group_id in group_info_store:users() do | 401 for group_id in mod_groups.groups() do |
419 local group_info = group_info_store:get(group_id); | 402 local group_info = mod_groups.get_info(group_id); |
420 table.insert(group_list, { | 403 table.insert(group_list, { |
421 id = group_id; | 404 id = group_id; |
422 name = group_info.name; | 405 name = group_info.name; |
423 members = group_members_store:get(group_id); | 406 members = mod_groups.get_members(group_id); |
424 }); | 407 }); |
425 end | 408 end |
426 | 409 |
427 event.response.headers["Content-Type"] = json_content_type; | 410 event.response.headers["Content-Type"] = json_content_type; |
428 return json.encode_array(group_list); | 411 return json.encode_array(group_list); |
429 end | 412 end |
430 | 413 |
431 function get_group_by_id(event, group_id) | 414 function get_group_by_id(event, group_id) |
432 local group = group_info_store:get(group_id); | 415 local group = mod_groups.get_info(group_id); |
433 if not group then | 416 if not group then |
434 return 404; | 417 return 404; |
435 end | 418 end |
436 | 419 |
437 event.response.headers["Content-Type"] = json_content_type; | 420 event.response.headers["Content-Type"] = json_content_type; |
438 | 421 |
439 return json.encode({ | 422 return json.encode({ |
440 id = group_id; | 423 id = group_id; |
441 name = group.name; | 424 name = group.name; |
442 members = group_members_store:get(group_id); | 425 members = mod_groups.get_members(group_id); |
443 }); | 426 }); |
444 end | 427 end |
445 | 428 |
446 function create_group(event) | 429 function create_group(event) |
447 local request = event.request; | 430 local request = event.request; |
457 if not group.name then | 440 if not group.name then |
458 module:log("warn", "Group missing name property"); | 441 module:log("warn", "Group missing name property"); |
459 return 400; | 442 return 400; |
460 end | 443 end |
461 | 444 |
462 local group_id = id.short(); | 445 local group_id = mod_groups.create({ |
463 | |
464 local ok = group_info_store:set(group_id, { | |
465 name = group.name; | 446 name = group.name; |
466 }); | 447 }); |
467 if not ok then | 448 if not group_id then |
468 return 500; | 449 return 500; |
469 end | 450 end |
470 | 451 |
471 event.response.headers["Content-Type"] = json_content_type; | 452 event.response.headers["Content-Type"] = json_content_type; |
472 return json.encode({ | 453 return json.encode({ |
475 members = {}; | 456 members = {}; |
476 }); | 457 }); |
477 end | 458 end |
478 | 459 |
479 function update_group(event, group) --luacheck: ignore 212/event | 460 function update_group(event, group) --luacheck: ignore 212/event |
461 -- Add member | |
480 local group_id, member_name = group:match("^([^/]+)/members/([^/]+)$"); | 462 local group_id, member_name = group:match("^([^/]+)/members/([^/]+)$"); |
481 if group_id and member_name then | 463 if group_id and member_name then |
482 if not group_info_store:get(group_id) then | 464 if not mod_groups.add_member(group_id, member_name) then |
483 return 404; | |
484 elseif not group_memberships:set(group_id, member_name, true) then | |
485 return 500; | 465 return 500; |
486 end | 466 end |
487 return 204; | 467 return 204; |
488 end | 468 end |
489 | 469 |
490 local group_id = group:match("^([^/]+)$") | 470 local group_id = group:match("^([^/]+)$") |
491 if group_id then | 471 if group_id then |
492 local request = event.request; | 472 local request = event.request; |
493 if request.headers.content_type ~= json_content_type | 473 if request.headers.content_type ~= json_content_type or (not request.body or #request.body == 0) then |
494 or (not request.body or #request.body == 0) then | |
495 return 400; | 474 return 400; |
496 end | 475 end |
497 | 476 |
498 local update = json.decode(event.request.body); | 477 local update = json.decode(event.request.body); |
499 if not update then | 478 if not update then |
500 return 400; | 479 return 400; |
501 end | 480 end |
502 | 481 |
503 local group_info = group_info_store:get(group_id); | 482 local group_info = mod_groups.get_info(group_id); |
504 if not group_info then | 483 if not group_info then |
505 return 404; | 484 return 404; |
506 end | 485 end |
507 | 486 |
508 if update.name then | 487 if update.name then |
509 group_info["name"] = update.name | 488 group_info["name"] = update.name; |
510 end | 489 end |
511 group_info_store:set(group_id, group_info); | 490 if mod_groups.set_info(group_id, group_info) then |
512 return 204; | 491 return 204; |
513 end | 492 else |
514 return 400; | 493 return 500; |
494 end | |
495 end | |
496 return 404; | |
515 end | 497 end |
516 | 498 |
517 function delete_group(event, subpath) --luacheck: ignore 212/event | 499 function delete_group(event, subpath) --luacheck: ignore 212/event |
518 -- Check if this is a membership deletion and handle it | 500 -- Check if this is a membership deletion and handle it |
519 local group_id, member_name = subpath:match("^([^/]+)/members/([^/]+)$"); | 501 local group_id, member_name = subpath:match("^([^/]+)/members/([^/]+)$"); |
520 if group_id and member_name then | 502 if group_id and member_name then |
521 if not group_info_store:get(group_id) then | 503 if mod_groups.remove_member(group_id, member_name) then |
522 return 404; | 504 return 204; |
523 end | |
524 if group_memberships:set(group_id, member_name, nil) then | |
525 return 200; | |
526 else | 505 else |
527 return 500; | 506 return 500; |
528 end | 507 end |
529 else | 508 else |
509 -- Action refers to the group | |
530 group_id = subpath; | 510 group_id = subpath; |
531 end | 511 end |
532 | 512 |
533 if not group_id then | 513 if not group_id then |
534 return 400; | 514 return 400; |
535 end | 515 end |
536 | 516 |
537 if not group_info_store:get(group_id) then | 517 if not mod_groups.exists(group_id) then |
538 return 404; | 518 return 404; |
539 end | 519 end |
540 | 520 |
541 if not group_members_store:set(group_id, nil) then | 521 if not mod_groups.delete(group_id) then |
542 return 500; | 522 return 500; |
543 else | 523 end |
544 if not group_info_store:set(group_id, nil) then | 524 return 204; |
545 return 500; | |
546 end | |
547 end | |
548 return 200; | |
549 end | 525 end |
550 | 526 |
551 local function get_server_info(event) | 527 local function get_server_info(event) |
552 event.response.headers["Content-Type"] = json_content_type; | 528 event.response.headers["Content-Type"] = json_content_type; |
553 return json.encode({ | 529 return json.encode({ |