UserService.vala 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using InvercargillSql.Orm;
  2. using Spry.Authorisation;
  3. using Invercargill.Expressions;
  4. using Invercargill;
  5. using Invercargill.DataStructures;
  6. using Inversion;
  7. namespace Spry.Authentication {
  8. public class UserService : Object {
  9. private OrmSession db = inject<OrmSession>();
  10. private AuthorisationService authorisation_service = inject<AuthorisationService>();
  11. public async AuthorisationToken? authenticate_user(string username, string password) throws Error {
  12. print(expr("username == $0", new NativeElement<string>(username)).to_expression_string());
  13. var user = yield db.query<UserProjection>()
  14. .where(expr("username == $0", new NativeElement<string>(username)).to_expression_string())
  15. .first_async();
  16. if(!Sodium.PasswordHashing.check(user.password_hash, password)){
  17. return null;
  18. }
  19. return authorisation_service.authorise_identity(user);
  20. }
  21. public async UserEntity register_user(string username, string email, string forename, string surname, DateTime date_of_birth, string password, bool enabled = true) throws Error {
  22. var user = new UserEntity() {
  23. username = username,
  24. email = email,
  25. forename = forename,
  26. surname = surname,
  27. password_hash = Sodium.PasswordHashing.hash(password),
  28. date_of_birth = date_of_birth,
  29. created = new DateTime.now_utc(),
  30. modified = new DateTime.now_utc(),
  31. enabled = enabled,
  32. };
  33. yield yield db.insert_async<UserEntity>(user);
  34. return user;
  35. }
  36. public async void set_password(int64 user_id, string password) throws Error {
  37. var user = yield db.query<UserEntity>()
  38. .where(expr("id == $0", new NativeElement<int64?>(user_id)).to_expression_string())
  39. .first_async();
  40. user.password_hash = Sodium.PasswordHashing.hash(password);
  41. user.modified = new DateTime.now_utc();
  42. yield yield db.update_async<UserEntity>(user);
  43. }
  44. public async UserEntity alter_user(int64 user_id, string username, string email, string forename, string surname, DateTime date_of_birth, bool enabled) throws Error {
  45. var user = yield db.query<UserEntity>()
  46. .where(expr("id == $0", new NativeElement<int64?>(user_id)).to_expression_string())
  47. .first_async();
  48. user.username = username;
  49. user.email = email;
  50. user.forename = forename;
  51. user.surname = surname;
  52. user.date_of_birth = date_of_birth;
  53. user.modified = new DateTime.now_utc();
  54. user.enabled = enabled;
  55. yield db.update_async<UserEntity>(user);
  56. return user;
  57. }
  58. public async void set_user_enabled(int64 user_id, bool enabled) throws Error {
  59. var user = yield db.query<UserEntity>()
  60. .where(expr("id == $0", new NativeElement<int64?>(user_id)).to_expression_string())
  61. .first_async();
  62. user.modified = new DateTime.now_utc();
  63. user.enabled = enabled;
  64. yield db.update_async<UserEntity>(user);
  65. }
  66. public async ImmutableLot<UserProjection> list_users(int64 offset = 0, int64 limit = 100) throws Error {
  67. return yield db.query<UserProjection>()
  68. .offset(offset)
  69. .limit(limit)
  70. .materialise_async();
  71. }
  72. public async void delete_user(int64 user_id) throws Error {
  73. var user = yield db.query<UserEntity>()
  74. .where(expr("id == $0", new NativeElement<int64?>(user_id)).to_expression_string())
  75. .first_async();
  76. db.delete<UserEntity>(user);
  77. }
  78. public async void set_user_permission(int64 user_id, string permission) throws Error {
  79. var user_permission = new UserPermissionEntity() {
  80. user_id = user_id,
  81. permission = permission
  82. };
  83. yield db.insert_async<UserPermissionEntity>(user_permission);
  84. }
  85. public async void clear_user_permissions(int64 user_id) throws Error {
  86. var permissions = yield db.query<UserPermissionEntity>()
  87. .where(expr("user_id == $0", new NativeElement<int64?>(user_id)).to_expression_string())
  88. .materialise_async();
  89. foreach (var permission in permissions) {
  90. db.delete<UserPermissionEntity>(permission);
  91. }
  92. }
  93. public async ImmutableLot<string> get_user_permissions(int64 user_id) throws Error {
  94. var permissions = yield db.query<UserPermissionEntity>()
  95. .where(expr("user_id == $0", new NativeElement<int64?>(user_id)).to_expression_string())
  96. .materialise_async();
  97. var result = new Vector<string>();
  98. foreach (var permission in permissions) {
  99. result.add(permission.permission);
  100. }
  101. return result.to_immutable_buffer();
  102. }
  103. }
  104. }