mirror of
https://github.com/azaion/admin.git
synced 2026-04-22 11:36:33 +00:00
4bc76bbbac
add getusers tidy up BusinessException
70 lines
3.0 KiB
C#
70 lines
3.0 KiB
C#
using Azaion.Common;
|
|
using Azaion.Common.Database;
|
|
using Azaion.Common.Entities;
|
|
using Azaion.Common.Extensions;
|
|
using Azaion.Common.Requests;
|
|
using LinqToDB;
|
|
|
|
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 UpdateHardwareId(string email, string hardwareId, CancellationToken cancellationToken = default);
|
|
Task<IEnumerable<User>> GetUsers(string searchEmail, RoleEnum? searchRole, CancellationToken cancellationToken);
|
|
}
|
|
|
|
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.HardwareId != null && user.HardwareId != hardwareId)
|
|
throw new BusinessException(ExceptionEnum.HardwareIdMismatch);
|
|
return user;
|
|
});
|
|
|
|
public async Task UpdateHardwareId(string email, string hardwareId, CancellationToken cancellationToken = default) =>
|
|
await dbFactory.RunAdmin(async db =>
|
|
await db.Users.UpdateAsync(x => x.Email == email, u => new User { HardwareId = hardwareId}, 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));
|
|
}
|