mirror of
https://github.com/azaion/admin.git
synced 2026-04-22 16:46:34 +00:00
renmove ResourceEnum, use filename only
add ToHash for encryption Key
This commit is contained in:
@@ -4,6 +4,7 @@ using Azaion.Common.Entities;
|
||||
using Azaion.Common.Extensions;
|
||||
using Azaion.Common.Requests;
|
||||
using LinqToDB;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Azaion.Services;
|
||||
|
||||
@@ -11,8 +12,9 @@ public interface IUserService
|
||||
{
|
||||
Task RegisterUser(RegisterUserRequest request, CancellationToken cancellationToken = default);
|
||||
Task<User> ValidateUser(LoginRequest request, string? hardwareId = null, CancellationToken cancellationToken = default);
|
||||
Task UpdateHardwareId(string email, string hardwareId, 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
|
||||
@@ -49,14 +51,25 @@ public class UserService(IDbFactory dbFactory) : IUserService
|
||||
return user;
|
||||
|
||||
// For Non-API admins hardwareId should match if it was already set
|
||||
if (user.HardwareId != null && user.HardwareId != hardwareId)
|
||||
if (user.HardwareHash != null && user.HardwareHash != hardwareId)
|
||||
throw new BusinessException(ExceptionEnum.HardwareIdMismatch);
|
||||
return user;
|
||||
});
|
||||
|
||||
public async Task UpdateHardwareId(string email, string hardwareId, CancellationToken cancellationToken = default) =>
|
||||
|
||||
public async Task UpdateHardware(string email, HardwareInfo hardware, CancellationToken cancellationToken = default) =>
|
||||
await dbFactory.RunAdmin(async db =>
|
||||
await db.Users.UpdateAsync(x => x.Email == email, u => new User { HardwareId = hardwareId}, token: cancellationToken));
|
||||
{
|
||||
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 =>
|
||||
@@ -66,4 +79,16 @@ public class UserService(IDbFactory dbFactory) : IUserService
|
||||
.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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user