mirror of
https://github.com/azaion/admin.git
synced 2026-04-22 08:56:33 +00:00
structure app by rest api standards
add getusers tidy up BusinessException
This commit is contained in:
@@ -28,7 +28,7 @@ public class ResourcesService(IOptions<ResourcesConfig> resourcesConfig) : IReso
|
||||
public async Task SaveResource(ResourceEnum resourceEnum, IFormFile data, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (data == null)
|
||||
throw new BusinessException(ExceptionEnum.NoFile, "No file provided!");
|
||||
throw new BusinessException(ExceptionEnum.NoFileProvided);
|
||||
if (!Directory.Exists(resourcesConfig.Value.ResourcesFolder))
|
||||
Directory.CreateDirectory(resourcesConfig.Value.ResourcesFolder);
|
||||
|
||||
@@ -40,7 +40,7 @@ public class ResourcesService(IOptions<ResourcesConfig> resourcesConfig) : IReso
|
||||
{
|
||||
var resource = resourcesConfig.Value.Resources.GetValueOrDefault(resourceEnum.ToString());
|
||||
if (resource == null)
|
||||
throw new BusinessException(ExceptionEnum.WrongResourceType, "Wrong resource type!");
|
||||
throw new BusinessException(ExceptionEnum.WrongResourceType);
|
||||
return Path.Combine(resourcesConfig.Value.ResourcesFolder, resource);
|
||||
}
|
||||
}
|
||||
@@ -10,8 +10,8 @@ public static class Security
|
||||
public static string ToHash(this string str) =>
|
||||
Convert.ToBase64String(SHA384.HashData(Encoding.UTF8.GetBytes(str)));
|
||||
|
||||
public static string MakeEncryptionKey(string username, string password) =>
|
||||
$"{username}-{password}---#%@AzaionKey@%#---";
|
||||
public static string MakeEncryptionKey(string username, string password, string hardwareId) =>
|
||||
$"{username}-{password}-{hardwareId}-#%@AzaionKey@%#---";
|
||||
|
||||
public static async Task EncryptTo(this Stream stream, Stream toStream, string key, CancellationToken cancellationToken = default)
|
||||
{
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using Azaion.Common;
|
||||
using Azaion.Common.Database;
|
||||
using Azaion.Common.Entities;
|
||||
using Azaion.Common.Extensions;
|
||||
using Azaion.Common.Requests;
|
||||
using LinqToDB;
|
||||
|
||||
@@ -11,6 +12,7 @@ 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
|
||||
@@ -21,7 +23,7 @@ public class UserService(IDbFactory dbFactory) : IUserService
|
||||
{
|
||||
var existingUser = await db.Users.FirstOrDefaultAsync(u => u.Email == request.Email, token: cancellationToken);
|
||||
if (existingUser != null)
|
||||
throw new BusinessException(ExceptionEnum.UserExists, "User already exists");
|
||||
throw new BusinessException(ExceptionEnum.EmailExists);
|
||||
|
||||
await db.InsertAsync(new User
|
||||
{
|
||||
@@ -38,21 +40,30 @@ public class UserService(IDbFactory dbFactory) : IUserService
|
||||
{
|
||||
var user = await db.Users.FirstOrDefaultAsync(x => x.Email == request.Email, token: cancellationToken);
|
||||
if (user == null)
|
||||
throw new BusinessException(ExceptionEnum.NoUserFound, "No user found");
|
||||
throw new BusinessException(ExceptionEnum.NoEmailFound);
|
||||
|
||||
if (request.Password.ToHash() != user.PasswordHash)
|
||||
throw new BusinessException(ExceptionEnum.PasswordIncorrect, "Passwords do not match");
|
||||
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, "Hardware id mismatch");
|
||||
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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user