Decouple UserType, add Role entity and management

Removed all UserType logic and references, replacing them with a new Role model and management system. Introduced RoleManager service, repository, and API endpoint for role CRUD operations. Updated user flows and JWT claims to use RoleCode. Removed admin-only user CRUD endpoints. Updated DI, documentation, and error handling for consistency. Appsettings and DTOs now reflect the new role-based approach.
This commit is contained in:
Dhananjaya99
2026-10-19 12:57:07 +05:30
parent fbd079d337
commit 15832d1ae3
19 changed files with 393 additions and 297 deletions
+72
View File
@@ -0,0 +1,72 @@
using AuthHex.Models.IOs;
using AuthHex.Services.RoleManager;
namespace AuthHex.Services.FunctionHandler
{
public class RoleManager
{
public readonly Dictionary<string, Func<object, HttpContext, Task<object>>> _functionHandler;
public RoleManager(RoleManagerService roleManagerService)
{
_functionHandler = new()
{
{ "createRole", roleManagerService.CreateRole },
{ "getRole", roleManagerService.GetRole },
{ "listRoles", async (payload, context) => await roleManagerService.ListRoles(context) },
{ "updateRole", roleManagerService.UpdateRole },
{ "deleteRole", roleManagerService.DeleteRole }
};
}
public async Task<ApiResponse> Execute(ApiRequest request, HttpContext httpContext)
{
if (!_functionHandler.TryGetValue(request.FunctionName, out var handler))
{
return new ApiResponse
{
StatusCode = 404,
Message = $"Function '{request.FunctionName}' not found.",
Data = null
};
}
try
{
var results = await handler(request.Payload, httpContext);
return new ApiResponse
{
StatusCode = 200,
Message = "success",
Data = results
};
}
catch (KeyNotFoundException ex)
{
return new ApiResponse
{
StatusCode = 400,
Message = ex.Message,
Data = null
};
}
catch (InvalidOperationException ex)
{
return new ApiResponse
{
StatusCode = 400,
Message = ex.Message,
Data = null
};
}
catch (ArgumentException ex)
{
return new ApiResponse
{
StatusCode = 500,
Message = ex.Message,
Data = null
};
}
}
}
}
+1 -16
View File
@@ -18,19 +18,13 @@ namespace AuthHex.Services.FunctionHandler
{ "refreshToken", UserManagerService.RefreshToken },
{ "getUserDetails", UserManagerService.GetUserDetails },
{ "getUserSessions", async (payload, context) => await UserManagerService.GetUserSessions(context) },
{ "listUserTypes", async (payload, context) => await UserManagerService.ListUserTypes(context) },
{ "ChangeUserStatus", UserManagerService.ChangeUserStatus},
{ "ChangeUserStatus", UserManagerService.ChangeUserStatus},
{"LockUserAccount" , UserManagerService.LockUserAccount },
{"ChangeUserPassword" , UserManagerService.ChangeUserPassword },
{"VerifyPassword" , UserManagerService.VerifyPassword },
{"LogoutUser" , UserManagerService.LogoutUser},
{"UpdateUser", UserManagerService.UpdateUser},
{"VerifyOtpForLogin",UserManagerService.VerifyOtpForLogin},
// Admin-only — require X-Admin-Api-Key, act on an arbitrary target user.
{"AdminUpdateUser", UserManagerService.AdminUpdateUser},
{"AdminSetUserStatus", UserManagerService.AdminSetUserStatus},
{"deleteUser", UserManagerService.DeleteUser},
// 2FA Management Functions
{ "initiateTwoFASetup", async (payload, context) => await UserManagerService.InitiateTwoFASetup(context) },
@@ -62,15 +56,6 @@ namespace AuthHex.Services.FunctionHandler
Data = results
};
}
catch (UnauthorizedAccessException ex)
{
return new ApiResponse
{
StatusCode = 401,
Message = ex.Message,
Data = null
};
}
catch (KeyNotFoundException ex)
{
return new ApiResponse