Adding permissions during startup
This commit is contained in:
32
MainProject/Config/permissions.json
Normal file
32
MainProject/Config/permissions.json
Normal file
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"PermissionInfos": [
|
||||
{
|
||||
"System": "base",
|
||||
"RolePermissionModuleOperations": [
|
||||
{
|
||||
"Module": "role",
|
||||
"Operations": [
|
||||
{ "Operation": "create", "Roles": [] },
|
||||
{ "Operation": "read", "Roles": [] },
|
||||
{ "Operation": "update", "Roles": [] },
|
||||
{ "Operation": "delete", "Roles": [] },
|
||||
{ "Operation": "list", "Roles": [] },
|
||||
{ "Operation": "use", "Roles": [] }
|
||||
]
|
||||
},
|
||||
{
|
||||
"Module": "user",
|
||||
"Operations": [
|
||||
{ "Operation": "create", "Roles": [] },
|
||||
{ "Operation": "read", "Roles": [] },
|
||||
{ "Operation": "update", "Roles": [] },
|
||||
{ "Operation": "delete", "Roles": [] },
|
||||
{ "Operation": "list", "Roles": [] },
|
||||
{ "Operation": "use", "Roles": [] }
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
]
|
||||
}
|
||||
9
MainProject/Models/Common/OperationInfo.cs
Normal file
9
MainProject/Models/Common/OperationInfo.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace BasicDotnetTemplate.MainProject.Models.Common;
|
||||
|
||||
public class OperationInfo
|
||||
{
|
||||
#nullable enable
|
||||
public string? Operation { get; set; }
|
||||
public List<string>? Roles {get; set; }
|
||||
#nullable disable
|
||||
}
|
||||
9
MainProject/Models/Common/PermissionInfo.cs
Normal file
9
MainProject/Models/Common/PermissionInfo.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace BasicDotnetTemplate.MainProject.Models.Common;
|
||||
|
||||
public class PermissionInfo
|
||||
{
|
||||
#nullable enable
|
||||
public string? System { get; set; }
|
||||
public List<RolePermissionModuleOperation>? RolePermissionModuleOperations {get; set; }
|
||||
#nullable disable
|
||||
}
|
||||
8
MainProject/Models/Common/PermissionsFile.cs
Normal file
8
MainProject/Models/Common/PermissionsFile.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace BasicDotnetTemplate.MainProject.Models.Common;
|
||||
|
||||
public class PermissionsFile
|
||||
{
|
||||
#nullable enable
|
||||
public List<PermissionInfo>? PermissionInfos { get; set; }
|
||||
#nullable disable
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace BasicDotnetTemplate.MainProject.Models.Common;
|
||||
|
||||
public class RolePermissionModuleOperation
|
||||
{
|
||||
#nullable enable
|
||||
public string? Module { get; set; }
|
||||
public List<OperationInfo>? Operations { get; set; }
|
||||
#nullable disable
|
||||
}
|
||||
@@ -9,6 +9,6 @@ public class AppSettings
|
||||
public DatabaseSettings? DatabaseSettings { get; set; }
|
||||
public JwtSettings? JwtSettings { get; set; }
|
||||
public EncryptionSettings? EncryptionSettings { get; set; }
|
||||
|
||||
public PermissionsSettings? PermissionsSettings { get; set; }
|
||||
#nullable disable
|
||||
}
|
||||
8
MainProject/Models/Settings/PermissionsSettings.cs
Normal file
8
MainProject/Models/Settings/PermissionsSettings.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace BasicDotnetTemplate.MainProject.Models.Settings;
|
||||
|
||||
public class PermissionsSettings
|
||||
{
|
||||
#nullable enable
|
||||
public string? FilePath { get; set; }
|
||||
#nullable disable
|
||||
}
|
||||
@@ -45,6 +45,7 @@ internal static class Program
|
||||
WebApplication app = builder.Build();
|
||||
ProgramUtils.AddMiddlewares(ref app);
|
||||
ProgramUtils.CreateRoles(ref app);
|
||||
ProgramUtils.CreatePermissions(ref app);
|
||||
|
||||
Logger.Info("[Program][Initialize] End building");
|
||||
return app;
|
||||
|
||||
@@ -4,6 +4,8 @@ using BasicDotnetTemplate.MainProject.Core.Database;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Common.Exceptions;
|
||||
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using BasicDotnetTemplate.MainProject.Models.Common;
|
||||
using BasicDotnetTemplate.MainProject.Utils;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Services;
|
||||
|
||||
@@ -57,6 +59,9 @@ public interface IPermissionService
|
||||
bool enabled
|
||||
);
|
||||
Task<bool?> DeleteRolePermissionSystemModuleOperationAsync(RolePermissionSystemModuleOperation permission);
|
||||
|
||||
Task<List<string>?> CreatePermissionsOnStartupAsync();
|
||||
|
||||
}
|
||||
|
||||
public class PermissionService : BaseService, IPermissionService
|
||||
@@ -221,7 +226,7 @@ public class PermissionService : BaseService, IPermissionService
|
||||
return permission;
|
||||
}
|
||||
|
||||
#region "PermissionSystem"
|
||||
#region "PermissionSystem"
|
||||
|
||||
public async Task<PermissionSystem?> GetPermissionSystemByGuidAsync(string guid)
|
||||
{
|
||||
@@ -290,10 +295,10 @@ public class PermissionService : BaseService, IPermissionService
|
||||
return deleted;
|
||||
}
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
|
||||
#region "PermissionModule"
|
||||
#region "PermissionModule"
|
||||
|
||||
|
||||
public async Task<PermissionModule?> GetPermissionModuleByGuidAsync(string guid)
|
||||
@@ -363,10 +368,10 @@ public class PermissionService : BaseService, IPermissionService
|
||||
return deleted;
|
||||
}
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
|
||||
#region "PermissionOperation"
|
||||
#region "PermissionOperation"
|
||||
|
||||
public async Task<PermissionOperation?> GetPermissionOperationByGuidAsync(string guid)
|
||||
{
|
||||
@@ -418,10 +423,10 @@ public class PermissionService : BaseService, IPermissionService
|
||||
return deleted;
|
||||
}
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
|
||||
#region "PermissionSystemModule"
|
||||
#region "PermissionSystemModule"
|
||||
|
||||
public async Task<PermissionSystemModule?> GetPermissionSystemModuleByGuidAsync(string guid)
|
||||
{
|
||||
@@ -489,10 +494,10 @@ public class PermissionService : BaseService, IPermissionService
|
||||
return deleted;
|
||||
}
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
|
||||
#region "PermissionSystemModuleOperation"
|
||||
#region "PermissionSystemModuleOperation"
|
||||
|
||||
public async Task<PermissionSystemModuleOperation?> GetPermissionSystemModuleOperationByGuidAsync(string guid)
|
||||
{
|
||||
@@ -560,10 +565,10 @@ public class PermissionService : BaseService, IPermissionService
|
||||
return deleted;
|
||||
}
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
|
||||
#region "RolePermissionSystemModuleOperation"
|
||||
#region "RolePermissionSystemModuleOperation"
|
||||
|
||||
public async Task<RolePermissionSystemModuleOperation?> GetRolePermissionSystemModuleOperationByGuidAsync(string guid)
|
||||
{
|
||||
@@ -631,7 +636,24 @@ public class PermissionService : BaseService, IPermissionService
|
||||
return deleted;
|
||||
}
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
public async Task<List<string>?> CreatePermissionsOnStartupAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
List<string>? newPermissions = null;
|
||||
PermissionsFile? permissionsFile = FileUtils.ConvertFileToObject<PermissionsFile>(System.AppDomain.CurrentDomain.BaseDirectory + this._appSettings.PermissionsSettings.FilePath);
|
||||
return newPermissions;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Logger.Error(exception, $"[PermissionService][CreatePermissionsOnStartupAsync]");
|
||||
throw new CreateException($"An error occurred while adding permissions during startup", exception);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -37,6 +37,9 @@
|
||||
"EncryptionSettings": {
|
||||
"Salt": "S7VIidfXQf1tOQYX",
|
||||
"Pepper": ""
|
||||
},
|
||||
"PermissionsSettings": {
|
||||
"FilePath": "Config/permissions.json"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user