Added permissions creation during startup
This commit is contained in:
32
MainProject/Utils/CommonDbMethodsUtils.cs
Normal file
32
MainProject/Utils/CommonDbMethodsUtils.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using BasicDotnetTemplate.MainProject.Core.Database;
|
||||
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Utils;
|
||||
public class CommonDbMethodsUtils
|
||||
{
|
||||
private readonly SqlServerContext _sqlServerContext;
|
||||
|
||||
public CommonDbMethodsUtils(SqlServerContext sqlServerContext)
|
||||
{
|
||||
_sqlServerContext = sqlServerContext;
|
||||
}
|
||||
|
||||
|
||||
public IQueryable<Role> GetRolesQueryable()
|
||||
{
|
||||
return this._sqlServerContext.Roles.Where(x => !x.IsDeleted);
|
||||
}
|
||||
|
||||
public IQueryable<Role> GetRoleByNameQueryable(string name)
|
||||
{
|
||||
return this.GetRolesQueryable().Where(x =>
|
||||
x.Name.ToString() == name.ToString()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,16 +1,5 @@
|
||||
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;
|
||||
|
||||
|
||||
|
||||
@@ -19,6 +8,10 @@ namespace BasicDotnetTemplate.MainProject.Utils;
|
||||
public static class FileUtils
|
||||
{
|
||||
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
private static readonly JsonSerializerOptions jsonSerializerOptions = new()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true
|
||||
};
|
||||
|
||||
public static T? ConvertFileToObject<T>(string? filePath = "")
|
||||
{
|
||||
@@ -38,10 +31,7 @@ public static class FileUtils
|
||||
{
|
||||
string fileContent = File.ReadAllText(filePath);
|
||||
|
||||
return JsonSerializer.Deserialize<T>(fileContent, new JsonSerializerOptions
|
||||
{
|
||||
PropertyNameCaseInsensitive = true
|
||||
});
|
||||
return JsonSerializer.Deserialize<T>(fileContent, jsonSerializerOptions);
|
||||
}
|
||||
catch (JsonException ex)
|
||||
{
|
||||
|
||||
@@ -277,13 +277,17 @@ public static class ProgramUtils
|
||||
Logger.Info("[ProgramUtils][CreatePermissions] Adding permissions...");
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
var permissionService = scope.ServiceProvider.GetRequiredService<IPermissionService>;
|
||||
Func<IPermissionService?> 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");
|
||||
foreach (var result in isValidThread.Result)
|
||||
{
|
||||
var currentResult = String.IsNullOrEmpty(result) ? "No permission tracked" : result;
|
||||
Logger.Info($"[ProgramUtils][CreatePermissions] => {currentResult}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user