| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- using InvercargillSql.Orm;
- using Spry.Authorisation;
- using Invercargill.Expressions;
- using Invercargill;
- using Invercargill.DataStructures;
- using Inversion;
- namespace Spry.Authentication {
- public class UserService : Object {
- private OrmSession db = inject<OrmSession>();
- private AuthorisationService authorisation_service = inject<AuthorisationService>();
- public async AuthorisationToken? authenticate_user(string username, string password) throws Error {
- var user = yield db.query<UserProjection>()
- .where_expr(expr("username == $0", new NativeElement<string>(username)))
- .first_async();
- if(!Sodium.PasswordHashing.check(user.password_hash, password)){
- return null;
- }
- return authorisation_service.authorise_identity(user);
- }
- public async UserEntity register_user(string username, string email, string forename, string surname, DateTime date_of_birth, string password, bool enabled = true) throws Error {
- var user = new UserEntity() {
- username = username,
- email = email,
- forename = forename,
- surname = surname,
- password_hash = Sodium.PasswordHashing.hash(password),
- date_of_birth = date_of_birth,
- created = new DateTime.now_utc(),
- modified = new DateTime.now_utc(),
- enabled = enabled,
- };
- db.insert<UserEntity>(user);
- return user;
- }
- public async void set_password(int64 user_id, string password) throws Error {
- var user = yield db.query<UserEntity>()
- .where_expr(expr("id == $0", new NativeElement<int64?>(user_id)))
- .first_async();
- user.password_hash = Sodium.PasswordHashing.hash(password);
- user.modified = new DateTime.now_utc();
- db.update<UserEntity>(user);
- }
- public async UserEntity alter_user(int64 user_id, string username, string email, string forename, string surname, DateTime date_of_birth, bool enabled) throws Error {
- var user = yield db.query<UserEntity>()
- .where_expr(expr("id == $0", new NativeElement<int64?>(user_id)))
- .first_async();
- user.username = username;
- user.email = email;
- user.forename = forename;
- user.surname = surname;
- user.date_of_birth = date_of_birth;
- user.modified = new DateTime.now_utc();
- user.enabled = enabled;
- db.update<UserEntity>(user);
- return user;
- }
- public async void set_user_enabled(int64 user_id, bool enabled) throws Error {
- var user = yield db.query<UserEntity>()
- .where_expr(expr("id == $0", new NativeElement<int64?>(user_id)))
- .first_async();
- user.modified = new DateTime.now_utc();
- user.enabled = enabled;
- db.update<UserEntity>(user);
- }
- public async ImmutableLot<UserProjection> list_users(int64 offset = 0, int64 limit = 100) throws Error {
- return yield db.query<UserProjection>()
- .offset(offset)
- .limit(limit)
- .materialise_async();
- }
- public async void delete_user(int64 user_id) throws Error {
- var user = yield db.query<UserEntity>()
- .where_expr(expr("id == $0", new NativeElement<int64?>(user_id)))
- .first_async();
- db.delete<UserEntity>(user);
- }
- public async void set_user_permission(int64 user_id, string permission) throws Error {
- var user_permission = new UserPermissionEntity() {
- user_id = user_id,
- permission = permission
- };
- db.insert<UserPermissionEntity>(user_permission);
- }
- public async void clear_user_permissions(int64 user_id) throws Error {
- var permissions = yield db.query<UserPermissionEntity>()
- .where_expr(expr("user_id == $0", new NativeElement<int64?>(user_id)))
- .materialise_async();
- foreach (var permission in permissions) {
- db.delete<UserPermissionEntity>(permission);
- }
- }
- public async ImmutableLot<string> get_user_permissions(int64 user_id) throws Error {
- var permissions = yield db.query<UserPermissionEntity>()
- .where_expr(expr("user_id == $0", new NativeElement<int64?>(user_id)))
- .materialise_async();
- var result = new Vector<string>();
- foreach (var permission in permissions) {
- result.add(permission.permission);
- }
- return result.to_immutable_buffer();
- }
- }
- }
|