Add project files.

This commit is contained in:
Sergio Matias Urquin
2025-04-29 18:55:44 -06:00
commit c34987797a
46 changed files with 3697 additions and 0 deletions

View File

@@ -0,0 +1,220 @@
// ***********************************************************************
// <copyright file="ModuleController.cs">
// Heath
// </copyright>
// ***********************************************************************
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
{
/// <summary>
/// Handles all requests for module authentication.
/// </summary>
[ApiVersion(MimeTypes.ApplicationVersion)]
[Route("api/v{api-version:apiVersion}/[controller]")]
[Produces(MimeTypes.ApplicationJson)]
[Consumes(MimeTypes.ApplicationJson)]
[ApiController]
public class ModuleController(IModuleService service, ILogger<ModuleController> logger) : ControllerBase
{
/// <summary>
/// Gets all the modules.
/// </summary>
/// <returns>The <see cref="IEnumerable{ModuleAdapter}"/> found entities.</returns>
/// <response code="200">The roles found.</response>
/// <response code="404">The roles not found error.</response>
/// <response code="500">The service internal error.</response>
[HttpGet]
[Consumes(MimeTypes.ApplicationJson)]
[Produces(MimeTypes.ApplicationJson)]
[ProducesResponseType(typeof(IEnumerable<ModuleAdapter>), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("ModuleManagement.Read, RoleManagement.Read")]
public async Task<IActionResult> GetAllModulesAsync()
{
try
{
var result = await service.GetAllModulesService();
return Ok(result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in GetAllModulesAsync");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
}
/// <summary>
/// Gets all the modules by module identifiers.
/// </summary>
/// <param name="modules">The list of module identifiers.</param>
/// <returns>The <see cref="IEnumerable{ModuleAdapter}"/> found entities.</returns>
/// <response code="200">The modules found.</response>
/// <response code="404">The modules not found error.</response>
/// <response code="500">The service internal error.</response>
[HttpPost]
[Route(Routes.GetModuleList)]
[Consumes(MimeTypes.ApplicationJson)]
[Produces(MimeTypes.ApplicationJson)]
[ProducesResponseType(typeof(IEnumerable<ModuleAdapter>), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("ModuleManagement.Read")]
public async Task<IActionResult> GetAllModulesByList([FromBody] string[] modules)
{
if (modules == null || !modules.Any())
{
return BadRequest("Module identifiers are required.");
}
try
{
var result = await service.GetAllModulesByListService(modules);
if (result == null || !result.Any())
{
return NotFound("No modules found for the given identifiers.");
}
return Ok(result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in GetAllModulesByList");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
}
/// <summary>
/// Gets the module by identifier.
/// </summary>
/// <param name="id">The module identifier.</param>
/// <returns>The <see cref="ModuleAdapter"/> found entity.</returns>
/// <response code="200">The module found.</response>
/// <response code="404">The module not found error.</response>
/// <response code="500">The service internal error.</response>
[HttpGet]
[Route(Routes.Id)]
[Consumes(MimeTypes.ApplicationJson)]
[Produces(MimeTypes.ApplicationJson)]
[ProducesResponseType(typeof(ModuleAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("ModuleManagement.Read")]
public async Task<IActionResult> GetModuleByIdAsync([FromRoute] string id)
{
try
{
var result = await service.GetModuleByIdService(id);
if (result is null) return NotFound($"module with id: '{id}' not found");
return Ok(result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in GetModuleByIdAsync");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
}
/// <summary>
/// Creates a new module.
/// </summary>
/// <param name="newModule">The module to be added.</param>
/// <returns>The <see cref="ModuleAdapter"/> created entity.</returns>
/// <response code="201">The module created.</response>
/// <response code="422">The module could not be created.</response>
/// <response code="500">The service internal e|ror.</response>
[HttpPost]
[ProducesResponseType(typeof(ModuleAdapter), StatusCodes.Status201Created)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("ModuleManagement.Write")]
public async Task<IActionResult> CreateModuleAsync([FromBody] ModuleRequest newModule)
{
try
{
var result = await service.CreateModuleService(newModule).ConfigureAwait(false);
return Created("CreatedWithIdService", result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in CreateModuleAsync");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
}
/// <summary>
/// Updates a full module by identifier.
/// </summary>
/// <param name="entity">The module to update.</param>
/// <param name="id">The module identifier.</param>
/// <returns>The <see cref="ModuleAdapter"/> updated entity.</returns>
/// <response code="200">The module updated.</response>
/// <response code="404">The module not found.</response>
/// <response code="422">The module could not be updated.</response>
/// <response code="500">The service internal error.</response>
[HttpPut]
[Route(Routes.Id)]
[Consumes(MimeTypes.ApplicationJson)]
[Produces(MimeTypes.ApplicationJson)]
[ProducesResponseType(typeof(ModuleAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("ModuleManagement.Write")]
public async Task<IActionResult> UpdateModuleAsync(ModuleAdapter entity, string id)
{
try
{
var result = await service.UpdateModuleService(entity, id);
return Ok(result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in UpdateModuleAsync");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
}
/// <summary>
/// Changes the status of the module.
/// </summary>
/// <param name="id">The module identifier.</param>
/// <param name="newStatus">The new status of the module.</param>
/// <returns>The <see cref="ModuleAdapter"/> updated entity.</returns>
/// <response code="200">The module updates.</response>
/// <response code="404">The module not found.</response>
/// <response code="422">The module could not be deleted.</response>
/// <response code="500">The service internal error.</response>
[HttpPatch]
[Route(Routes.ChangeStatus)]
[Consumes(MimeTypes.ApplicationJson)]
[Produces(MimeTypes.ApplicationJson)]
[ProducesResponseType(typeof(ModuleAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("ModuleManagement.Write")]
public async Task<IActionResult> ChangeModuleStatus([FromRoute] string id, [FromRoute] StatusEnum newStatus)
{
try
{
var result = await service.ChangeModuleStatusService(id, newStatus);
return Ok(result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in ChangeModuleStatus");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
}
}
}

View File

@@ -0,0 +1,219 @@
// ***********************************************************************
// <copyright file="PermissionController.cs">
// Heath
// </copyright>
// ***********************************************************************
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
{
/// <summary>
/// Handles all requests for permission authentication.
/// </summary>
[ApiVersion(MimeTypes.ApplicationVersion)]
[Route("api/v{api-version:apiVersion}/[controller]")]
[Produces(MimeTypes.ApplicationJson)]
[Consumes(MimeTypes.ApplicationJson)]
[ApiController]
public class PermissionController(IPermissionService service, ILogger<PermissionController> logger) : ControllerBase
{
/// <summary>
/// Gets all the permissions.
/// </summary>
/// <returns>The <see cref="IEnumerable{PermissionAdapter}"/> found entities.</returns>
/// <response code="200">The roles found.</response>
/// <response code="404">The roles not found error.</response>
/// <response code="500">The service internal error.</response>
[HttpGet]
[Consumes(MimeTypes.ApplicationJson)]
[Produces(MimeTypes.ApplicationJson)]
[ProducesResponseType(typeof(IEnumerable<PermissionAdapter>), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("PermissionManagement.Read, RoleManagement.Read")]
public async Task<IActionResult> GetAllPermissionsAsync()
{
try
{
var result = await service.GetAllPermissionsService();
return Ok(result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in GetAllPermissionsAsync");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
}
/// <summary>
/// Gets all the permissions by permission identifiers.
/// </summary>
/// <param name="permissions">The list of permission identifiers.</param>
/// <returns>The <see cref="IEnumerable{PermissionAdapter}"/> found entities.</returns>
/// <response code="200">The permissions found.</response>
/// <response code="404">The permissions not found error.</response>
/// <response code="500">The service internal error.</response>
[HttpPost]
[Route(Routes.GetPermissionList)]
[Consumes(MimeTypes.ApplicationJson)]
[Produces(MimeTypes.ApplicationJson)]
[ProducesResponseType(typeof(IEnumerable<PermissionAdapter>), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("PermissionManagement.Read")]
public async Task<IActionResult> GetAllPermissionsByList([FromBody] string[] permissions)
{
if (permissions == null || !permissions.Any())
{
return BadRequest("Permission identifiers are required.");
}
try
{
var result = await service.GetAllPermissionsByListService(permissions);
if (result == null || !result.Any())
{
return NotFound("No permissions found for the given identifiers.");
}
return Ok(result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in GetAllPermissionsByList");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
}
/// <summary>
/// Gets the permission by identifier.
/// </summary>
/// <param name="id">The permission identifier.</param>
/// <returns>The <see cref="PermissionAdapter"/> found entity.</returns>
/// <response code="200">The permission found.</response>
/// <response code="404">The permission not found error.</response>
/// <response code="500">The service internal error.</response>
[HttpGet]
[Route(Routes.Id)]
[Consumes(MimeTypes.ApplicationJson)]
[Produces(MimeTypes.ApplicationJson)]
[ProducesResponseType(typeof(PermissionAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("PermissionManagement.Read")]
public async Task<IActionResult> GetPermissionByIdAsync([FromRoute] string id)
{
try
{
var result = await service.GetPermissionByIdService(id);
if (result is null) return NotFound($"permission with id: '{id}' not found");
return Ok(result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in GetPermissionByIdAsync");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
}
/// <summary>
/// Creates a new permission.
/// </summary>
/// <param name="newPermission">The permission to be added.</param>
/// <returns>The <see cref="PermissionAdapter"/> created entity.</returns>
/// <response code="201">The permission created.</response>
/// <response code="422">The permission could not be created.</response>
/// <response code="500">The service internal e|ror.</response>
[HttpPost]
[ProducesResponseType(typeof(PermissionAdapter), StatusCodes.Status201Created)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("PermissionManagement.Write")]
public async Task<IActionResult> CreatePermissionAsync([FromBody] PermissionRequest newPermission)
{
try
{
var result = await service.CreatePermissionService(newPermission).ConfigureAwait(false);
return Created("CreatedWithIdService", result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in CreatePermissionAsync");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
}
/// <summary>
/// Updates a full permission by identifier.
/// </summary>
/// <param name="entity">The permission to update.</param>
/// <param name="id">The permission identifier.</param>
/// <returns>The <see cref="PermissionAdapter"/> updated entity.</returns>
/// <response code="200">The permission updated.</response>
/// <response code="404">The permission not found.</response>
/// <response code="422">The permission could not be updated.</response>
/// <response code="500">The service internal error.</response>
[HttpPut]
[Route(Routes.Id)]
[Consumes(MimeTypes.ApplicationJson)]
[Produces(MimeTypes.ApplicationJson)]
[ProducesResponseType(typeof(PermissionAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("PermissionManagement.Write")]
public async Task<IActionResult> UpdatePermissionAsync(PermissionAdapter entity, string id)
{
try
{
var result = await service.UpdatePermissionService(entity, id);
return Ok(result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in UpdatePermissionAsync");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
}
/// <summary>
/// Changes the status of the permission.
/// </summary>
/// <param name="id">The permission identifier.</param>
/// <param name="newStatus">The new status of the permission.</param>
/// <returns>The <see cref="PermissionAdapter"/> updated entity.</returns>
/// <response code="200">The permission updates.</response>
/// <response code="404">The permission not found.</response>
/// <response code="422">The permission could not be deleted.</response>
/// <response code="500">The service internal error.</response>
[HttpPatch]
[Route(Routes.ChangeStatus)]
[Consumes(MimeTypes.ApplicationJson)]
[Produces(MimeTypes.ApplicationJson)]
[ProducesResponseType(typeof(PermissionAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("PermissionManagement.Write")]
public async Task<IActionResult> ChangePermissionStatus([FromRoute] string id, [FromRoute] StatusEnum newStatus)
{
try
{
var result = await service.ChangePermissionStatusService(id, newStatus);
return Ok(result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in ChangePermissionStatus");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
}
}
}

View File

@@ -0,0 +1,231 @@
// ***********************************************************************
// <copyright file="RoleController.cs">
// Heath
// </copyright>
// ***********************************************************************
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
{
/// <summary>
/// Handles all requests for role authentication.
/// </summary>
[ApiVersion(MimeTypes.ApplicationVersion)]
[Route("api/v{api-version:apiVersion}/[controller]")]
[Produces(MimeTypes.ApplicationJson)]
[Consumes(MimeTypes.ApplicationJson)]
[ApiController]
public class RoleController(IRoleService service, ILogger<RoleController> logger) : ControllerBase
{
/// <summary>
/// Gets all the roles.
/// </summary>
/// <returns>The rol found entities.</returns>
/// <response code="200">The roles found.</response>
/// <response code="404">The roles not found error.</response>
/// <response code="500">The service internal error.</response>
[HttpGet]
[ProducesResponseType(typeof(IEnumerable<RoleAdapter>), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("RoleManagement.Read")]
public async Task<IActionResult> 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}");
}
}
/// <summary>
/// Gets the role by identifier.
/// </summary>
/// <param name="id">The role identifier.</param>
/// <returns>The <see cref="RoleAdapter"/> found entity.</returns>
/// <response code="200">The role found.</response>
/// <response code="404">The role not found error.</response>
/// <response code="500">The service internal error.</response>
[HttpGet]
[Route(Routes.Id)]
[ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("RoleManagement.Read")]
public async Task<IActionResult> 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}");
}
}
/// <summary>
/// Creates a new role.
/// </summary>
/// <param name="newRole">The role to be added.</param>
/// <returns>The <see cref="RoleAdapter"/> created entity.</returns>
/// <response code="201">The role created.</response>
/// <response code="422">The role could not be created.</response>
/// <response code="500">The service internal error.</response>
[HttpPost]
[ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status201Created)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("RoleManagement.Write")]
public async Task<IActionResult> 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}");
}
}
/// <summary>
/// Updates a full role by identifier.
/// </summary>
/// <param name="entity">The role to update.</param>
/// <param name="id">The role identifier.</param>
/// <returns>The <see cref="RoleAdapter"/> updated entity.</returns>
/// <response code="200">The role updated.</response>
/// <response code="404">The role not found.</response>
/// <response code="422">The role could not be updated.</response>
/// <response code="500">The service internal error.</response>
[HttpPut]
[Route(Routes.Id)]
[ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("RoleManagement.Write")]
public async Task<IActionResult> 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}");
}
}
/// <summary>
/// Changes the status of the role.
/// </summary>
/// <param name="id">The role identifier.</param>
/// <param name="newStatus">The new status of the role.</param>
/// <returns>The <see cref="RoleAdapter"/> updated entity.</returns>
/// <response code="200">The role updates.</response>
/// <response code="404">The role not found.</response>
/// <response code="422">The role could not be deleted.</response>
/// <response code="500">The service internal error.</response>
[HttpPatch]
[Route(Routes.ChangeStatus)]
[ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("RoleManagement.Write")]
public async Task<IActionResult> 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}");
}
}
/// <summary>
/// Adds an application to the role's list of applications.
/// </summary>
/// <param name="roleId">The identifier of the role to which the application will be added.</param>
/// <param name="application">The application enum value to add.</param>
/// <returns>A <see cref="Task{RoleAdapter}"/> representing the asynchronous operation, with the updated role object.</returns>
/// <response code="200">The role updates.</response>
/// <response code="404">The role not found.</response>
/// <response code="422">The role could not be deleted.</response>
/// <response code="500">The service internal error.</response>
[HttpPost(Routes.AddApplication)]
[ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("RoleManagement.Write")]
public async Task<IActionResult> 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}");
}
}
/// <summary>
/// Removes an application from the role's list of applications.
/// </summary>
/// <param name="roleId">The identifier of the role from which the application will be removed.</param>
/// <param name="application">The application enum value to remove.</param>
/// <returns>A <see cref="Task{RoleAdapter}"/> representing the asynchronous operation, with the updated role object.</returns>
/// <response code="200">The role updates.</response>
/// <response code="404">The role not found.</response>
/// <response code="422">The role could not be deleted.</response>
/// <response code="500">The service internal error.</response>
[HttpDelete(Routes.RemoveApplication)]
[ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("RoleManagement.Write")]
public async Task<IActionResult> 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}");
}
}
}
}

View File

@@ -0,0 +1,495 @@
// ***********************************************************************
// <copyright file="UserController.cs">
// Heath
// </copyright>
// ***********************************************************************
using Asp.Versioning;
using Core.Blueprint.Storage.Adapters;
using Core.Cerberos.Adapters;
using Core.Cerberos.Adapters.Attributes;
using Core.Cerberos.Adapters.Common.Constants;
using Core.Cerberos.Adapters.Common.Enums;
using Core.Cerberos.Provider.Contracts;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Graph;
using UserRequest = Core.Cerberos.Domain.Contexts.Onboarding.Request.UserRequest;
namespace LSA.Core.Kerberos.API.Controllers
{
/// <summary>
/// Handles all requests for user authentication.
/// </summary>
[ApiVersion("1.0")]
[Route("api/v{api-version:apiVersion}/[controller]")]
[Produces(MimeTypes.ApplicationJson)]
[Consumes(MimeTypes.ApplicationJson)]
[ApiController]
public class UserController(IUserService service, ILogger<UserController> logger) : ControllerBase
{
/// <summary>
/// Gets all the users.
/// </summary>
/// <returns>The <see cref="IEnumerable{UserAdapter}"/> found entity.</returns>
/// <response code="200">The users found.</response>
/// <response code="404">The users not found error.</response>
/// <response code="500">The service internal error.</response>
[HttpGet]
[ProducesResponseType(typeof(IEnumerable<UserAdapter>), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("UserManagement.Read")]
public async Task<IActionResult> GetAllUsersService()
{
try
{
var result = await service.GetAllUsersService();
return Ok(result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in GetAllUsersService");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
}
/// <summary>
/// Gets the user by identifier.
/// </summary>
/// <param name="id">The user identifier.</param>
/// <returns>The <see cref="UserAdapter"/> found entity.</returns>
/// <response code="200">The user found.</response>
/// <response code="404">The user not found error.</response>
/// <response code="500">The service internal error.</response>
[HttpGet]
[Route(Routes.Id)]
[ProducesResponseType(typeof(UserAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("UserManagement.Read")]
public async Task<IActionResult> GetUserByIdService([FromRoute] string id)
{
try
{
var result = await service.GetUserByIdService(id);
if (result is null) return NotFound($"user with id: '{id}' not found");
return Ok(result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in GetUserByIdService");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
}
/// <summary>
/// Gets the user by email.
/// </summary>
/// <param name="email">The user's email.</param>
/// <returns>The <see cref="UserAdapter"/> found entity.</returns>
/// <response code="200">The user found.</response>
/// <response code="404">The user not found error.</response>
/// <response code="500">The service internal error.</response>
[HttpGet]
[Route(Routes.Email)]
[ProducesResponseType(typeof(UserAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = $"{Schemes.HeathScheme}, {Schemes.AzureScheme}")]
public async Task<IActionResult> GetUserByEmail([FromRoute] string email)
{
try
{
var result = await service.GetUserByEmailService(email);
if (result is null) return NotFound($"user with email: '{email}' not found");
return Ok(result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in GetUserByIdEmail");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
}
/// <summary>
/// Validates if a user exists on the database.
/// </summary>
/// <param name="email">The user's email.</param>
/// <returns>The <see cref="UserExistenceAdapter"/> found entity.</returns>
/// <response code="200">The user found.</response>
/// <response code="404">The user not found error.</response>
/// <response code="500">The service internal error.</response>
[HttpGet]
[Route("{email}/ValidateExistence")]
[ProducesResponseType(typeof(UserExistenceAdapter), StatusCodes.Status200OK)]
[AllowAnonymous]
public async Task<IActionResult> ValidateUserExistence([FromRoute] string email)
{
try
{
var result = await service.ValidateUserExistenceService(email);
var existence = new UserExistenceAdapter
{
Existence = (result is not null) ? true : false
};
return Ok(existence);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in ValidateUserExistance");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
}
/// <summary>
/// Creates a new user.
/// </summary>
/// <param name="newUser">The user to be added.</param>
/// <param name="sendInvitation">Sends an invitation in case of third party access.</param>
/// <returns>The <see cref="UserAdapter"/> created entity.</returns>
/// <response code="201">The user created.</response>
/// <response code="422">The user could not be created.</response>
/// <response code="500">The service internal error.</response>
[HttpPost(Routes.Register)]
[ProducesResponseType(typeof(UserAdapter), StatusCodes.Status201Created)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("UserManagement.Write")]
public async Task<IActionResult> CreateUserAsync([FromBody] UserRequest newUser, [FromRoute] bool sendInvitation)
{
try
{
var user = await service.GetUserByEmailService(newUser.Email).ConfigureAwait(false);
if (user is not null)
return UnprocessableEntity("There is a user with the same email registered in the database");
var result = await service.CreateUserService(newUser).ConfigureAwait(false);
return Created("CreatedWithIdService", result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in CreateUserAsync");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
}
/// <summary>
/// Updates a full user by identifier.
/// </summary>
/// <param name="entity">The user to update.</param>
/// <param name="id">The user identifier.</param>
/// <returns>The <see cref="UserAdapter"/> updated entity.</returns>
/// <response code="200">The user updated.</response>
/// <response code="404">The user not found.</response>
/// <response code="422">The user could not be updated.</response>
/// <response code="500">The service internal error.</response>
[HttpPut]
[Route(Routes.Id)]
[ProducesResponseType(typeof(UserAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("UserManagement.Write")]
public async Task<IActionResult> UpdateUserAsync([FromBody] UserAdapter entity, [FromRoute] string id)
{
try
{
var result = await service.UpdateUserService(entity, id);
return Ok(result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in UpdateUserAsync");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
}
/// <summary>
/// Logs in the user.
/// </summary>
/// <param name="email">The User's email.</param>
/// <returns>A <see cref="UserAdapter"/> representing
/// the asynchronous execution of the service.</returns>
/// <response code="200">The User found.</response>
/// <response code="404">The User not found.</response>
/// <response code="500">The service internal error.</response>
[HttpPatch(Routes.LogIn)]
[ProducesResponseType(typeof(UserAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = $"{Schemes.HeathScheme}, {Schemes.AzureScheme}")]
public async Task<IActionResult> LoginUserAsync([FromRoute] string email)
{
try
{
var result = await service.LogInUserService(email).ConfigureAwait(false);
if (result is null)
return new NotFoundObjectResult($"The user with email: '{email}' was not found");
return Ok(result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in LogInUserService");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
}
/// <summary>
/// Logs out the user.
/// </summary>
/// <param name="email">The User's email.</param>
/// <returns>A <see cref="UserAdapter"/> representing
/// the asynchronous execution of the service.</returns>
/// <response code="200">The User updated.</response>
/// <response code="500">The service internal error.</response>
[HttpPatch(Routes.LogOut)]
[ProducesResponseType(typeof(UserAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = $"{Schemes.HeathScheme}, {Schemes.AzureScheme}")]
public async Task<IActionResult> LogOutUserSessionAsync([FromRoute] string email)
{
try
{
var result = await service.LogOutUserSessionService(email).ConfigureAwait(false);
return Ok(result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in LogOutUserSessionService");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
}
/// <summary>
/// Changes the status of the user.
/// </summary>
/// <param name="id">The user identifier.</param>
/// <param name="newStatus">The new status of the user.</param>
/// <returns>The <see cref="UserAdapter"/> updated entity.</returns>
/// <response code="200">The user updates.</response>
/// <response code="404">The user not found.</response>
/// <response code="422">The user could not be deleted.</response>
/// <response code="500">The service internal error.</response>
[HttpPatch]
[Route(Routes.ChangeStatus)]
[ProducesResponseType(typeof(UserAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("UserManagement.Write")]
public async Task<IActionResult> ChangeUserStatus([FromRoute] string id, [FromRoute] StatusEnum newStatus)
{
try
{
var result = await service.ChangeUserStatusService(id, newStatus);
return Ok(result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in ChangeUserStatus");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
}
/// <summary>
/// Adds a company to the user's list of companies.
/// </summary>
/// <param name="userId">The user identifier.</param>
/// <param name="companyId">The company identifier to add.</param>
/// <returns>The updated <see cref="UserAdapter"/> entity.</returns>
/// <response code="200">The user with the updated companies.</response>
/// <response code="404">The user or company not found.</response>
/// <response code="500">The service internal error.</response>
[HttpPost]
[Route(Routes.AddCompany)]
[ProducesResponseType(typeof(UserAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("UserManagement.Write")]
public async Task<IActionResult> AddCompanyToUserAsync([FromRoute] string userId, [FromRoute] string companyId)
{
try
{
var result = await service.AddCompanyToUserService(userId, companyId);
return Ok(result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in AddCompanyToUserAsync");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
}
/// <summary>
/// Removes a company from the user's list of companies.
/// </summary>
/// <param name="userId">The user identifier.</param>
/// <param name="companyId">The company identifier to remove.</param>
/// <returns>The updated <see cref="UserAdapter"/> entity.</returns>
/// <response code="200">The user with the updated companies.</response>
/// <response code="404">The user or company not found.</response>
/// <response code="500">The service internal error.</response>
[HttpDelete]
[Route(Routes.RemoveCompany)]
[ProducesResponseType(typeof(UserAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("UserManagement.Write")]
public async Task<IActionResult> RemoveCompanyFromUserAsync([FromRoute] string userId, [FromRoute] string companyId)
{
try
{
var result = await service.RemoveCompanyFromUserService(userId, companyId);
return Ok(result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in RemoveCompanyFromUserAsync");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
}
/// <summary>
/// Adds a project to the user's list of projects.
/// </summary>
/// <param name="userId">The user identifier.</param>
/// <param name="projectId">The project identifier to add.</param>
/// <returns>The updated <see cref="UserAdapter"/> entity.</returns>
/// <response code="200">The user with the updated projects.</response>
/// <response code="404">The user or project not found.</response>
/// <response code="500">The service internal error.</response>
[HttpPost]
[Route(Routes.AddProject)]
[ProducesResponseType(typeof(UserAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("UserManagement.Write")]
public async Task<IActionResult> AddProjectToUserAsync([FromRoute] string userId, [FromRoute] string projectId)
{
try
{
var result = await service.AddProjectToUserService(userId, projectId);
return Ok(result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in AddProjectToUserAsync");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
}
/// <summary>
/// Removes a project from the user's list of projects.
/// </summary>
/// <param name="userId">The user identifier.</param>
/// <param name="projectId">The project identifier to remove.</param>
/// <returns>The updated <see cref="UserAdapter"/> entity.</returns>
/// <response code="200">The user with the updated projects.</response>
/// <response code="404">The user or project not found.</response>
/// <response code="500">The service internal error.</response>
[HttpDelete]
[Route(Routes.RemoveProject)]
[ProducesResponseType(typeof(UserAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("UserManagement.Write")]
public async Task<IActionResult> RemoveProjectFromUserAsync([FromRoute] string userId, [FromRoute] string projectId)
{
try
{
var result = await service.RemoveProjectFromUserService(userId, projectId);
return Ok(result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in RemoveProjectFromUserAsync");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
}
/// <summary>
/// Gets a token for the user, including roles, permissions, and modules.
/// </summary>
/// <param name="email">The user's email.</param>
/// <returns>The token adapter with user details, role, permissions, and modules.</returns>
/// <response code="200">The token adapter with user details.</response>
/// <response code="404">The user not found.</response>
/// <response code="500">The service internal error.</response>
[HttpGet]
[Route("{email}/GetTokenAdapter")]
[ProducesResponseType(typeof(TokenAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = $"{Schemes.HeathScheme}, {Schemes.AzureScheme}")]
public async Task<IActionResult> GetTokenAdapter([FromRoute] string email)
{
try
{
var tokenAdapter = await service.GetTokenAdapter(email);
if (tokenAdapter == null) return NotFound($"User with email: {email} not found");
return Ok(tokenAdapter);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in GetTokenAdapter");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
}
/// <summary>
/// Get Consent Form PDF.
/// </summary>
/// <returns>The <see cref="BlobDownloadUriAdapter"/> found pdf.</returns>
/// <response code="200">The pdf found.</response>
/// <response code="404">The pdf not found error.</response>
/// <response code="500">The service internal error.</response>
[HttpGet]
[Route("GetConsentFormPDF")]
[ProducesResponseType(typeof(BlobDownloadUriAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = Schemes.HeathScheme)]
[Permission("UserManagement.Read")]
public async Task<IActionResult> GetConsentFormPDFService()
{
try
{
var result = await service.GetConsentFormPDFService();
return Ok(result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in GetConsentFormPDFService");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
}
/// <summary>
/// Accept user consent form.
/// </summary>
/// <returns>A <see cref="UserAdapter"/> representing
/// the asynchronous execution of the service.</returns>
/// <response code="200">The User found.</response>
/// <response code="404">The User not found.</response>
/// <response code="500">The service internal error.</response>
[HttpPatch("AcceptUserConsentForm")]
[ProducesResponseType(typeof(UserAdapter), StatusCodes.Status200OK)]
[Authorize(AuthenticationSchemes = $"{Schemes.HeathScheme}, {Schemes.AzureScheme}")]
public async Task<IActionResult> AcceptUserConsentFormAsync()
{
try
{
var result = await service.AcceptUserConsentFormService().ConfigureAwait(false);
return Ok(result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error in AcceptUserConsentFormService");
return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}");
}
}
}
}