// ***********************************************************************
//
// AgileWebs
//
// ***********************************************************************
using Asp.Versioning;
using Core.Cerberos.Adapters;
using Core.Cerberos.Adapters.Attributes;
using Core.Cerberos.Adapters.Common.Constants;
using Core.Cerberos.Adapters.Common.Enums;
using Core.Cerberos.Domain.Contexts.Onboarding.Request;
using Core.Cerberos.Provider.Contracts;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace LSA.Core.Kerberos.API.Controllers
{
///
/// Handles all requests for role authentication.
///
[ApiVersion(MimeTypes.ApplicationVersion)]
[Route("api/v{api-version:apiVersion}/[controller]")]
[Produces(MimeTypes.ApplicationJson)]
[Consumes(MimeTypes.ApplicationJson)]
[ApiController]
public class RoleController(IRoleService service, ILogger logger) : ControllerBase
{
///
/// Gets all the roles.
///
/// The rol found entities.
/// The roles found.
/// The roles not found error.
/// The service internal error.
[HttpGet]
[ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("RoleManagement.Read")]
public async Task GetAllRolesAsync()
{
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}");
}
}
///
/// Gets the role by identifier.
///
/// The role identifier.
/// The found entity.
/// The role found.
/// The role not found error.
/// The service internal error.
[HttpGet]
[Route(Routes.Id)]
[ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("RoleManagement.Read")]
public async Task GetRoleByIdAsync([FromRoute] string id)
{
try
{
var result = await service.GetRoleByIdService(id);
if (result is null) return NotFound($"role with id: '{id}' not found");
return Ok(result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in GetRoleByIdAsync");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
}
///
/// Creates a new role.
///
/// The role to be added.
/// The created entity.
/// The role created.
/// The role could not be created.
/// The service internal error.
[HttpPost]
[ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status201Created)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("RoleManagement.Write")]
public async Task CreateRoleAsync([FromBody] RoleRequest newRole)
{
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}");
}
}
///
/// Updates a full role by identifier.
///
/// The role to update.
/// The role identifier.
/// The updated entity.
/// The role updated.
/// The role not found.
/// The role could not be updated.
/// The service internal error.
[HttpPut]
[Route(Routes.Id)]
[ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("RoleManagement.Write")]
public async Task UpdateRoleAsync([FromBody] RoleAdapter entity, [FromRoute] string id)
{
try
{
var result = await service.UpdateRoleService(entity, id);
return Ok(result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in UpdateRoleAsync");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
}
///
/// Changes the status of the role.
///
/// The role identifier.
/// The new status of the role.
/// The updated entity.
/// The role updates.
/// The role not found.
/// The role could not be deleted.
/// The service internal error.
[HttpPatch]
[Route(Routes.ChangeStatus)]
[ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("RoleManagement.Write")]
public async Task ChangeRoleStatus([FromRoute] string id, [FromRoute] StatusEnum newStatus)
{
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}");
}
}
///
/// Adds an application to the role's list of applications.
///
/// The identifier of the role to which the application will be added.
/// The application enum value to add.
/// A representing the asynchronous operation, with the updated role object.
/// The role updates.
/// The role not found.
/// The role could not be deleted.
/// The service internal error.
[HttpPost(Routes.AddApplication)]
[ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("RoleManagement.Write")]
public async Task AddApplicationToRoleAsync([FromRoute] string roleId,
[FromRoute] ApplicationsEnum application)
{
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}");
}
}
///
/// Removes an application from the role's list of applications.
///
/// The identifier of the role from which the application will be removed.
/// The application enum value to remove.
/// A representing the asynchronous operation, with the updated role object.
/// The role updates.
/// The role not found.
/// The role could not be deleted.
/// The service internal error.
[HttpDelete(Routes.RemoveApplication)]
[ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("RoleManagement.Write")]
public async Task RemoveApplicationFromRoleAsync([FromRoute] string roleId,
[FromRoute] ApplicationsEnum application)
{
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}");
}
}
}
}