Use Blueprint.Mongo package in Role and Permission services

This commit is contained in:
Oscar Morales
2025-05-21 12:45:21 -06:00
parent c18c85959c
commit 1c38008e97
10 changed files with 403 additions and 666 deletions

View File

@@ -12,6 +12,7 @@ using Core.Thalos.Domain.Contexts.Onboarding.Request;
using Core.Thalos.Provider.Contracts;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using StatusEnum = Core.Blueprint.Mongo.StatusEnum;
namespace LSA.Core.Kerberos.API.Controllers
{
@@ -23,7 +24,7 @@ namespace LSA.Core.Kerberos.API.Controllers
[Produces(MimeTypes.ApplicationJson)]
[Consumes(MimeTypes.ApplicationJson)]
[ApiController]
public class RoleController(IRoleService service, ILogger<RoleController> logger) : ControllerBase
public class RoleController(IRoleProvider service) : ControllerBase
{
/// <summary>
/// Gets all the roles.
@@ -36,19 +37,10 @@ namespace LSA.Core.Kerberos.API.Controllers
[ProducesResponseType(typeof(IEnumerable<RoleAdapter>), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.DefaultScheme)]
[Permission("RoleManagement.Read")]
public async Task<IActionResult> GetAllRolesAsync()
public async Task<IActionResult> GetAllRolesAsync(CancellationToken cancellationToken)
{
try
{
var result = await service.GetAllRolesService();
return Ok(result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in GetAllRolesAsync");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
var result = await service.GetAllRoles(cancellationToken).ConfigureAwait(false);
return Ok(result);
}
/// <summary>
@@ -64,21 +56,16 @@ namespace LSA.Core.Kerberos.API.Controllers
[ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.DefaultScheme)]
[Permission("RoleManagement.Read")]
public async Task<IActionResult> GetRoleByIdAsync([FromRoute] string id)
public async Task<IActionResult> GetRoleByIdAsync([FromRoute] string _id, CancellationToken cancellationToken)
{
try
{
var result = await service.GetRoleByIdService(id);
var result = await service.GetRoleById(_id, cancellationToken).ConfigureAwait(false);
if (result is null) return NotFound($"role with id: '{id}' not found");
return Ok(result);
}
catch (Exception ex)
if (result == null)
{
logger.LogError(ex, "Error in GetRoleByIdAsync");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
return NotFound("Entity not found");
}
return Ok(result);
}
/// <summary>
@@ -93,19 +80,10 @@ namespace LSA.Core.Kerberos.API.Controllers
[ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status201Created)]
[Authorize(AuthenticationSchemes = Schemes.DefaultScheme)]
[Permission("RoleManagement.Write")]
public async Task<IActionResult> CreateRoleAsync([FromBody] RoleRequest newRole)
public async Task<IActionResult> CreateRoleAsync([FromBody] RoleRequest newRole, CancellationToken cancellationToken)
{
try
{
var result = await service.CreateRoleService(newRole).ConfigureAwait(false);
return Created("CreatedWithIdService", result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in CreateRoleAsync");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
var result = await service.CreateRole(newRole, cancellationToken).ConfigureAwait(false);
return Created("CreatedWithIdAsync", result);
}
/// <summary>
@@ -123,19 +101,16 @@ namespace LSA.Core.Kerberos.API.Controllers
[ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.DefaultScheme)]
[Permission("RoleManagement.Write")]
public async Task<IActionResult> UpdateRoleAsync([FromBody] RoleAdapter entity, [FromRoute] string id)
public async Task<IActionResult> UpdateRoleAsync([FromRoute] string _id, [FromBody] RoleAdapter entity, CancellationToken cancellationToken)
{
try
if (_id != entity._Id?.ToString())
{
var result = await service.UpdateRoleService(entity, id);
return BadRequest("Role ID mismatch");
}
return Ok(result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in UpdateRoleAsync");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
var result = await service.UpdateRole(entity, cancellationToken).ConfigureAwait(false);
return Ok(result);
}
/// <summary>
@@ -153,20 +128,10 @@ namespace LSA.Core.Kerberos.API.Controllers
[ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.DefaultScheme)]
[Permission("RoleManagement.Write")]
public async Task<IActionResult> ChangeRoleStatus([FromRoute] string id, [FromRoute] StatusEnum newStatus)
public async Task<IActionResult> ChangeRoleStatus([FromRoute] string id, [FromRoute] StatusEnum newStatus, CancellationToken cancellationToken)
{
try
{
var result = await service.ChangeRoleStatusService(id, newStatus);
return Ok(result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in ChangeRoleStatus");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
var result = await service.ChangeRoleStatus(id, newStatus, cancellationToken).ConfigureAwait(false);
return Ok(result);
}
/// <summary>
@@ -183,20 +148,10 @@ namespace LSA.Core.Kerberos.API.Controllers
[ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.DefaultScheme)]
[Permission("RoleManagement.Write")]
public async Task<IActionResult> AddApplicationToRoleAsync([FromRoute] string roleId,
[FromRoute] ApplicationsEnum application)
public async Task<IActionResult> AddApplicationToRoleAsync([FromRoute] string roleId, [FromRoute] ApplicationsEnum application, CancellationToken cancellationToken)
{
try
{
var updatedRole = await service.AddApplicationToRoleService(roleId, application);
return Ok(updatedRole);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in AddApplicationToRoleAsync");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
var result = await service.AddApplicationToRole(roleId, application, cancellationToken).ConfigureAwait(false);
return Ok(result);
}
/// <summary>
@@ -213,19 +168,10 @@ namespace LSA.Core.Kerberos.API.Controllers
[ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.DefaultScheme)]
[Permission("RoleManagement.Write")]
public async Task<IActionResult> RemoveApplicationFromRoleAsync([FromRoute] string roleId,
[FromRoute] ApplicationsEnum application)
public async Task<IActionResult> RemoveApplicationFromRoleAsync([FromRoute] string roleId, [FromRoute] ApplicationsEnum application, CancellationToken cancellationToken)
{
try
{
var updatedRole = await service.RemoveApplicationFromRoleService(roleId, application);
return Ok(updatedRole);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in RemoveApplicationFromRoleAsync");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
var result = await service.RemoveApplicationFromRole(roleId, application, cancellationToken).ConfigureAwait(false);
return Ok(result);
}
}
}