Remove role management from AuthHex; update documentation and user handling to reflect ERP ownership of roles.
CI/CD - erp auth / Build & Deploy (pull_request) Failing after 15m9s
CI/CD - erp auth / Build & Deploy (pull_request) Failing after 15m9s
This commit is contained in:
@@ -377,7 +377,6 @@ public class AltOptionManagerService
|
||||
user.EmailVerified,
|
||||
user.MobileNumberVerified,
|
||||
user.IsMfaEnabled,
|
||||
RoleId = user.RoleId,
|
||||
UserTypeId = user.UserTypeId
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,72 +0,0 @@
|
||||
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,151 +0,0 @@
|
||||
using System.Text.Json;
|
||||
using AuthHex.Infra.UoW;
|
||||
using AuthHex.Interfaces;
|
||||
using AuthHex.Models;
|
||||
using Core_Archi.Helpers;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace AuthHex.Services.RoleManager;
|
||||
|
||||
public class RoleManagerService
|
||||
{
|
||||
private readonly IRoleManageRepository _repository;
|
||||
private readonly IUnitOfWork _uow;
|
||||
|
||||
public RoleManagerService(IRoleManageRepository repository, IUnitOfWork uow)
|
||||
{
|
||||
_repository = repository;
|
||||
_uow = uow;
|
||||
}
|
||||
|
||||
public async Task<object> CreateRole(object payload, HttpContext httpContext)
|
||||
{
|
||||
await _uow.BeginAsync();
|
||||
try
|
||||
{
|
||||
var data = DeserializePayload.Deserialize(payload);
|
||||
|
||||
var code = data.ContainsKey("code") ? data["code"].GetString() : null;
|
||||
var name = data.ContainsKey("name") ? data["name"].GetString() : null;
|
||||
var isSystemRole = data.ContainsKey("isSystemRole")
|
||||
&& data["isSystemRole"].ValueKind != JsonValueKind.Null
|
||||
&& data["isSystemRole"].GetBoolean();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(code))
|
||||
throw new ArgumentException("code is required");
|
||||
|
||||
var role = new Role
|
||||
{
|
||||
RoleId = Guid.NewGuid(),
|
||||
Code = code,
|
||||
Name = string.IsNullOrWhiteSpace(name) ? code : name,
|
||||
IsSystemRole = isSystemRole,
|
||||
CreatedAt = DateTime.UtcNow
|
||||
};
|
||||
|
||||
await _repository.AddRoleAsync(role);
|
||||
await _uow.CommitAsync();
|
||||
|
||||
return ToDto(role);
|
||||
}
|
||||
catch
|
||||
{
|
||||
await _uow.RollbackAsync();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<object> GetRole(object payload, HttpContext httpContext)
|
||||
{
|
||||
var data = DeserializePayload.Deserialize(payload);
|
||||
|
||||
if (!data.ContainsKey("roleId") || !Guid.TryParse(data["roleId"].GetString(), out var roleId))
|
||||
throw new ArgumentException("Invalid roleId");
|
||||
|
||||
var role = await _repository.GetRoleByIdAsync(roleId)
|
||||
?? throw new KeyNotFoundException("Role not found");
|
||||
|
||||
return ToDto(role);
|
||||
}
|
||||
|
||||
public async Task<object> ListRoles(HttpContext httpContext)
|
||||
{
|
||||
var roles = await _repository.ListRolesAsync();
|
||||
return roles.Select(ToDto).ToList();
|
||||
}
|
||||
|
||||
public async Task<object> UpdateRole(object payload, HttpContext httpContext)
|
||||
{
|
||||
await _uow.BeginAsync();
|
||||
try
|
||||
{
|
||||
var data = DeserializePayload.Deserialize(payload);
|
||||
|
||||
if (!data.ContainsKey("roleId") || !Guid.TryParse(data["roleId"].GetString(), out var roleId))
|
||||
throw new ArgumentException("Invalid roleId");
|
||||
|
||||
var role = await _repository.GetRoleByIdAsync(roleId)
|
||||
?? throw new KeyNotFoundException("Role not found");
|
||||
|
||||
if (data.ContainsKey("code"))
|
||||
{
|
||||
var code = data["code"].GetString();
|
||||
if (string.IsNullOrWhiteSpace(code))
|
||||
throw new ArgumentException("code cannot be empty");
|
||||
role.Code = code;
|
||||
}
|
||||
|
||||
if (data.ContainsKey("name"))
|
||||
role.Name = data["name"].GetString();
|
||||
|
||||
if (data.ContainsKey("isSystemRole") && data["isSystemRole"].ValueKind != JsonValueKind.Null)
|
||||
role.IsSystemRole = data["isSystemRole"].GetBoolean();
|
||||
|
||||
await _uow.CommitAsync();
|
||||
|
||||
return ToDto(role);
|
||||
}
|
||||
catch
|
||||
{
|
||||
await _uow.RollbackAsync();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<object> DeleteRole(object payload, HttpContext httpContext)
|
||||
{
|
||||
await _uow.BeginAsync();
|
||||
try
|
||||
{
|
||||
var data = DeserializePayload.Deserialize(payload);
|
||||
|
||||
if (!data.ContainsKey("roleId") || !Guid.TryParse(data["roleId"].GetString(), out var roleId))
|
||||
throw new ArgumentException("Invalid roleId");
|
||||
|
||||
var role = await _repository.GetRoleByIdAsync(roleId)
|
||||
?? throw new KeyNotFoundException("Role not found");
|
||||
|
||||
if (await _repository.RoleInUseAsync(roleId))
|
||||
throw new InvalidOperationException("ROLE_IN_USE: role is assigned to one or more users");
|
||||
|
||||
await _repository.DeleteRoleAsync(role);
|
||||
await _uow.CommitAsync();
|
||||
|
||||
return new { message = "Role deleted successfully" };
|
||||
}
|
||||
catch
|
||||
{
|
||||
await _uow.RollbackAsync();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private static object ToDto(Role role) => new
|
||||
{
|
||||
role.RoleId,
|
||||
role.Code,
|
||||
role.Name,
|
||||
role.IsSystemRole,
|
||||
role.CreatedAt
|
||||
};
|
||||
}
|
||||
@@ -65,8 +65,7 @@ public class UserManagerService
|
||||
|
||||
var deviceName = data.ContainsKey("deviceName") ? data["deviceName"].GetString() : "Unknown Device";
|
||||
|
||||
if (!data.ContainsKey("roleId") || !Guid.TryParse(data["roleId"].GetString(), out var RoleId))
|
||||
throw new ArgumentException("Invalid roleId");
|
||||
// A "roleId" in the payload (old ERP builds still send one) is accepted and ignored: roles are the ERP's business.
|
||||
|
||||
if (!data.ContainsKey("userTypeId") || !Guid.TryParse(data["userTypeId"].GetString(), out var UserTypeId))
|
||||
throw new ArgumentException("Invalid userTypeId");
|
||||
@@ -80,7 +79,6 @@ public class UserManagerService
|
||||
Email = Email,
|
||||
MobileNumber = MobileNumber,
|
||||
Nic = Nic,
|
||||
RoleId = RoleId,
|
||||
UserTypeId = UserTypeId
|
||||
};
|
||||
bool canSave = await CanRegisterUser(userToCheck);
|
||||
@@ -110,7 +108,6 @@ public class UserManagerService
|
||||
Nic = Nic,
|
||||
Email = Email,
|
||||
MobileNumber = MobileNumber,
|
||||
RoleId = RoleId,
|
||||
UserTypeId = UserTypeId,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
IsActive = true,
|
||||
@@ -194,7 +191,6 @@ public class UserManagerService
|
||||
user.EmailVerified,
|
||||
user.MobileNumberVerified,
|
||||
user.IsMfaEnabled,
|
||||
RoleId = user.RoleId,
|
||||
UserTypeId = user.UserTypeId
|
||||
}
|
||||
};
|
||||
@@ -303,7 +299,6 @@ public class UserManagerService
|
||||
user.EmailVerified,
|
||||
user.MobileNumberVerified,
|
||||
user.IsMfaEnabled,
|
||||
RoleId = user.RoleId,
|
||||
UserTypeId = user.UserTypeId
|
||||
}
|
||||
};
|
||||
@@ -412,7 +407,6 @@ public class UserManagerService
|
||||
user.EmailVerified,
|
||||
user.MobileNumberVerified,
|
||||
user.IsMfaEnabled,
|
||||
RoleId = user.RoleId,
|
||||
UserTypeId = user.UserTypeId
|
||||
}
|
||||
}
|
||||
@@ -505,7 +499,6 @@ public class UserManagerService
|
||||
user.EmailVerified,
|
||||
user.MobileNumberVerified,
|
||||
user.IsMfaEnabled,
|
||||
RoleId = user.RoleId,
|
||||
UserTypeId = user.UserTypeId
|
||||
}
|
||||
};
|
||||
@@ -552,12 +545,6 @@ public class UserManagerService
|
||||
user.IsActive,
|
||||
user.IsLocked,
|
||||
user.CreatedAt,
|
||||
Role = new
|
||||
{
|
||||
user.Role?.RoleId,
|
||||
user.Role?.Code,
|
||||
user.Role?.Name
|
||||
},
|
||||
UserType = new
|
||||
{
|
||||
user.UserType?.UserTypeId,
|
||||
@@ -1215,9 +1202,6 @@ public class UserManagerService
|
||||
if (user.UserTypeId == userToCheck.UserTypeId)
|
||||
return false;
|
||||
|
||||
if (user.RoleId == userToCheck.RoleId)
|
||||
return false;
|
||||
|
||||
if (user.IsLocked == false)
|
||||
return false;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user