mirror of
https://github.com/azaion/admin.git
synced 2026-04-22 21:46:33 +00:00
f5e466108a
add ToHash for encryption Key
95 lines
3.8 KiB
C#
95 lines
3.8 KiB
C#
using Azaion.Common;
|
|
using Azaion.Common.Database;
|
|
using Azaion.Common.Entities;
|
|
using Azaion.Common.Extensions;
|
|
using Azaion.Common.Requests;
|
|
using LinqToDB;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace Azaion.Services;
|
|
|
|
public interface IUserService
|
|
{
|
|
Task RegisterUser(RegisterUserRequest request, CancellationToken cancellationToken = default);
|
|
Task<User> ValidateUser(LoginRequest request, string? hardwareId = null, CancellationToken cancellationToken = default);
|
|
Task UpdateHardware(string email, HardwareInfo hardwareInfo, CancellationToken cancellationToken = default);
|
|
Task<IEnumerable<User>> GetUsers(string? searchEmail, RoleEnum? searchRole, CancellationToken cancellationToken);
|
|
Task CheckHardware(User user, GetResourceRequest request);
|
|
}
|
|
|
|
public class UserService(IDbFactory dbFactory) : IUserService
|
|
{
|
|
public async Task RegisterUser(RegisterUserRequest request, CancellationToken cancellationToken = default)
|
|
{
|
|
await dbFactory.RunAdmin(async db =>
|
|
{
|
|
var existingUser = await db.Users.FirstOrDefaultAsync(u => u.Email == request.Email, token: cancellationToken);
|
|
if (existingUser != null)
|
|
throw new BusinessException(ExceptionEnum.EmailExists);
|
|
|
|
await db.InsertAsync(new User
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
Email = request.Email,
|
|
PasswordHash = request.Password.ToHash(),
|
|
Role = request.Role
|
|
}, token: cancellationToken);
|
|
});
|
|
}
|
|
|
|
public async Task<User> ValidateUser(LoginRequest request, string? hardwareId = null, CancellationToken cancellationToken = default) =>
|
|
await dbFactory.Run(async db =>
|
|
{
|
|
var user = await db.Users.FirstOrDefaultAsync(x => x.Email == request.Email, token: cancellationToken);
|
|
if (user == null)
|
|
throw new BusinessException(ExceptionEnum.NoEmailFound);
|
|
|
|
if (request.Password.ToHash() != user.PasswordHash)
|
|
throw new BusinessException(ExceptionEnum.WrongPassword);
|
|
|
|
if (user.Role == RoleEnum.ApiAdmin)
|
|
return user;
|
|
|
|
// For Non-API admins hardwareId should match if it was already set
|
|
if (user.HardwareHash != null && user.HardwareHash != hardwareId)
|
|
throw new BusinessException(ExceptionEnum.HardwareIdMismatch);
|
|
return user;
|
|
});
|
|
|
|
|
|
public async Task UpdateHardware(string email, HardwareInfo hardware, CancellationToken cancellationToken = default) =>
|
|
await dbFactory.RunAdmin(async db =>
|
|
{
|
|
var hardwareStr = JsonConvert.SerializeObject(hardware);
|
|
|
|
await db.Users.UpdateAsync(x => x.Email == email,
|
|
u => new User
|
|
{
|
|
Hardware = hardwareStr,
|
|
HardwareHash = hardware.Hash
|
|
}, token: cancellationToken);
|
|
});
|
|
|
|
|
|
public async Task<IEnumerable<User>> GetUsers(string? searchEmail, RoleEnum? searchRole, CancellationToken cancellationToken) =>
|
|
await dbFactory.Run(async db =>
|
|
await db.Users
|
|
.WhereIf(!string.IsNullOrEmpty(searchEmail),
|
|
u => u.Email.ToLower().Contains(searchEmail!.ToLower()))
|
|
.WhereIf(searchRole != null,
|
|
u => u.Role == searchRole)
|
|
.ToListAsync(token: cancellationToken));
|
|
|
|
public async Task CheckHardware(User user, GetResourceRequest request)
|
|
{
|
|
if (string.IsNullOrEmpty(user.HardwareHash))
|
|
{
|
|
await UpdateHardware(user.Email, request.Hardware);
|
|
user.HardwareHash = request.Hardware.Hash;
|
|
}
|
|
|
|
if (user.HardwareHash != request.Hardware.Hash)
|
|
throw new BusinessException(ExceptionEnum.HardwareIdMismatch);
|
|
}
|
|
}
|