Adding permissions during startup
This commit is contained in:
52
MainProject/Utils/FileUtils.cs.cs
Normal file
52
MainProject/Utils/FileUtils.cs.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using MongoDB.Driver;
|
||||
using NLog;
|
||||
using BasicDotnetTemplate.MainProject.Core.Database;
|
||||
using BasicDotnetTemplate.MainProject.Core.Middlewares;
|
||||
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||
using BasicDotnetTemplate.MainProject.Services;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Data.Role;
|
||||
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||
|
||||
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Utils;
|
||||
|
||||
public static class FileUtils
|
||||
{
|
||||
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
public static T? ConvertFileToObject<T>(string? filePath = "")
|
||||
{
|
||||
Logger.Info("[FileUtils][ReadJson] Reading file");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(filePath))
|
||||
{
|
||||
throw new ArgumentException("filePath cannot be null or empty", nameof(filePath));
|
||||
}
|
||||
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
throw new FileNotFoundException("The specified file does not exists", filePath);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
string fileContent = File.ReadAllText(filePath);
|
||||
|
||||
return JsonSerializer.Deserialize<T>(fileContent, new JsonSerializerOptions
|
||||
{
|
||||
PropertyNameCaseInsensitive = true
|
||||
});
|
||||
}
|
||||
catch (JsonException ex)
|
||||
{
|
||||
throw new InvalidOperationException("Error during file deserialization", ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -218,6 +218,7 @@ public static class ProgramUtils
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
builder.Services.AddScoped<IAuthService, AuthService>();
|
||||
builder.Services.AddScoped<IJwtService, JwtService>();
|
||||
builder.Services.AddScoped<IPermissionService, PermissionService>();
|
||||
builder.Services.AddScoped<IRoleService, RoleService>();
|
||||
builder.Services.AddScoped<IUserService, UserService>();
|
||||
Logger.Info("[ProgramUtils][AddScopes] Done scopes");
|
||||
@@ -271,4 +272,25 @@ public static class ProgramUtils
|
||||
|
||||
}
|
||||
|
||||
public static void CreatePermissions(ref WebApplication app)
|
||||
{
|
||||
Logger.Info("[ProgramUtils][CreatePermissions] Adding permissions...");
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
var permissionService = scope.ServiceProvider.GetRequiredService<IPermissionService>;
|
||||
if (permissionService != null)
|
||||
{
|
||||
var isValidThread = Task.Run(() => permissionService!.Invoke()?.CreatePermissionsOnStartupAsync());
|
||||
if (isValidThread.Result != null)
|
||||
{
|
||||
Logger.Info("[ProgramUtils][CreatePermissions] Done permissions");
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.Error("[ProgramUtils][CreatePermissions] Something went wrong");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user