UserProjection.vala 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using InvercargillSql.Orm.Projections;
  2. using Invercargill.DataStructures;
  3. using Spry.Authorisation;
  4. using Invercargill;
  5. using Invercargill.Expressions;
  6. namespace Spry.Authentication {
  7. public class UserProjection : Object, Identity {
  8. public Element identifier { owned get { return new NativeElement<int64?>(id); } }
  9. public string username { get { return _username; } }
  10. public ImmutableLot<string> permissions { owned get { return _permissions.to_immutable_buffer(); } }
  11. public Invercargill.Properties data { owned get {
  12. var props = new DataStructures.PropertyDictionary();
  13. props.set_native<string>("email", email);
  14. props.set_native<string>("forename", forename);
  15. props.set_native<string>("surname", surname);
  16. props.set_native<DateTime>("date_of_birth", date_of_birth);
  17. props.set_native<DateTime>("created", created);
  18. props.set_native<DateTime>("modified", modified);
  19. return props;
  20. }}
  21. public int64 id { get; set; }
  22. public string email { get; set; }
  23. public string forename { get; set; }
  24. public string surname { get; set; }
  25. public string password_hash { get; set; }
  26. public DateTime date_of_birth { get; set; }
  27. public DateTime created { get; set; }
  28. public DateTime modified { get; set; }
  29. public bool enabled { get; set; }
  30. private string _username;
  31. private ImmutableBuffer<string> _permissions;
  32. public static void projection_mapping(ProjectionBuilder<UserProjection> cfg) throws Error {
  33. cfg.source<UserEntity>("u")
  34. .select<int64?>("id", expr("u.id"), (o, v) => o.id = v)
  35. .select<string>("username", expr("u.username"), (o, v) => o._username = v)
  36. .select<string>("email", expr("u.email"), (o, v) => o.email = v)
  37. .select<string>("forename", expr("u.forename"), (o, v) => o.forename = v)
  38. .select<string>("surname", expr("u.surname"), (o, v) => o.surname = v)
  39. .select<string>("password_hash", expr("u.password_hash"), (o, v) => o.password_hash = v)
  40. .select<DateTime>("date_of_birth", expr("u.date_of_birth"), (o, v) => o.date_of_birth = v)
  41. .select<DateTime>("created", expr("u.created"), (o, v) => o.created = v)
  42. .select<DateTime>("modified", expr("u.modified"), (o, v) => o.modified = v)
  43. .select<bool>("enabled", expr("u.enabled"), (o, v) => o.enabled = v)
  44. .join<UserPermissionEntity>("p", expr("p.user_id == u.id"))
  45. .select_many<string>("permissions", expr("p.permission"), (o, v) => o._permissions = v.to_immutable_buffer());
  46. }
  47. }
  48. }