UserManagementEntrypoint.vala 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using Inversion;
  2. using Invercargill.DataStructures;
  3. using InvercargillJson;
  4. using Spry;
  5. using Spry.Authentication;
  6. using Statum;
  7. namespace {{NS}} {
  8. /** One row of the users table, serialised into the users slot's JSON array. */
  9. public class UserRow : Object {
  10. public string id { get; set; default = ""; }
  11. public string username { get; set; default = ""; }
  12. public string email { get; set; default = ""; }
  13. public bool enabled { get; set; }
  14. public string[] permissions { get; set; }
  15. public Statum.Model.ActionDto set_enabled { get; set; }
  16. public Statum.Model.ActionDto grant { get; set; }
  17. public Statum.Model.ActionDto revoke { get; set; }
  18. public Statum.Model.ActionDto delete { get; set; }
  19. }
  20. /**
  21. * Entrypoint for /users: admin-guarded. Loads every user (with
  22. * permissions) from the UserService into the `users` slot as a JSON array
  23. * of rows, each carrying enable/grant/revoke/delete action references
  24. * sealed with the row's user_id (Spry.Actions.UserActionPrivate) under
  25. * the base Spry action types — the shipped actions decrypt the blob
  26. * themselves, so no subclasses are needed.
  27. */
  28. public class UserManagementEntrypoint : StatumEntrypoint {
  29. private UserService users = inject<UserService>();
  30. public override async DirectiveBuilder handle() throws GLib.Error {
  31. var guard = yield SpryAuth.require_permission(directives(), request, users,
  32. SpryAuth.ADMIN_PERMISSION);
  33. if (guard != null) {
  34. return (!)guard;
  35. }
  36. var rows = new Series<UserRow>();
  37. foreach (var user in yield users.list_users(0, 200)) {
  38. var sealed = new Spry.Actions.UserActionPrivate();
  39. sealed.user_id = user.id;
  40. var row = new UserRow();
  41. row.id = user.id.to_string();
  42. row.username = user.username;
  43. row.email = user.email ?? "";
  44. row.enabled = user.enabled;
  45. row.permissions = user.permissions.any() ? user.permissions.to_array() : new string[0];
  46. row.set_enabled = action_registry.author_private<Spry.Actions.SetUserEnabledAction, Spry.Actions.UserActionPrivate>(sealed);
  47. row.grant = action_registry.author_private<Spry.Actions.GrantPermissionAction, Spry.Actions.UserActionPrivate>(sealed);
  48. row.revoke = action_registry.author_private<Spry.Actions.RevokePermissionAction, Spry.Actions.UserActionPrivate>(sealed);
  49. row.delete = action_registry.author_private<Spry.Actions.DeleteUserAction, Spry.Actions.UserActionPrivate>(sealed);
  50. rows.add(row);
  51. }
  52. var array = new JsonArray();
  53. foreach (var row in rows) {
  54. array.add(new JsonElement.from_properties(GObjectMapping.to_properties(row)));
  55. }
  56. var public_data = new PropertyDictionary();
  57. public_data["users"] = new JsonElement.from_elements(array);
  58. var slot = state_service.new_slot(Statum.Scope.PAGE, new Statum.State() {
  59. type_name = "users",
  60. public_data = public_data,
  61. private_data = new PropertyDictionary()
  62. });
  63. return directives().set(slot);
  64. }
  65. }
  66. }