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 DatabaseSettings? DatabaseSettings { get; set; }
|
||||||
public JwtSettings? JwtSettings { get; set; }
|
public JwtSettings? JwtSettings { get; set; }
|
||||||
public EncryptionSettings? EncryptionSettings { get; set; }
|
public EncryptionSettings? EncryptionSettings { get; set; }
|
||||||
|
public PermissionsSettings? PermissionsSettings { get; set; }
|
||||||
#nullable disable
|
#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();
|
WebApplication app = builder.Build();
|
||||||
ProgramUtils.AddMiddlewares(ref app);
|
ProgramUtils.AddMiddlewares(ref app);
|
||||||
ProgramUtils.CreateRoles(ref app);
|
ProgramUtils.CreateRoles(ref app);
|
||||||
|
ProgramUtils.CreatePermissions(ref app);
|
||||||
|
|
||||||
Logger.Info("[Program][Initialize] End building");
|
Logger.Info("[Program][Initialize] End building");
|
||||||
return app;
|
return app;
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ using BasicDotnetTemplate.MainProject.Core.Database;
|
|||||||
using BasicDotnetTemplate.MainProject.Models.Api.Common.Exceptions;
|
using BasicDotnetTemplate.MainProject.Models.Api.Common.Exceptions;
|
||||||
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Common;
|
||||||
|
using BasicDotnetTemplate.MainProject.Utils;
|
||||||
|
|
||||||
namespace BasicDotnetTemplate.MainProject.Services;
|
namespace BasicDotnetTemplate.MainProject.Services;
|
||||||
|
|
||||||
@@ -57,6 +59,9 @@ public interface IPermissionService
|
|||||||
bool enabled
|
bool enabled
|
||||||
);
|
);
|
||||||
Task<bool?> DeleteRolePermissionSystemModuleOperationAsync(RolePermissionSystemModuleOperation permission);
|
Task<bool?> DeleteRolePermissionSystemModuleOperationAsync(RolePermissionSystemModuleOperation permission);
|
||||||
|
|
||||||
|
Task<List<string>?> CreatePermissionsOnStartupAsync();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class PermissionService : BaseService, IPermissionService
|
public class PermissionService : BaseService, IPermissionService
|
||||||
@@ -633,5 +638,22 @@ public class PermissionService : BaseService, IPermissionService
|
|||||||
|
|
||||||
#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.AddHttpContextAccessor();
|
||||||
builder.Services.AddScoped<IAuthService, AuthService>();
|
builder.Services.AddScoped<IAuthService, AuthService>();
|
||||||
builder.Services.AddScoped<IJwtService, JwtService>();
|
builder.Services.AddScoped<IJwtService, JwtService>();
|
||||||
|
builder.Services.AddScoped<IPermissionService, PermissionService>();
|
||||||
builder.Services.AddScoped<IRoleService, RoleService>();
|
builder.Services.AddScoped<IRoleService, RoleService>();
|
||||||
builder.Services.AddScoped<IUserService, UserService>();
|
builder.Services.AddScoped<IUserService, UserService>();
|
||||||
Logger.Info("[ProgramUtils][AddScopes] Done scopes");
|
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": {
|
"EncryptionSettings": {
|
||||||
"Salt": "S7VIidfXQf1tOQYX",
|
"Salt": "S7VIidfXQf1tOQYX",
|
||||||
"Pepper": ""
|
"Pepper": ""
|
||||||
|
},
|
||||||
|
"PermissionsSettings": {
|
||||||
|
"FilePath": "Config/permissions.json"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user