UserService.vala 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. var user = yield db.query<UserProjection>()
  13. .where_expr(expr("username == $0", new NativeElement<string>(username)))
  14. .first_async();
  15. if(!Sodium.PasswordHashing.check(user.password_hash, password)){
  16. return null;
  17. }
  18. return authorisation_service.authorise_identity(user);
  19. }
  20. public async UserEntity register_user(string username, string email, string forename, string surname, DateTime date_of_birth, string password, bool enabled = true) throws Error {
  21. var user = new UserEntity() {
  22. username = username,
  23. email = email,
  24. forename = forename,
  25. surname = surname,
  26. password_hash = Sodium.PasswordHashing.hash(password),
  27. date_of_birth = date_of_birth,
  28. created = new DateTime.now_utc(),
  29. modified = new DateTime.now_utc(),
  30. enabled = enabled,
  31. };
  32. db.insert<UserEntity>(user);
  33. return user;
  34. }
  35. public async void set_password(int64 user_id, string password) throws Error {
  36. var user = yield db.query<UserEntity>()
  37. .where_expr(expr("id == $0", new NativeElement<int64?>(user_id)))
  38. .first_async();
  39. user.password_hash = Sodium.PasswordHashing.hash(password);
  40. user.modified = new DateTime.now_utc();
  41. db.update<UserEntity>(user);
  42. }
  43. public async UserEntity alter_user(int64 user_id, string username, string email, string forename, string surname, DateTime date_of_birth, bool enabled) throws Error {
  44. var user = yield db.query<UserEntity>()
  45. .where_expr(expr("id == $0", new NativeElement<int64?>(user_id)))
  46. .first_async();
  47. user.username = username;
  48. user.email = email;
  49. user.forename = forename;
  50. user.surname = surname;
  51. user.date_of_birth = date_of_birth;
  52. user.modified = new DateTime.now_utc();
  53. user.enabled = enabled;
  54. db.update<UserEntity>(user);
  55. return user;
  56. }
  57. public async void set_user_enabled(int64 user_id, bool enabled) throws Error {
  58. var user = yield db.query<UserEntity>()
  59. .where_expr(expr("id == $0", new NativeElement<int64?>(user_id)))
  60. .first_async();
  61. user.modified = new DateTime.now_utc();
  62. user.enabled = enabled;
  63. db.update<UserEntity>(user);
  64. }
  65. public async ImmutableLot<UserProjection> list_users(int64 offset = 0, int64 limit = 100) throws Error {
  66. return yield db.query<UserProjection>()
  67. .offset(offset)
  68. .limit(limit)
  69. .materialise_async();
  70. }
  71. public async void delete_user(int64 user_id) throws Error {
  72. var user = yield db.query<UserEntity>()
  73. .where_expr(expr("id == $0", new NativeElement<int64?>(user_id)))
  74. .first_async();
  75. db.delete<UserEntity>(user);
  76. }
  77. public async void set_user_permission(int64 user_id, string permission) throws Error {
  78. var user_permission = new UserPermissionEntity() {
  79. user_id = user_id,
  80. permission = permission
  81. };
  82. db.insert<UserPermissionEntity>(user_permission);
  83. }
  84. public async void clear_user_permissions(int64 user_id) throws Error {
  85. var permissions = yield db.query<UserPermissionEntity>()
  86. .where_expr(expr("user_id == $0", new NativeElement<int64?>(user_id)))
  87. .materialise_async();
  88. foreach (var permission in permissions) {
  89. db.delete<UserPermissionEntity>(permission);
  90. }
  91. }
  92. public async ImmutableLot<string> get_user_permissions(int64 user_id) throws Error {
  93. var permissions = yield db.query<UserPermissionEntity>()
  94. .where_expr(expr("user_id == $0", new NativeElement<int64?>(user_id)))
  95. .materialise_async();
  96. var result = new Vector<string>();
  97. foreach (var permission in permissions) {
  98. result.add(permission.permission);
  99. }
  100. return result.to_immutable_buffer();
  101. }
  102. }
  103. }