mirror of
https://github.com/azaion/admin.git
synced 2026-04-22 09:26:34 +00:00
db works, upload works
This commit is contained in:
@@ -49,7 +49,7 @@ public class AuthService(IHttpContextAccessor httpContextAccessor, IOptions<JwtC
|
||||
new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
|
||||
new Claim(ClaimTypes.Name, user.Email),
|
||||
new Claim(ClaimTypes.Role, user.Role.ToString()),
|
||||
new Claim(Constants.HARDWARE_ID, user.HardwareId)
|
||||
new Claim(Constants.HARDWARE_ID, user.HardwareId ?? "")
|
||||
]),
|
||||
Expires = DateTime.UtcNow.AddHours(jwtConfig.Value.TokenLifetimeHours),
|
||||
Issuer = jwtConfig.Value.Issuer,
|
||||
|
||||
@@ -14,12 +14,14 @@
|
||||
<Reference Include="Microsoft.AspNetCore.Http.Abstractions">
|
||||
<HintPath>C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\8.0.8\Microsoft.AspNetCore.Http.Abstractions.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.AspNetCore.Http.Features" />
|
||||
<Reference Include="Microsoft.Extensions.Options">
|
||||
<HintPath>C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\8.0.8\Microsoft.Extensions.Options.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
|
||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.1.2" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using Azaion.Common;
|
||||
using Azaion.Common.Configs;
|
||||
using Azaion.Common.Entities;
|
||||
using Azaion.Common.Requests;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Azaion.Services;
|
||||
@@ -9,7 +9,7 @@ namespace Azaion.Services;
|
||||
public interface IResourcesService
|
||||
{
|
||||
Task GetEncryptedResource(ResourceEnum resource, string key, Stream outputStream, CancellationToken cancellationToken = default);
|
||||
Task SaveResource(UploadResourceRequest request, CancellationToken cancellationToken = default);
|
||||
Task SaveResource(ResourceEnum resourceEnum, IFormFile data, CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
||||
public class ResourcesService(IOptions<ResourcesConfig> resourcesConfig) : IResourcesService
|
||||
@@ -20,10 +20,15 @@ public class ResourcesService(IOptions<ResourcesConfig> resourcesConfig) : IReso
|
||||
await fileStream.EncryptTo(outputStream, key, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task SaveResource(UploadResourceRequest request, CancellationToken cancellationToken = default)
|
||||
public async Task SaveResource(ResourceEnum resourceEnum, IFormFile data, CancellationToken cancellationToken = default)
|
||||
{
|
||||
await using var fileStream = new FileStream(GetResourcePath(request.ResourceEnum), FileMode.OpenOrCreate, FileAccess.ReadWrite);
|
||||
await request.Data.CopyToAsync(fileStream, cancellationToken);
|
||||
if (data == null)
|
||||
throw new BusinessException(ExceptionEnum.NoFile, "No file provided!");
|
||||
if (!Directory.Exists(resourcesConfig.Value.ResourcesFolder))
|
||||
Directory.CreateDirectory(resourcesConfig.Value.ResourcesFolder);
|
||||
|
||||
await using var fileStream = new FileStream(GetResourcePath(resourceEnum), FileMode.OpenOrCreate, FileAccess.ReadWrite);
|
||||
await data.CopyToAsync(fileStream, cancellationToken);
|
||||
}
|
||||
|
||||
private string GetResourcePath(ResourceEnum resourceEnum)
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace Azaion.Services;
|
||||
public interface IUserService
|
||||
{
|
||||
Task RegisterUser(RegisterUserRequest request, CancellationToken cancellationToken = default);
|
||||
Task<User> ValidateUser(string username, string password, string? hardwareId = null, CancellationToken cancellationToken = default);
|
||||
Task<User> ValidateUser(LoginRequest request, string? hardwareId = null, CancellationToken cancellationToken = default);
|
||||
Task UpdateHardwareId(string username, string hardwareId, CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ public class UserService(IDbFactory dbFactory) : IUserService
|
||||
|
||||
await db.InsertAsync(new User
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Email = request.Email,
|
||||
PasswordHash = request.Password.ToHash(),
|
||||
Role = request.Role
|
||||
@@ -32,14 +33,14 @@ public class UserService(IDbFactory dbFactory) : IUserService
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<User> ValidateUser(string username, string password, string? hardwareId = null, CancellationToken cancellationToken = default) =>
|
||||
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 == username, token: cancellationToken);
|
||||
var user = await db.Users.FirstOrDefaultAsync(x => x.Email == request.Email, token: cancellationToken);
|
||||
if (user == null)
|
||||
throw new BusinessException(ExceptionEnum.NoUserFound, "No user found");
|
||||
|
||||
if (password.ToHash() != user.PasswordHash)
|
||||
if (request.Password.ToHash() != user.PasswordHash)
|
||||
throw new BusinessException(ExceptionEnum.PasswordIncorrect, "Passwords do not match");
|
||||
|
||||
if (user.Role == RoleEnum.ApiAdmin)
|
||||
|
||||
Reference in New Issue
Block a user