Use Blueprint.Mongo package in Role and Permission services
This commit is contained in:
		| @@ -139,7 +139,7 @@ namespace LSA.Core.Kerberos.API.Controllers | |||||||
|         { |         { | ||||||
|             if (_id != entity._Id?.ToString()) |             if (_id != entity._Id?.ToString()) | ||||||
|             { |             { | ||||||
|                 return BadRequest("User ID mismatch"); |                 return BadRequest("Module ID mismatch"); | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             var result = await service.UpdateModule(entity, cancellationToken).ConfigureAwait(false); |             var result = await service.UpdateModule(entity, cancellationToken).ConfigureAwait(false); | ||||||
|   | |||||||
| @@ -5,14 +5,15 @@ | |||||||
| // *********************************************************************** | // *********************************************************************** | ||||||
|  |  | ||||||
| using Asp.Versioning; | using Asp.Versioning; | ||||||
|  | using Core.Blueprint.Mongo; | ||||||
| using Core.Thalos.Adapters; | using Core.Thalos.Adapters; | ||||||
| using Core.Thalos.Adapters.Attributes; | using Core.Thalos.Adapters.Attributes; | ||||||
| using Core.Thalos.Adapters.Common.Constants; | using Core.Thalos.Adapters.Common.Constants; | ||||||
| using Core.Thalos.Adapters.Common.Enums; |  | ||||||
| using Core.Thalos.Domain.Contexts.Onboarding.Request; |  | ||||||
| using Core.Thalos.Provider.Contracts; | using Core.Thalos.Provider.Contracts; | ||||||
| using Microsoft.AspNetCore.Authorization; | using Microsoft.AspNetCore.Authorization; | ||||||
| using Microsoft.AspNetCore.Mvc; | using Microsoft.AspNetCore.Mvc; | ||||||
|  | using Microsoft.Graph; | ||||||
|  | using PermissionRequest = Core.Thalos.Domain.Contexts.Onboarding.Request.PermissionRequest; | ||||||
|  |  | ||||||
| namespace LSA.Core.Kerberos.API.Controllers | namespace LSA.Core.Kerberos.API.Controllers | ||||||
| { | { | ||||||
| @@ -24,7 +25,7 @@ namespace LSA.Core.Kerberos.API.Controllers | |||||||
|     [Produces(MimeTypes.ApplicationJson)] |     [Produces(MimeTypes.ApplicationJson)] | ||||||
|     [Consumes(MimeTypes.ApplicationJson)] |     [Consumes(MimeTypes.ApplicationJson)] | ||||||
|     [ApiController] |     [ApiController] | ||||||
|     public class PermissionController(IPermissionService service, ILogger<PermissionController> logger) : ControllerBase |     public class PermissionController(IPermissionProvider service) : ControllerBase | ||||||
|     { |     { | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Gets all the permissions. |         /// Gets all the permissions. | ||||||
| @@ -39,20 +40,11 @@ namespace LSA.Core.Kerberos.API.Controllers | |||||||
|         [ProducesResponseType(typeof(IEnumerable<PermissionAdapter>), StatusCodes.Status200OK)] |         [ProducesResponseType(typeof(IEnumerable<PermissionAdapter>), StatusCodes.Status200OK)] | ||||||
|         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] |         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] | ||||||
|         [Permission("PermissionManagement.Read, RoleManagement.Read")] |         [Permission("PermissionManagement.Read, RoleManagement.Read")] | ||||||
|         public async Task<IActionResult> GetAllPermissionsAsync() |         public async Task<IActionResult> GetAllPermissionsAsync(CancellationToken cancellationToken) | ||||||
|         { |         { | ||||||
|             try |             var result = await service.GetAllPermissions(cancellationToken).ConfigureAwait(false); | ||||||
|             { |  | ||||||
|                 var result = await service.GetAllPermissionsService(); |  | ||||||
|  |  | ||||||
|             return Ok(result); |             return Ok(result); | ||||||
|         } |         } | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, "Error in GetAllPermissionsAsync"); |  | ||||||
|                 return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}"); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Gets all the permissions by permission identifiers. |         /// Gets all the permissions by permission identifiers. | ||||||
| @@ -69,30 +61,16 @@ namespace LSA.Core.Kerberos.API.Controllers | |||||||
|         [ProducesResponseType(typeof(IEnumerable<PermissionAdapter>), StatusCodes.Status200OK)] |         [ProducesResponseType(typeof(IEnumerable<PermissionAdapter>), StatusCodes.Status200OK)] | ||||||
|         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] |         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] | ||||||
|         [Permission("PermissionManagement.Read")] |         [Permission("PermissionManagement.Read")] | ||||||
|         public async Task<IActionResult> GetAllPermissionsByList([FromBody] string[] permissions) |         public async Task<IActionResult> GetAllPermissionsByList([FromBody] string[] permissions, CancellationToken cancellationToken) | ||||||
|         { |         { | ||||||
|             if (permissions == null || !permissions.Any()) |             if (permissions == null || !permissions.Any()) | ||||||
|             { |             { | ||||||
|                 return BadRequest("Permission identifiers are required."); |                 return BadRequest("Module identifiers are required."); | ||||||
|             } |  | ||||||
|  |  | ||||||
|             try |  | ||||||
|             { |  | ||||||
|                 var result = await service.GetAllPermissionsByListService(permissions); |  | ||||||
|  |  | ||||||
|                 if (result == null || !result.Any()) |  | ||||||
|                 { |  | ||||||
|                     return NotFound("No permissions found for the given identifiers."); |  | ||||||
|             } |             } | ||||||
|  |  | ||||||
|  |             var result = await service.GetAllPermissionsByList(permissions, cancellationToken).ConfigureAwait(false); | ||||||
|             return Ok(result); |             return Ok(result); | ||||||
|         } |         } | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, "Error in GetAllPermissionsByList"); |  | ||||||
|                 return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}"); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Gets the permission by identifier. |         /// Gets the permission by identifier. | ||||||
| @@ -109,22 +87,17 @@ namespace LSA.Core.Kerberos.API.Controllers | |||||||
|         [ProducesResponseType(typeof(PermissionAdapter), StatusCodes.Status200OK)] |         [ProducesResponseType(typeof(PermissionAdapter), StatusCodes.Status200OK)] | ||||||
|         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] |         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] | ||||||
|         [Permission("PermissionManagement.Read")] |         [Permission("PermissionManagement.Read")] | ||||||
|         public async Task<IActionResult> GetPermissionByIdAsync([FromRoute] string id) |         public async Task<IActionResult> GetPermissionByIdAsync([FromRoute] string _id, CancellationToken cancellationToken) | ||||||
|         { |         { | ||||||
|             try |             var result = await service.GetPermissionById(_id, cancellationToken).ConfigureAwait(false); | ||||||
|             { |  | ||||||
|                 var result = await service.GetPermissionByIdService(id); |  | ||||||
|  |  | ||||||
|                 if (result is null) return NotFound($"permission with id: '{id}' not found"); |             if (result == null) | ||||||
|  |             { | ||||||
|  |                 return NotFound("Entity not found"); | ||||||
|  |             } | ||||||
|  |  | ||||||
|             return Ok(result); |             return Ok(result); | ||||||
|         } |         } | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, "Error in GetPermissionByIdAsync"); |  | ||||||
|                 return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}"); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Creates a new permission. |         /// Creates a new permission. | ||||||
| @@ -138,18 +111,10 @@ namespace LSA.Core.Kerberos.API.Controllers | |||||||
|         [ProducesResponseType(typeof(PermissionAdapter), StatusCodes.Status201Created)] |         [ProducesResponseType(typeof(PermissionAdapter), StatusCodes.Status201Created)] | ||||||
|         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] |         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] | ||||||
|         [Permission("PermissionManagement.Write")] |         [Permission("PermissionManagement.Write")] | ||||||
|         public async Task<IActionResult> CreatePermissionAsync([FromBody] PermissionRequest newPermission) |         public async Task<IActionResult> CreatePermissionAsync([FromBody] PermissionRequest newPermission, CancellationToken cancellationToken) | ||||||
|         { |         { | ||||||
|             try |             var result = await service.CreatePermission(newPermission, cancellationToken).ConfigureAwait(false); | ||||||
|             { |             return Created("CreatedWithIdAsync", result); | ||||||
|                 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> |         /// <summary> | ||||||
| @@ -169,20 +134,17 @@ namespace LSA.Core.Kerberos.API.Controllers | |||||||
|         [ProducesResponseType(typeof(PermissionAdapter), StatusCodes.Status200OK)] |         [ProducesResponseType(typeof(PermissionAdapter), StatusCodes.Status200OK)] | ||||||
|         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] |         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] | ||||||
|         [Permission("PermissionManagement.Write")] |         [Permission("PermissionManagement.Write")] | ||||||
|         public async Task<IActionResult> UpdatePermissionAsync(PermissionAdapter entity, string id) |         public async Task<IActionResult> UpdatePermissionAsync([FromRoute] string _id, PermissionAdapter entity, CancellationToken cancellationToken) | ||||||
|         { |         { | ||||||
|             try |             if (_id != entity._Id?.ToString()) | ||||||
|             { |             { | ||||||
|                 var result = await service.UpdatePermissionService(entity, id); |                 return BadRequest("Permission ID mismatch"); | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             var result = await service.UpdatePermission(entity, cancellationToken).ConfigureAwait(false); | ||||||
|  |  | ||||||
|             return Ok(result); |             return Ok(result); | ||||||
|         } |         } | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, "Error in UpdatePermissionAsync"); |  | ||||||
|                 return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}"); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Changes the status of the permission. |         /// Changes the status of the permission. | ||||||
| @@ -201,19 +163,10 @@ namespace LSA.Core.Kerberos.API.Controllers | |||||||
|         [ProducesResponseType(typeof(PermissionAdapter), StatusCodes.Status200OK)] |         [ProducesResponseType(typeof(PermissionAdapter), StatusCodes.Status200OK)] | ||||||
|         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] |         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] | ||||||
|         [Permission("PermissionManagement.Write")] |         [Permission("PermissionManagement.Write")] | ||||||
|         public async Task<IActionResult> ChangePermissionStatus([FromRoute] string id, [FromRoute] StatusEnum newStatus) |         public async Task<IActionResult> ChangePermissionStatus([FromRoute] string id, [FromRoute] StatusEnum newStatus, CancellationToken cancellationToken) | ||||||
|         { |         { | ||||||
|             try |             var result = await service.ChangePermissionStatus(id, newStatus, cancellationToken).ConfigureAwait(false); | ||||||
|             { |  | ||||||
|                 var result = await service.ChangePermissionStatusService(id, newStatus); |  | ||||||
|  |  | ||||||
|             return Ok(result); |             return Ok(result); | ||||||
|         } |         } | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, "Error in ChangePermissionStatus"); |  | ||||||
|                 return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}"); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -12,6 +12,7 @@ using Core.Thalos.Domain.Contexts.Onboarding.Request; | |||||||
| using Core.Thalos.Provider.Contracts; | using Core.Thalos.Provider.Contracts; | ||||||
| using Microsoft.AspNetCore.Authorization; | using Microsoft.AspNetCore.Authorization; | ||||||
| using Microsoft.AspNetCore.Mvc; | using Microsoft.AspNetCore.Mvc; | ||||||
|  | using StatusEnum = Core.Blueprint.Mongo.StatusEnum; | ||||||
|  |  | ||||||
| namespace LSA.Core.Kerberos.API.Controllers | namespace LSA.Core.Kerberos.API.Controllers | ||||||
| { | { | ||||||
| @@ -23,7 +24,7 @@ namespace LSA.Core.Kerberos.API.Controllers | |||||||
|     [Produces(MimeTypes.ApplicationJson)] |     [Produces(MimeTypes.ApplicationJson)] | ||||||
|     [Consumes(MimeTypes.ApplicationJson)] |     [Consumes(MimeTypes.ApplicationJson)] | ||||||
|     [ApiController] |     [ApiController] | ||||||
|     public class RoleController(IRoleService service, ILogger<RoleController> logger) : ControllerBase |     public class RoleController(IRoleProvider service) : ControllerBase | ||||||
|     { |     { | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Gets all the roles. |         /// Gets all the roles. | ||||||
| @@ -36,20 +37,11 @@ namespace LSA.Core.Kerberos.API.Controllers | |||||||
|         [ProducesResponseType(typeof(IEnumerable<RoleAdapter>), StatusCodes.Status200OK)] |         [ProducesResponseType(typeof(IEnumerable<RoleAdapter>), StatusCodes.Status200OK)] | ||||||
|         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] |         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] | ||||||
|         [Permission("RoleManagement.Read")] |         [Permission("RoleManagement.Read")] | ||||||
|         public async Task<IActionResult> GetAllRolesAsync() |         public async Task<IActionResult> GetAllRolesAsync(CancellationToken cancellationToken) | ||||||
|         { |         { | ||||||
|             try |             var result = await service.GetAllRoles(cancellationToken).ConfigureAwait(false); | ||||||
|             { |  | ||||||
|                 var result = await service.GetAllRolesService(); |  | ||||||
|  |  | ||||||
|             return Ok(result); |             return Ok(result); | ||||||
|         } |         } | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, "Error in GetAllRolesAsync"); |  | ||||||
|                 return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}"); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Gets the role by identifier. |         /// Gets the role by identifier. | ||||||
| @@ -64,22 +56,17 @@ namespace LSA.Core.Kerberos.API.Controllers | |||||||
|         [ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)] |         [ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)] | ||||||
|         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] |         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] | ||||||
|         [Permission("RoleManagement.Read")] |         [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.GetRoleById(_id, cancellationToken).ConfigureAwait(false); | ||||||
|             { |  | ||||||
|                 var result = await service.GetRoleByIdService(id); |  | ||||||
|  |  | ||||||
|                 if (result is null) return NotFound($"role with id: '{id}' not found"); |             if (result == null) | ||||||
|  |             { | ||||||
|  |                 return NotFound("Entity not found"); | ||||||
|  |             } | ||||||
|  |  | ||||||
|             return Ok(result); |             return Ok(result); | ||||||
|         } |         } | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, "Error in GetRoleByIdAsync"); |  | ||||||
|                 return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}"); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Creates a new role. |         /// Creates a new role. | ||||||
| @@ -93,19 +80,10 @@ namespace LSA.Core.Kerberos.API.Controllers | |||||||
|         [ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status201Created)] |         [ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status201Created)] | ||||||
|         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] |         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] | ||||||
|         [Permission("RoleManagement.Write")] |         [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.CreateRole(newRole, cancellationToken).ConfigureAwait(false); | ||||||
|             { |             return Created("CreatedWithIdAsync", result); | ||||||
|                 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> |         /// <summary> | ||||||
| @@ -123,20 +101,17 @@ namespace LSA.Core.Kerberos.API.Controllers | |||||||
|         [ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)] |         [ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)] | ||||||
|         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] |         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] | ||||||
|         [Permission("RoleManagement.Write")] |         [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"); | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             var result = await service.UpdateRole(entity, cancellationToken).ConfigureAwait(false); | ||||||
|  |  | ||||||
|             return Ok(result); |             return Ok(result); | ||||||
|         } |         } | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, "Error in UpdateRoleAsync"); |  | ||||||
|                 return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}"); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Changes the status of the role. |         /// Changes the status of the role. | ||||||
| @@ -153,21 +128,11 @@ namespace LSA.Core.Kerberos.API.Controllers | |||||||
|         [ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)] |         [ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)] | ||||||
|         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] |         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] | ||||||
|         [Permission("RoleManagement.Write")] |         [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.ChangeRoleStatus(id, newStatus, cancellationToken).ConfigureAwait(false); | ||||||
|             { |  | ||||||
|                 var result = await service.ChangeRoleStatusService(id, newStatus); |  | ||||||
|  |  | ||||||
|             return Ok(result); |             return Ok(result); | ||||||
|         } |         } | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, "Error in ChangeRoleStatus"); |  | ||||||
|                 return StatusCode(500, $"Internal server error, ErrorMessage: {ex.Message}"); |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Adds an application to the role's list of applications. |         /// Adds an application to the role's list of applications. | ||||||
| @@ -183,20 +148,10 @@ namespace LSA.Core.Kerberos.API.Controllers | |||||||
|         [ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)] |         [ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)] | ||||||
|         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] |         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] | ||||||
|         [Permission("RoleManagement.Write")] |         [Permission("RoleManagement.Write")] | ||||||
|         public async Task<IActionResult> AddApplicationToRoleAsync([FromRoute] string roleId, |         public async Task<IActionResult> AddApplicationToRoleAsync([FromRoute] string roleId, [FromRoute] ApplicationsEnum application, CancellationToken cancellationToken) | ||||||
|                                                                    [FromRoute] ApplicationsEnum application) |  | ||||||
|         { |         { | ||||||
|             try |             var result = await service.AddApplicationToRole(roleId, application, cancellationToken).ConfigureAwait(false); | ||||||
|             { |             return Ok(result); | ||||||
|                 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> |         /// <summary> | ||||||
| @@ -213,19 +168,10 @@ namespace LSA.Core.Kerberos.API.Controllers | |||||||
|         [ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)] |         [ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)] | ||||||
|         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] |         [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] | ||||||
|         [Permission("RoleManagement.Write")] |         [Permission("RoleManagement.Write")] | ||||||
|         public async Task<IActionResult> RemoveApplicationFromRoleAsync([FromRoute] string roleId, |         public async Task<IActionResult> RemoveApplicationFromRoleAsync([FromRoute] string roleId, [FromRoute] ApplicationsEnum application, CancellationToken cancellationToken) | ||||||
|                                                                         [FromRoute] ApplicationsEnum application) |  | ||||||
|         { |         { | ||||||
|             try |             var result = await service.RemoveApplicationFromRole(roleId, application, cancellationToken).ConfigureAwait(false); | ||||||
|             { |             return Ok(result); | ||||||
|                 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}"); |  | ||||||
|             } |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -3,13 +3,13 @@ | |||||||
| //     AgileWebs | //     AgileWebs | ||||||
| // </copyright> | // </copyright> | ||||||
| // *********************************************************************** | // *********************************************************************** | ||||||
|  | using Core.Blueprint.Mongo; | ||||||
| using Core.Thalos.Adapters; | using Core.Thalos.Adapters; | ||||||
| using Core.Thalos.Adapters.Common.Enums; |  | ||||||
| using Core.Thalos.Domain.Contexts.Onboarding.Request; | using Core.Thalos.Domain.Contexts.Onboarding.Request; | ||||||
| 
 | 
 | ||||||
| namespace Core.Thalos.Provider.Contracts | namespace Core.Thalos.Provider.Contracts | ||||||
| { | { | ||||||
|     public interface IPermissionService |     public interface IPermissionProvider | ||||||
|     { |     { | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Creates a new Permission. |         /// Creates a new Permission. | ||||||
| @@ -17,7 +17,7 @@ namespace Core.Thalos.Provider.Contracts | |||||||
|         /// <param name="entity">The Permission to be created.</param> |         /// <param name="entity">The Permission to be created.</param> | ||||||
|         /// <returns>A <see cref="{Task{PermissionAdapter}}"/> representing |         /// <returns>A <see cref="{Task{PermissionAdapter}}"/> representing | ||||||
|         /// the asynchronous execution of the service.</returns> |         /// the asynchronous execution of the service.</returns> | ||||||
|         Task<PermissionAdapter> CreatePermissionService(PermissionRequest newPermission); |         ValueTask<PermissionAdapter> CreatePermission(PermissionRequest newPermission, CancellationToken cancellationToken); | ||||||
| 
 | 
 | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Gets an Permission by identifier. |         /// Gets an Permission by identifier. | ||||||
| @@ -25,14 +25,14 @@ namespace Core.Thalos.Provider.Contracts | |||||||
|         /// <param name="id">The Permission identifier.</param> |         /// <param name="id">The Permission identifier.</param> | ||||||
|         /// <returns>A <see cref="{Task{PermissionAdapter}}"/> representing |         /// <returns>A <see cref="{Task{PermissionAdapter}}"/> representing | ||||||
|         /// the asynchronous execution of the service.</returns> |         /// the asynchronous execution of the service.</returns> | ||||||
|         Task<PermissionAdapter> GetPermissionByIdService(string id); |         ValueTask<PermissionAdapter> GetPermissionById(string _id, CancellationToken cancellationToken); | ||||||
| 
 | 
 | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Gets all the roles. |         /// Gets all the roles. | ||||||
|         /// </summary> |         /// </summary> | ||||||
|         /// <returns>A <see cref="{Task{IEnumerbale{PermissionAdapter}}}"/> representing |         /// <returns>A <see cref="{Task{IEnumerbale{PermissionAdapter}}}"/> representing | ||||||
|         /// the asynchronous execution of the service.</returns> |         /// the asynchronous execution of the service.</returns> | ||||||
|         Task<IEnumerable<PermissionAdapter>> GetAllPermissionsService(); |         ValueTask<IEnumerable<PermissionAdapter>> GetAllPermissions(CancellationToken cancellationToken); | ||||||
| 
 | 
 | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Gets all the permissions by permissions identifier list. |         /// Gets all the permissions by permissions identifier list. | ||||||
| @@ -40,7 +40,7 @@ namespace Core.Thalos.Provider.Contracts | |||||||
|         /// <param name="permissions">The list of permissions identifiers.</param> |         /// <param name="permissions">The list of permissions identifiers.</param> | ||||||
|         /// <returns>A <see cref="Task{IEnumerable{PermissionAdapter}}"/> representing |         /// <returns>A <see cref="Task{IEnumerable{PermissionAdapter}}"/> representing | ||||||
|         /// the asynchronous execution of the service.</returns> |         /// the asynchronous execution of the service.</returns> | ||||||
|         Task<IEnumerable<PermissionAdapter>> GetAllPermissionsByListService(string[] permissions); |         ValueTask<IEnumerable<PermissionAdapter>> GetAllPermissionsByList(string[] permissions, CancellationToken cancellationToken); | ||||||
| 
 | 
 | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Changes the status of the permission. |         /// Changes the status of the permission. | ||||||
| @@ -50,7 +50,7 @@ namespace Core.Thalos.Provider.Contracts | |||||||
|         /// <returns>The <see cref="PermissionAdapter"/> updated entity.</returns> |         /// <returns>The <see cref="PermissionAdapter"/> updated entity.</returns> | ||||||
|         /// <returns>A <see cref="{Task{PermissionAdapter}}"/> representing |         /// <returns>A <see cref="{Task{PermissionAdapter}}"/> representing | ||||||
|         /// the asynchronous execution of the service.</returns> |         /// the asynchronous execution of the service.</returns> | ||||||
|         Task<PermissionAdapter> ChangePermissionStatusService(string id, StatusEnum newStatus); |         ValueTask<PermissionAdapter> ChangePermissionStatus(string id, StatusEnum newStatus, CancellationToken cancellationToken); | ||||||
| 
 | 
 | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Updates a Permission by id. |         /// Updates a Permission by id. | ||||||
| @@ -59,6 +59,6 @@ namespace Core.Thalos.Provider.Contracts | |||||||
|         /// <param name="id">The Permission identifier.</param> |         /// <param name="id">The Permission identifier.</param> | ||||||
|         /// <returns>A <see cref="{Task{PermissionAdapter}}"/> representing |         /// <returns>A <see cref="{Task{PermissionAdapter}}"/> representing | ||||||
|         /// the asynchronous execution of the service.</returns> |         /// the asynchronous execution of the service.</returns> | ||||||
|         Task<PermissionAdapter> UpdatePermissionService(PermissionAdapter entity, string id); |         ValueTask<PermissionAdapter> UpdatePermission(PermissionAdapter entity, CancellationToken cancellationToken); | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @@ -9,7 +9,7 @@ using Core.Thalos.Domain.Contexts.Onboarding.Request; | |||||||
| 
 | 
 | ||||||
| namespace Core.Thalos.Provider.Contracts | namespace Core.Thalos.Provider.Contracts | ||||||
| { | { | ||||||
|     public interface IRoleService |     public interface IRoleProvider | ||||||
|     { |     { | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Creates a new Role. |         /// Creates a new Role. | ||||||
| @@ -17,7 +17,7 @@ namespace Core.Thalos.Provider.Contracts | |||||||
|         /// <param name="entity">The Role to be created.</param> |         /// <param name="entity">The Role to be created.</param> | ||||||
|         /// <returns>A <see cref="{Task{RoleAdapter}}"/> representing |         /// <returns>A <see cref="{Task{RoleAdapter}}"/> representing | ||||||
|         /// the asynchronous execution of the service.</returns> |         /// the asynchronous execution of the service.</returns> | ||||||
|         Task<RoleAdapter> CreateRoleService(RoleRequest newRole); |         ValueTask<RoleAdapter> CreateRole(RoleRequest newRole, CancellationToken cancellationToken); | ||||||
| 
 | 
 | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Gets an Role by identifier. |         /// Gets an Role by identifier. | ||||||
| @@ -25,14 +25,14 @@ namespace Core.Thalos.Provider.Contracts | |||||||
|         /// <param name="id">The Role identifier.</param> |         /// <param name="id">The Role identifier.</param> | ||||||
|         /// <returns>A <see cref="{Task{RoleAdapter}}"/> representing |         /// <returns>A <see cref="{Task{RoleAdapter}}"/> representing | ||||||
|         /// the asynchronous execution of the service.</returns> |         /// the asynchronous execution of the service.</returns> | ||||||
|         Task<RoleAdapter> GetRoleByIdService(string id); |         ValueTask<RoleAdapter> GetRoleById(string _id, CancellationToken cancellationToken); | ||||||
| 
 | 
 | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Gets all the roles. |         /// Gets all the roles. | ||||||
|         /// </summary> |         /// </summary> | ||||||
|         /// <returns>A <see cref="{Task{IEnumerbale{RoleAdapter}}}"/> representing |         /// <returns>A <see cref="{Task{IEnumerbale{RoleAdapter}}}"/> representing | ||||||
|         /// the asynchronous execution of the service.</returns> |         /// the asynchronous execution of the service.</returns> | ||||||
|         Task<IEnumerable<RoleAdapter>> GetAllRolesService(); |         ValueTask<IEnumerable<RoleAdapter>> GetAllRoles(CancellationToken cancellationToken); | ||||||
| 
 | 
 | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Changes the status of the role. |         /// Changes the status of the role. | ||||||
| @@ -42,7 +42,7 @@ namespace Core.Thalos.Provider.Contracts | |||||||
|         /// <returns>The <see cref="RoleAdapter"/> updated entity.</returns> |         /// <returns>The <see cref="RoleAdapter"/> updated entity.</returns> | ||||||
|         /// <returns>A <see cref="{Task{RoleAdapter}}"/> representing |         /// <returns>A <see cref="{Task{RoleAdapter}}"/> representing | ||||||
|         /// the asynchronous execution of the service.</returns> |         /// the asynchronous execution of the service.</returns> | ||||||
|         Task<RoleAdapter> ChangeRoleStatusService(string id, StatusEnum newStatus); |         ValueTask<RoleAdapter> ChangeRoleStatus(string id, Core.Blueprint.Mongo.StatusEnum newStatus, CancellationToken cancellationToken); | ||||||
| 
 | 
 | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Updates a Role by id. |         /// Updates a Role by id. | ||||||
| @@ -51,7 +51,7 @@ namespace Core.Thalos.Provider.Contracts | |||||||
|         /// <param name="id">The Role identifier.</param> |         /// <param name="id">The Role identifier.</param> | ||||||
|         /// <returns>A <see cref="{Task{RoleAdapter}}"/> representing |         /// <returns>A <see cref="{Task{RoleAdapter}}"/> representing | ||||||
|         /// the asynchronous execution of the service.</returns> |         /// the asynchronous execution of the service.</returns> | ||||||
|         Task<RoleAdapter> UpdateRoleService(RoleAdapter entity, string id); |         ValueTask<RoleAdapter> UpdateRole(RoleAdapter entity, CancellationToken cancellationToken); | ||||||
| 
 | 
 | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Adds an application to the role's list of applications. |         /// Adds an application to the role's list of applications. | ||||||
| @@ -59,7 +59,7 @@ namespace Core.Thalos.Provider.Contracts | |||||||
|         /// <param name="roleId">The identifier of the role to which the application will be added.</param> |         /// <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> |         /// <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> |         /// <returns>A <see cref="{Task{RoleAdapter}}"/> representing the asynchronous operation, with the updated role object.</returns> | ||||||
|         Task<RoleAdapter> AddApplicationToRoleService(string roleId, ApplicationsEnum application); |         ValueTask<RoleAdapter> AddApplicationToRole(string roleId, ApplicationsEnum application, CancellationToken cancellationToken); | ||||||
| 
 | 
 | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Removes an application from the role's list of applications. |         /// Removes an application from the role's list of applications. | ||||||
| @@ -67,6 +67,6 @@ namespace Core.Thalos.Provider.Contracts | |||||||
|         /// <param name="roleId">The identifier of the role from which the application will be removed.</param> |         /// <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> |         /// <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> |         /// <returns>A <see cref="{Task{RoleAdapter}}"/> representing the asynchronous operation, with the updated role object.</returns> | ||||||
|         Task<RoleAdapter> RemoveApplicationFromRoleService(string roleId, ApplicationsEnum application); |         ValueTask<RoleAdapter> RemoveApplicationFromRole(string roleId, ApplicationsEnum application, CancellationToken cancellationToken); | ||||||
|     } |     } | ||||||
| } | } | ||||||
							
								
								
									
										153
									
								
								Core.Thalos.Provider/Providers/Onboarding/PermissionProvider.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										153
									
								
								Core.Thalos.Provider/Providers/Onboarding/PermissionProvider.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,153 @@ | |||||||
|  | // *********************************************************************** | ||||||
|  | // <copyright file="PermissionService.cs"> | ||||||
|  | //     AgileWebs | ||||||
|  | // </copyright> | ||||||
|  | // *********************************************************************** | ||||||
|  | using Core.Thalos.Adapters; | ||||||
|  | using Core.Blueprint.Mongo; | ||||||
|  | using Core.Blueprint.Redis; | ||||||
|  | using Core.Blueprint.Redis.Helpers; | ||||||
|  | using Mapster; | ||||||
|  | using Microsoft.Extensions.Options; | ||||||
|  | using MongoDB.Driver; | ||||||
|  | using Core.Thalos.Provider.Contracts; | ||||||
|  | using Core.Thalos.Domain.Contexts.Onboarding.Request; | ||||||
|  |  | ||||||
|  | namespace Core.Thalos.Provider.Providers.Onboarding | ||||||
|  | { | ||||||
|  |     /// <summary> | ||||||
|  |     /// Handles all services and business rules related to <see cref="PermissionAdapter"/>. | ||||||
|  |     /// </summary> | ||||||
|  |     public class PermissionProvider : IPermissionProvider | ||||||
|  |     { | ||||||
|  |         private readonly CollectionRepository<PermissionAdapter> repository; | ||||||
|  |         private readonly CacheSettings cacheSettings; | ||||||
|  |         private readonly IRedisCacheProvider cacheProvider; | ||||||
|  |  | ||||||
|  |         public PermissionProvider(CollectionRepository<PermissionAdapter> repository, | ||||||
|  |         IRedisCacheProvider cacheProvider, IOptions<CacheSettings> cacheSettings) | ||||||
|  |         { | ||||||
|  |             this.repository = repository; | ||||||
|  |             this.repository.CollectionInitialization(); | ||||||
|  |             this.cacheSettings = cacheSettings.Value; | ||||||
|  |             this.cacheProvider = cacheProvider; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         /// <summary> | ||||||
|  |         /// Creates a new Permission. | ||||||
|  |         /// </summary> | ||||||
|  |         /// <param name="entity">The Permission to be created.</param> | ||||||
|  |         /// <returns>A <see cref="{Task{PermissionAdapter}}"/> representing | ||||||
|  |         /// the asynchronous execution of the service.</returns> | ||||||
|  |         public async ValueTask<PermissionAdapter> CreatePermission(PermissionRequest newPermission, CancellationToken cancellationToken) | ||||||
|  |         { | ||||||
|  |             var permissionCollection = newPermission.Adapt<PermissionAdapter>(); | ||||||
|  |  | ||||||
|  |             await repository.InsertOneAsync(permissionCollection); | ||||||
|  |  | ||||||
|  |             return permissionCollection; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         /// <summary> | ||||||
|  |         /// Gets an Permission by identifier. | ||||||
|  |         /// </summary> | ||||||
|  |         /// <param name="id">The Permission identifier.</param> | ||||||
|  |         /// <returns>A <see cref="{Task{PermissionAdapter}}"/> representing | ||||||
|  |         /// the asynchronous execution of the service.</returns>0 | ||||||
|  |         public async ValueTask<PermissionAdapter> GetPermissionById(string _id, CancellationToken cancellationToken) | ||||||
|  |         { | ||||||
|  |             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetPermissionById", _id); | ||||||
|  |             var cachedData = await cacheProvider.GetAsync<PermissionAdapter>(cacheKey); | ||||||
|  |  | ||||||
|  |             if (cachedData is not null) { return cachedData; } | ||||||
|  |  | ||||||
|  |             var permission = await repository.FindByIdAsync(_id); | ||||||
|  |  | ||||||
|  |             await cacheProvider.SetAsync(cacheKey, permission); | ||||||
|  |  | ||||||
|  |             return permission; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         /// <summary> | ||||||
|  |         /// Gets all the permissions. | ||||||
|  |         /// </summary> | ||||||
|  |         /// <returns>A <see cref="{Task{IEnumerbale{PermissionAdapter}}}"/> representing | ||||||
|  |         /// the asynchronous execution of the service.</returns> | ||||||
|  |         public async ValueTask<IEnumerable<PermissionAdapter>> GetAllPermissions(CancellationToken cancellationToken) | ||||||
|  |         { | ||||||
|  |             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllPermissions"); | ||||||
|  |             var cachedData = await cacheProvider.GetAsync<IEnumerable<PermissionAdapter>>(cacheKey) ?? []; | ||||||
|  |  | ||||||
|  |             if (cachedData.Any()) return cachedData; | ||||||
|  |  | ||||||
|  |             var permissions = await repository.AsQueryable(); | ||||||
|  |  | ||||||
|  |             await cacheProvider.SetAsync(cacheKey, permissions); | ||||||
|  |  | ||||||
|  |             return permissions; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         /// <summary> | ||||||
|  |         /// Gets all the permissions by permissions identifier list. | ||||||
|  |         /// </summary> | ||||||
|  |         /// <param name="permissions">The list of permissions identifiers.</param> | ||||||
|  |         /// <returns>A <see cref="Task{IEnumerable{PermissionAdapter}}"/> representing | ||||||
|  |         /// the asynchronous execution of the service.</returns> | ||||||
|  |         public async ValueTask<IEnumerable<PermissionAdapter>> GetAllPermissionsByList(string[] permissions, CancellationToken cancellationToken) | ||||||
|  |         { | ||||||
|  |             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllPermissionsByList", permissions); | ||||||
|  |  | ||||||
|  |             var cachedData = await cacheProvider.GetAsync<IEnumerable<PermissionAdapter>>(cacheKey) ?? []; | ||||||
|  |  | ||||||
|  |             if (cachedData.Any()) return cachedData; | ||||||
|  |  | ||||||
|  |             var builder = Builders<PermissionAdapter>.Filter; | ||||||
|  |             var filters = new List<FilterDefinition<PermissionAdapter>>(); | ||||||
|  |  | ||||||
|  |             if (permissions == null || !permissions.Any()) | ||||||
|  |             { | ||||||
|  |                 filters.Add(builder.In(x => x.Id, permissions)); | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             var finalFilter = filters.Any() ? builder.And(filters) : builder.Empty; | ||||||
|  |  | ||||||
|  |             var permissionsList = await repository.FilterByMongoFilterAsync(finalFilter); | ||||||
|  |  | ||||||
|  |             await cacheProvider.SetAsync(cacheKey, permissionsList); | ||||||
|  |  | ||||||
|  |             return permissionsList; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |  | ||||||
|  |         /// <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>A <see cref="{Task{PermissionAdapter}}"/> representing | ||||||
|  |         /// the asynchronous execution of the service.</returns> | ||||||
|  |         public async ValueTask<PermissionAdapter> ChangePermissionStatus(string id, StatusEnum newStatus, CancellationToken cancellationToken) | ||||||
|  |         { | ||||||
|  |             var entity = await repository.FindByIdAsync(id); | ||||||
|  |             entity.Status = newStatus; | ||||||
|  |  | ||||||
|  |             await repository.ReplaceOneAsync(entity); | ||||||
|  |  | ||||||
|  |             return entity; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         /// <summary> | ||||||
|  |         /// Updates a Permission by id. | ||||||
|  |         /// </summary> | ||||||
|  |         /// <param name="entity">The Permission to be updated.</param> | ||||||
|  |         /// <param name="id">The Permission identifier.</param> | ||||||
|  |         /// <returns>A <see cref="{Task{PermissionAdapter}}"/> representing | ||||||
|  |         /// the asynchronous execution of the service.</returns> | ||||||
|  |         public async ValueTask<PermissionAdapter> UpdatePermission(PermissionAdapter entity, CancellationToken cancellationToken) | ||||||
|  |         { | ||||||
|  |             await repository.ReplaceOneAsync(entity); | ||||||
|  |  | ||||||
|  |             return entity; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -1,234 +0,0 @@ | |||||||
| // *********************************************************************** |  | ||||||
| // <copyright file="PermissionService.cs"> |  | ||||||
| //     AgileWebs |  | ||||||
| // </copyright> |  | ||||||
| // *********************************************************************** |  | ||||||
| using Core.Thalos.Adapters; |  | ||||||
| using Core.Thalos.Adapters.Common.Constants; |  | ||||||
| using Core.Thalos.Adapters.Common.Enums; |  | ||||||
| using Core.Thalos.Domain.Contexts.Onboarding.Mappers; |  | ||||||
| using Core.Thalos.Domain.Contexts.Onboarding.Request; |  | ||||||
| using Core.Thalos.Infraestructure.Caching.Configs; |  | ||||||
| using Core.Thalos.Infraestructure.Caching.Contracts; |  | ||||||
| using Core.Thalos.Provider.Contracts; |  | ||||||
| using LSA.Core.Dapper.Service.Caching; |  | ||||||
| using Microsoft.AspNetCore.Http; |  | ||||||
| using Microsoft.Extensions.Logging; |  | ||||||
| using Microsoft.Extensions.Options; |  | ||||||
| using MongoDB.Bson; |  | ||||||
| using MongoDB.Driver; |  | ||||||
|  |  | ||||||
| namespace Core.Thalos.Provider.Providers.Onboarding |  | ||||||
| { |  | ||||||
|     /// <summary> |  | ||||||
|     /// Handles all services and business rules related to <see cref="PermissionAdapter"/>. |  | ||||||
|     /// </summary> |  | ||||||
|     public class PermissionService(ILogger<PermissionService> logger, IHttpContextAccessor httpContextAccessor, ICacheService cacheService, |  | ||||||
|         IOptions<CacheSettings> cacheSettings, IMongoDatabase database) : IPermissionService |  | ||||||
|     { |  | ||||||
|         private readonly CacheSettings _cacheSettings = cacheSettings.Value; |  | ||||||
|  |  | ||||||
|         /// <summary> |  | ||||||
|         /// Creates a new Permission. |  | ||||||
|         /// </summary> |  | ||||||
|         /// <param name="entity">The Permission to be created.</param> |  | ||||||
|         /// <returns>A <see cref="{Task{PermissionAdapter}}"/> representing |  | ||||||
|         /// the asynchronous execution of the service.</returns> |  | ||||||
|         public async Task<PermissionAdapter> CreatePermissionService(PermissionRequest newPermission) |  | ||||||
|         { |  | ||||||
|             try |  | ||||||
|             { |  | ||||||
|                 var entity = newPermission.ToAdapter(httpContextAccessor); |  | ||||||
|                 await database.GetCollection<PermissionAdapter>(CollectionNames.Permission).InsertOneAsync(entity); |  | ||||||
|                 entity.Id = (entity as dynamic ?? "").Id.ToString(); |  | ||||||
|  |  | ||||||
|                 return entity; |  | ||||||
|             } |  | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, $"CreatePermissionService: Error in getting data - {ex.Message}"); |  | ||||||
|                 throw new Exception(ex.Message, ex); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         /// <summary> |  | ||||||
|         /// Gets an Permission by identifier. |  | ||||||
|         /// </summary> |  | ||||||
|         /// <param name="id">The Permission identifier.</param> |  | ||||||
|         /// <returns>A <see cref="{Task{PermissionAdapter}}"/> representing |  | ||||||
|         /// the asynchronous execution of the service.</returns>0 |  | ||||||
|         public async Task<PermissionAdapter> GetPermissionByIdService(string id) |  | ||||||
|         { |  | ||||||
|             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetPermissionByIdService", id); |  | ||||||
|             var cachedData = await cacheService.GetAsync<PermissionAdapter>(cacheKey); |  | ||||||
|  |  | ||||||
|             if (cachedData is not null) { return cachedData; } |  | ||||||
|  |  | ||||||
|             try |  | ||||||
|             { |  | ||||||
|                 var filter = Builders<PermissionAdapter>.Filter.And( |  | ||||||
|                     Builders<PermissionAdapter>.Filter.Eq("_id", ObjectId.Parse(id)), |  | ||||||
|                     Builders<PermissionAdapter>.Filter.Eq("status", StatusEnum.Active.ToString()) |  | ||||||
|                 ); |  | ||||||
|  |  | ||||||
|                 var permission = await database.GetCollection<PermissionAdapter>(CollectionNames.Permission) |  | ||||||
|                                     .Find(filter) |  | ||||||
|                                     .FirstOrDefaultAsync(); |  | ||||||
|  |  | ||||||
|                 var cacheDuration = CacheHelper.GetCacheDuration(_cacheSettings); |  | ||||||
|  |  | ||||||
|                 await cacheService.SetAsync(cacheKey, permission, cacheDuration); |  | ||||||
|  |  | ||||||
|                 return permission; |  | ||||||
|             } |  | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, $"GetPermissionByIdService: Error in getting data - {ex.Message}"); |  | ||||||
|                 throw new Exception(ex.Message, ex); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         /// <summary> |  | ||||||
|         /// Gets all the permissions. |  | ||||||
|         /// </summary> |  | ||||||
|         /// <returns>A <see cref="{Task{IEnumerbale{PermissionAdapter}}}"/> representing |  | ||||||
|         /// the asynchronous execution of the service.</returns> |  | ||||||
|         public async Task<IEnumerable<PermissionAdapter>> GetAllPermissionsService() |  | ||||||
|         { |  | ||||||
|             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllPermissionsService"); |  | ||||||
|             var cachedData = await cacheService.GetAsync<IEnumerable<PermissionAdapter>>(cacheKey) ?? []; |  | ||||||
|  |  | ||||||
|             if (cachedData.Any()) return cachedData; |  | ||||||
|  |  | ||||||
|             try |  | ||||||
|             { |  | ||||||
|                 var filter = Builders<PermissionAdapter>.Filter.Eq("status", StatusEnum.Active.ToString()); |  | ||||||
|  |  | ||||||
|                 var roles = await database.GetCollection<PermissionAdapter>(CollectionNames.Permission) |  | ||||||
|                                     .Find(filter) |  | ||||||
|                                     .ToListAsync(); |  | ||||||
|  |  | ||||||
|                 var cacheDuration = CacheHelper.GetCacheDuration(_cacheSettings); |  | ||||||
|  |  | ||||||
|                 await cacheService.SetAsync(cacheKey, roles, cacheDuration); |  | ||||||
|  |  | ||||||
|                 return roles; |  | ||||||
|             } |  | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, $"GetAllPermissionsService: Error in getting data - {ex.Message}"); |  | ||||||
|                 throw new Exception(ex.Message, ex); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         /// <summary> |  | ||||||
|         /// Gets all the permissions by permissions identifier list. |  | ||||||
|         /// </summary> |  | ||||||
|         /// <param name="permissions">The list of permissions identifiers.</param> |  | ||||||
|         /// <returns>A <see cref="Task{IEnumerable{PermissionAdapter}}"/> representing |  | ||||||
|         /// the asynchronous execution of the service.</returns> |  | ||||||
|         public async Task<IEnumerable<PermissionAdapter>> GetAllPermissionsByListService(string[] permissions) |  | ||||||
|         { |  | ||||||
|             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllPermissionsByListService", permissions); |  | ||||||
|  |  | ||||||
|             var cachedData = await cacheService.GetAsync<IEnumerable<PermissionAdapter>>(cacheKey); |  | ||||||
|  |  | ||||||
|             if (cachedData != null && cachedData.Any()) return cachedData; |  | ||||||
|  |  | ||||||
|             try |  | ||||||
|             { |  | ||||||
|                 var objectIds = permissions.Select(id => ObjectId.Parse(id)).ToArray(); |  | ||||||
|  |  | ||||||
|                 var filter = Builders<PermissionAdapter>.Filter.In("_id", objectIds) |  | ||||||
|                                 & Builders<PermissionAdapter>.Filter.Eq("status", StatusEnum.Active.ToString()); |  | ||||||
|  |  | ||||||
|                 var roles = await database.GetCollection<PermissionAdapter>(CollectionNames.Permission) |  | ||||||
|                                           .Find(filter) |  | ||||||
|                                           .ToListAsync(); |  | ||||||
|  |  | ||||||
|                 var cacheDuration = CacheHelper.GetCacheDuration(_cacheSettings); |  | ||||||
|  |  | ||||||
|                 await cacheService.SetAsync(cacheKey, roles, cacheDuration); |  | ||||||
|  |  | ||||||
|                 return roles; |  | ||||||
|             } |  | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, $"GetAllPermissionsByListService: Error in getting data - {ex.Message}"); |  | ||||||
|                 throw new Exception(ex.Message, ex); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|  |  | ||||||
|         /// <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>A <see cref="{Task{PermissionAdapter}}"/> representing |  | ||||||
|         /// the asynchronous execution of the service.</returns> |  | ||||||
|         public async Task<PermissionAdapter> ChangePermissionStatusService(string id, StatusEnum newStatus) |  | ||||||
|         { |  | ||||||
|             try |  | ||||||
|             { |  | ||||||
|                 var filter = Builders<PermissionAdapter>.Filter |  | ||||||
|                     .Eq("_id", ObjectId.Parse(id)); |  | ||||||
|  |  | ||||||
|                 var update = Builders<PermissionAdapter>.Update |  | ||||||
|                             //.Set(v => v.Status, newStatus) |  | ||||||
|                             .Set(v => v.UpdatedBy, Helper.GetEmail(httpContextAccessor)) |  | ||||||
|                             .Set(v => v.UpdatedAt, DateTime.UtcNow); |  | ||||||
|  |  | ||||||
|                 await database.GetCollection<PermissionAdapter>(CollectionNames.Permission).UpdateOneAsync(filter, update); |  | ||||||
|  |  | ||||||
|                 var updatedPermission = await database.GetCollection<PermissionAdapter>(CollectionNames.Permission) |  | ||||||
|                                     .Find(filter) |  | ||||||
|                                     .FirstOrDefaultAsync(); |  | ||||||
|  |  | ||||||
|                 return updatedPermission; |  | ||||||
|             } |  | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, $"ChangePermissionStatusService: Error in getting data - {ex.Message}"); |  | ||||||
|                 throw new Exception(ex.Message, ex); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         /// <summary> |  | ||||||
|         /// Updates a Permission by id. |  | ||||||
|         /// </summary> |  | ||||||
|         /// <param name="entity">The Permission to be updated.</param> |  | ||||||
|         /// <param name="id">The Permission identifier.</param> |  | ||||||
|         /// <returns>A <see cref="{Task{PermissionAdapter}}"/> representing |  | ||||||
|         /// the asynchronous execution of the service.</returns> |  | ||||||
|         public async Task<PermissionAdapter> UpdatePermissionService(PermissionAdapter entity, string id) |  | ||||||
|         { |  | ||||||
|             try |  | ||||||
|             { |  | ||||||
|                 var filter = Builders<PermissionAdapter>.Filter |  | ||||||
|                     .Eq("_id", ObjectId.Parse(id)); |  | ||||||
|  |  | ||||||
|                 var update = Builders<PermissionAdapter>.Update |  | ||||||
|                             .Set(v => v.Name, entity.Name) |  | ||||||
|                             .Set(v => v.Description, entity.Description) |  | ||||||
|                             .Set(v => v.AccessLevel, entity.AccessLevel) |  | ||||||
|                             .Set(v => v.Status, entity.Status) |  | ||||||
|                             .Set(v => v.UpdatedBy, Helper.GetEmail(httpContextAccessor)) |  | ||||||
|                             .Set(v => v.UpdatedAt, DateTime.UtcNow); |  | ||||||
|  |  | ||||||
|                 await database.GetCollection<PermissionAdapter>(CollectionNames.Permission).UpdateOneAsync(filter, update); |  | ||||||
|  |  | ||||||
|                 var updatedPermission = await database.GetCollection<PermissionAdapter>(CollectionNames.Permission) |  | ||||||
|                                             .Find(filter) |  | ||||||
|                                             .FirstOrDefaultAsync(); |  | ||||||
|  |  | ||||||
|                 return updatedPermission; |  | ||||||
|             } |  | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, $"UpdatePermissionService: Error in getting data - {ex.Message}"); |  | ||||||
|                 throw new Exception(ex.Message, ex); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
							
								
								
									
										170
									
								
								Core.Thalos.Provider/Providers/Onboarding/RoleProvider.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										170
									
								
								Core.Thalos.Provider/Providers/Onboarding/RoleProvider.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,170 @@ | |||||||
|  | // *********************************************************************** | ||||||
|  | // <copyright file="RoleService.cs"> | ||||||
|  | //     AgileWebs | ||||||
|  | // </copyright> | ||||||
|  | // *********************************************************************** | ||||||
|  | using Core.Thalos.Adapters; | ||||||
|  | using Core.Thalos.Adapters.Common.Enums; | ||||||
|  | using Core.Blueprint.Mongo; | ||||||
|  | using Core.Blueprint.Redis; | ||||||
|  | using Core.Blueprint.Redis.Helpers; | ||||||
|  | using Mapster; | ||||||
|  | using Microsoft.Extensions.Options; | ||||||
|  | using MongoDB.Driver; | ||||||
|  | using Core.Thalos.Provider.Contracts; | ||||||
|  | using MongoDB.Bson; | ||||||
|  | using System.Text.RegularExpressions; | ||||||
|  | using MongoDB.Bson.Serialization; | ||||||
|  | using Core.Thalos.Domain.Contexts.Onboarding.Request; | ||||||
|  | using Microsoft.Graph; | ||||||
|  | using System.ComponentModel.Design; | ||||||
|  |  | ||||||
|  | namespace Core.Thalos.Provider.Providers.Onboarding | ||||||
|  | { | ||||||
|  |     /// <summary> | ||||||
|  |     /// Handles all services and business rules related to <see cref="RoleAdapter"/>. | ||||||
|  |     /// </summary> | ||||||
|  |     public class RoleProvider : IRoleProvider | ||||||
|  |     { | ||||||
|  |         private readonly CollectionRepository<RoleAdapter> repository; | ||||||
|  |         private readonly CacheSettings cacheSettings; | ||||||
|  |         private readonly IRedisCacheProvider cacheProvider; | ||||||
|  |  | ||||||
|  |         public RoleProvider(CollectionRepository<RoleAdapter> repository, | ||||||
|  |         IRedisCacheProvider cacheProvider, IOptions<CacheSettings> cacheSettings) | ||||||
|  |         { | ||||||
|  |             this.repository = repository; | ||||||
|  |             this.repository.CollectionInitialization(); | ||||||
|  |             this.cacheSettings = cacheSettings.Value; | ||||||
|  |             this.cacheProvider = cacheProvider; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         /// <summary> | ||||||
|  |         /// Creates a new Role. | ||||||
|  |         /// </summary> | ||||||
|  |         /// <param name="entity">The Role to be created.</param> | ||||||
|  |         /// <returns>A <see cref="{Task{RoleAdapter}}"/> representing | ||||||
|  |         /// the asynchronous execution of the service.</returns> | ||||||
|  |         public async ValueTask<RoleAdapter> CreateRole(RoleRequest newRole, CancellationToken cancellationToken) | ||||||
|  |         { | ||||||
|  |             var roleCollection = newRole.Adapt<RoleAdapter>(); | ||||||
|  |  | ||||||
|  |             await repository.InsertOneAsync(roleCollection); | ||||||
|  |  | ||||||
|  |             return roleCollection; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         /// <summary> | ||||||
|  |         /// Gets an Role by identifier. | ||||||
|  |         /// </summary> | ||||||
|  |         /// <param name="id">The Role identifier.</param> | ||||||
|  |         /// <returns>A <see cref="{Task{RoleAdapter}}"/> representing | ||||||
|  |         /// the asynchronous execution of the service.</returns> | ||||||
|  |         public async ValueTask<RoleAdapter> GetRoleById(string _id, CancellationToken cancellationToken) | ||||||
|  |         { | ||||||
|  |             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetRoleById", _id); | ||||||
|  |             var cachedData = await cacheProvider.GetAsync<RoleAdapter>(cacheKey); | ||||||
|  |  | ||||||
|  |             if (cachedData is not null) { return cachedData; } | ||||||
|  |  | ||||||
|  |             var role = await repository.FindByIdAsync(_id); | ||||||
|  |  | ||||||
|  |             await cacheProvider.SetAsync(cacheKey, role); | ||||||
|  |  | ||||||
|  |             return role; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         /// <summary> | ||||||
|  |         /// Gets all the roles. | ||||||
|  |         /// </summary> | ||||||
|  |         /// <returns>A <see cref="{Task{IEnumerbale{RoleAdapter}}}"/> representing | ||||||
|  |         /// the asynchronous execution of the service.</returns> | ||||||
|  |         public async ValueTask<IEnumerable<RoleAdapter>> GetAllRoles(CancellationToken cancellationToken) | ||||||
|  |         { | ||||||
|  |             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllRoles"); | ||||||
|  |             var cachedData = await cacheProvider.GetAsync<IEnumerable<RoleAdapter>>(cacheKey) ?? []; | ||||||
|  |  | ||||||
|  |             if (cachedData.Any()) return cachedData; | ||||||
|  |  | ||||||
|  |             var roles = await repository.AsQueryable(); | ||||||
|  |  | ||||||
|  |             await cacheProvider.SetAsync(cacheKey, roles); | ||||||
|  |  | ||||||
|  |             return roles; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         /// <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>A <see cref="{Task{RoleAdapter}}"/> representing | ||||||
|  |         /// the asynchronous execution of the service.</returns> | ||||||
|  |         public async ValueTask<RoleAdapter> ChangeRoleStatus(string id, Core.Blueprint.Mongo.StatusEnum newStatus, CancellationToken cancellationToken) | ||||||
|  |         { | ||||||
|  |             var entity = await repository.FindByIdAsync(id); | ||||||
|  |             entity.Status = newStatus; | ||||||
|  |  | ||||||
|  |             await repository.ReplaceOneAsync(entity); | ||||||
|  |  | ||||||
|  |             return entity; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         /// <summary> | ||||||
|  |         /// Updates a Role by id. | ||||||
|  |         /// </summary> | ||||||
|  |         /// <param name="entity">The Role to be updated.</param> | ||||||
|  |         /// <param name="id">The Role identifier.</param> | ||||||
|  |         /// <returns>A <see cref="{Task{RoleAdapter}}"/> representing | ||||||
|  |         /// the asynchronous execution of the service.</returns> | ||||||
|  |         public async ValueTask<RoleAdapter> UpdateRole(RoleAdapter entity, CancellationToken cancellationToken) | ||||||
|  |         { | ||||||
|  |             await repository.ReplaceOneAsync(entity); | ||||||
|  |  | ||||||
|  |             return entity; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         /// <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> | ||||||
|  |         public async ValueTask<RoleAdapter> AddApplicationToRole(string roleId, ApplicationsEnum application, CancellationToken cancellationToken) | ||||||
|  |         { | ||||||
|  |             var role = await repository.FindOneAsync( | ||||||
|  |                 u => u.Id == roleId && | ||||||
|  |                 u.Status == Core.Blueprint.Mongo.StatusEnum.Active); | ||||||
|  |  | ||||||
|  |             var updatedApplications = role.Applications.Append(application).Distinct().ToArray(); | ||||||
|  |             role.Applications = updatedApplications; | ||||||
|  |  | ||||||
|  |             await repository.ReplaceOneAsync(role); | ||||||
|  |  | ||||||
|  |             return role; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         /// <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> | ||||||
|  |         public async ValueTask<RoleAdapter> RemoveApplicationFromRole(string roleId, ApplicationsEnum application, CancellationToken cancellationToken) | ||||||
|  |         { | ||||||
|  |             var role = await repository.FindOneAsync( | ||||||
|  |                 u => u.Id == roleId && | ||||||
|  |                 u.Status == Core.Blueprint.Mongo.StatusEnum.Active); | ||||||
|  |  | ||||||
|  |             var updatedApplications = role.Applications | ||||||
|  |                     ?.Where(c => c != application) | ||||||
|  |                     .ToArray(); | ||||||
|  |  | ||||||
|  |             role.Applications = updatedApplications; | ||||||
|  |  | ||||||
|  |             await repository.ReplaceOneAsync(role); | ||||||
|  |  | ||||||
|  |             return role; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -1,251 +0,0 @@ | |||||||
| // *********************************************************************** |  | ||||||
| // <copyright file="RoleService.cs"> |  | ||||||
| //     AgileWebs |  | ||||||
| // </copyright> |  | ||||||
| // *********************************************************************** |  | ||||||
| using Core.Thalos.Adapters; |  | ||||||
| using Core.Thalos.Adapters.Common.Constants; |  | ||||||
| using Core.Thalos.Adapters.Common.Enums; |  | ||||||
| using Core.Thalos.Domain.Contexts.Onboarding.Mappers; |  | ||||||
| using Core.Thalos.Domain.Contexts.Onboarding.Request; |  | ||||||
| using Core.Thalos.Infraestructure.Caching.Configs; |  | ||||||
| using Core.Thalos.Infraestructure.Caching.Contracts; |  | ||||||
| using Core.Thalos.Provider.Contracts; |  | ||||||
| using LSA.Core.Dapper.Service.Caching; |  | ||||||
| using Microsoft.AspNetCore.Http; |  | ||||||
| using Microsoft.Extensions.Logging; |  | ||||||
| using Microsoft.Extensions.Options; |  | ||||||
| using MongoDB.Bson; |  | ||||||
| using MongoDB.Driver; |  | ||||||
|  |  | ||||||
| namespace Core.Thalos.Provider.Providers.Onboarding |  | ||||||
| { |  | ||||||
|     /// <summary> |  | ||||||
|     /// Handles all services and business rules related to <see cref="RoleAdapter"/>. |  | ||||||
|     /// </summary> |  | ||||||
|     public class RoleService(ILogger<RoleService> logger, IHttpContextAccessor httpContextAccessor, ICacheService cacheService, |  | ||||||
|         IOptions<CacheSettings> cacheSettings, IMongoDatabase database) : IRoleService |  | ||||||
|     { |  | ||||||
|         private readonly CacheSettings _cacheSettings = cacheSettings.Value; |  | ||||||
|  |  | ||||||
|         /// <summary> |  | ||||||
|         /// Creates a new Role. |  | ||||||
|         /// </summary> |  | ||||||
|         /// <param name="entity">The Role to be created.</param> |  | ||||||
|         /// <returns>A <see cref="{Task{RoleAdapter}}"/> representing |  | ||||||
|         /// the asynchronous execution of the service.</returns> |  | ||||||
|         public async Task<RoleAdapter> CreateRoleService(RoleRequest newRole) |  | ||||||
|         { |  | ||||||
|             try |  | ||||||
|             { |  | ||||||
|                 var entity = newRole.ToAdapter(httpContextAccessor); |  | ||||||
|                 await database.GetCollection<RoleAdapter>(CollectionNames.Role).InsertOneAsync(entity); |  | ||||||
|                 entity.Id = (entity as dynamic ?? "").Id.ToString(); |  | ||||||
|  |  | ||||||
|                 return entity; |  | ||||||
|             } |  | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, $"CreateRoleService: Error in getting data - {ex.Message}"); |  | ||||||
|                 throw new Exception(ex.Message, ex); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         /// <summary> |  | ||||||
|         /// Gets an Role by identifier. |  | ||||||
|         /// </summary> |  | ||||||
|         /// <param name="id">The Role identifier.</param> |  | ||||||
|         /// <returns>A <see cref="{Task{RoleAdapter}}"/> representing |  | ||||||
|         /// the asynchronous execution of the service.</returns> |  | ||||||
|         public async Task<RoleAdapter> GetRoleByIdService(string id) |  | ||||||
|         { |  | ||||||
|             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetRoleByIdService", id); |  | ||||||
|             var cachedData = await cacheService.GetAsync<RoleAdapter>(cacheKey); |  | ||||||
|  |  | ||||||
|             if (cachedData is not null) { return cachedData; } |  | ||||||
|             try |  | ||||||
|             { |  | ||||||
|                 var filter = Builders<RoleAdapter>.Filter.And( |  | ||||||
|                         Builders<RoleAdapter>.Filter.Eq("_id", ObjectId.Parse(id)), |  | ||||||
|                         Builders<RoleAdapter>.Filter.Eq("status", StatusEnum.Active.ToString()) |  | ||||||
|                     ); |  | ||||||
|  |  | ||||||
|                 var role = await database.GetCollection<RoleAdapter>(CollectionNames.Role) |  | ||||||
|                 .Find(filter) |  | ||||||
|                             .FirstOrDefaultAsync(); |  | ||||||
|  |  | ||||||
|                 var cacheDuration = CacheHelper.GetCacheDuration(_cacheSettings); |  | ||||||
|  |  | ||||||
|                 await cacheService.SetAsync(cacheKey, role, cacheDuration); |  | ||||||
|  |  | ||||||
|                 return role; |  | ||||||
|             } |  | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, $"GetRoleByIdService: Error in getting data - {ex.Message}"); |  | ||||||
|                 throw new Exception(ex.Message, ex); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         /// <summary> |  | ||||||
|         /// Gets all the roles. |  | ||||||
|         /// </summary> |  | ||||||
|         /// <returns>A <see cref="{Task{IEnumerbale{RoleAdapter}}}"/> representing |  | ||||||
|         /// the asynchronous execution of the service.</returns> |  | ||||||
|         public async Task<IEnumerable<RoleAdapter>> GetAllRolesService() |  | ||||||
|         { |  | ||||||
|             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllRolesService"); |  | ||||||
|             var cachedData = await cacheService.GetAsync<IEnumerable<RoleAdapter>>(cacheKey) ?? []; |  | ||||||
|  |  | ||||||
|             if (cachedData.Any()) return cachedData; |  | ||||||
|             try |  | ||||||
|             { |  | ||||||
|                 var filter = Builders<RoleAdapter>.Filter.Eq("status", StatusEnum.Active.ToString()); |  | ||||||
|  |  | ||||||
|                 var roles = await database.GetCollection<RoleAdapter>(CollectionNames.Role) |  | ||||||
|                                 .Find(filter) |  | ||||||
|                                 .ToListAsync(); |  | ||||||
|  |  | ||||||
|                 var cacheDuration = CacheHelper.GetCacheDuration(_cacheSettings); |  | ||||||
|  |  | ||||||
|                 await cacheService.SetAsync(cacheKey, roles, cacheDuration); |  | ||||||
|  |  | ||||||
|                 return roles; |  | ||||||
|             } |  | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, $"GetAllRolesService: Error in getting data - {ex.Message}"); |  | ||||||
|                 throw new Exception(ex.Message, ex); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         /// <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>A <see cref="{Task{RoleAdapter}}"/> representing |  | ||||||
|         /// the asynchronous execution of the service.</returns> |  | ||||||
|         public async Task<RoleAdapter> ChangeRoleStatusService(string id, StatusEnum newStatus) |  | ||||||
|         { |  | ||||||
|             try |  | ||||||
|             { |  | ||||||
|                 var filter = Builders<RoleAdapter>.Filter |  | ||||||
|                     .Eq("_id", ObjectId.Parse(id)); |  | ||||||
|  |  | ||||||
|                 var update = Builders<RoleAdapter>.Update |  | ||||||
|                             //.Set(v => v.Status, newStatus) |  | ||||||
|                             .Set(v => v.UpdatedBy, Helper.GetEmail(httpContextAccessor)) |  | ||||||
|                             .Set(v => v.UpdatedAt, DateTime.UtcNow); |  | ||||||
|  |  | ||||||
|                 await database.GetCollection<RoleAdapter>(CollectionNames.Role).UpdateOneAsync(filter, update); |  | ||||||
|  |  | ||||||
|                 var updatedRole = await database.GetCollection<RoleAdapter>(CollectionNames.Role) |  | ||||||
|                                     .Find(filter) |  | ||||||
|                                     .FirstOrDefaultAsync(); |  | ||||||
|  |  | ||||||
|                 return updatedRole; |  | ||||||
|             } |  | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, $"ChangeRoleStatusService: Error in getting data - {ex.Message}"); |  | ||||||
|                 throw new Exception(ex.Message, ex); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         /// <summary> |  | ||||||
|         /// Updates a Role by id. |  | ||||||
|         /// </summary> |  | ||||||
|         /// <param name="entity">The Role to be updated.</param> |  | ||||||
|         /// <param name="id">The Role identifier.</param> |  | ||||||
|         /// <returns>A <see cref="{Task{RoleAdapter}}"/> representing |  | ||||||
|         /// the asynchronous execution of the service.</returns> |  | ||||||
|         public async Task<RoleAdapter> UpdateRoleService(RoleAdapter entity, string id) |  | ||||||
|         { |  | ||||||
|             try |  | ||||||
|             { |  | ||||||
|                 var filter = Builders<RoleAdapter>.Filter |  | ||||||
|                     .Eq("_id", ObjectId.Parse(id)); |  | ||||||
|  |  | ||||||
|                 var update = Builders<RoleAdapter>.Update |  | ||||||
|                             .Set(v => v.Name, entity.Name) |  | ||||||
|                             .Set(v => v.Description, entity.Description) |  | ||||||
|                             .Set(v => v.Applications, entity.Applications) |  | ||||||
|                             .Set(v => v.Modules, entity.Modules) |  | ||||||
|                             .Set(v => v.Permissions, entity.Permissions) |  | ||||||
|                             .Set(v => v.Status, entity.Status) |  | ||||||
|                             .Set(v => v.UpdatedBy, Helper.GetEmail(httpContextAccessor)) |  | ||||||
|                             .Set(v => v.UpdatedAt, DateTime.UtcNow); |  | ||||||
|  |  | ||||||
|                 await database.GetCollection<RoleAdapter>(CollectionNames.Role).UpdateOneAsync(filter, update); |  | ||||||
|  |  | ||||||
|                 var updatedRole = await database.GetCollection<RoleAdapter>(CollectionNames.Role) |  | ||||||
|                                     .Find(filter) |  | ||||||
|                                     .FirstOrDefaultAsync(); |  | ||||||
|  |  | ||||||
|                 return updatedRole; |  | ||||||
|             } |  | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, $"UpdateRoleService: Error in getting data - {ex.Message}"); |  | ||||||
|                 throw new Exception(ex.Message, ex); |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         /// <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> |  | ||||||
|         public async Task<RoleAdapter> AddApplicationToRoleService(string roleId, ApplicationsEnum application) |  | ||||||
|         { |  | ||||||
|             try |  | ||||||
|             { |  | ||||||
|                 var filter = Builders<RoleAdapter>.Filter.Eq("_id", ObjectId.Parse(roleId)); |  | ||||||
|                 var update = Builders<RoleAdapter>.Update.AddToSet(r => r.Applications, application); |  | ||||||
|  |  | ||||||
|                 await database.GetCollection<RoleAdapter>(CollectionNames.Role).UpdateOneAsync(filter, update); |  | ||||||
|  |  | ||||||
|                 var updatedRole = await database.GetCollection<RoleAdapter>(CollectionNames.Role) |  | ||||||
|                                     .Find(filter) |  | ||||||
|                                     .FirstOrDefaultAsync(); |  | ||||||
|                 return updatedRole; |  | ||||||
|             } |  | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, $"AddApplicationToRoleService: Error in getting data - {ex.Message}"); |  | ||||||
|                 throw new Exception(ex.Message, ex); |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         /// <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> |  | ||||||
|         public async Task<RoleAdapter> RemoveApplicationFromRoleService(string roleId, ApplicationsEnum application) |  | ||||||
|         { |  | ||||||
|             try |  | ||||||
|             { |  | ||||||
|                 var filter = Builders<RoleAdapter>.Filter.Eq("_id", ObjectId.Parse(roleId)); |  | ||||||
|                 var update = Builders<RoleAdapter>.Update.Pull(r => r.Applications, application); |  | ||||||
|  |  | ||||||
|                 await database.GetCollection<RoleAdapter>(CollectionNames.Role).UpdateOneAsync(filter, update); |  | ||||||
|  |  | ||||||
|                 var updatedRole = await database.GetCollection<RoleAdapter>(CollectionNames.Role) |  | ||||||
|                                     .Find(filter) |  | ||||||
|                                     .FirstOrDefaultAsync(); |  | ||||||
|                 return updatedRole; |  | ||||||
|             } |  | ||||||
|             catch (Exception ex) |  | ||||||
|             { |  | ||||||
|                 logger.LogError(ex, $"RemoveApplicationFromRoleService: Error in getting data - {ex.Message}"); |  | ||||||
|                 throw new Exception(ex.Message, ex); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -64,9 +64,9 @@ namespace Core.Thalos.Provider | |||||||
|             services.AddHttpContextAccessor(); |             services.AddHttpContextAccessor(); | ||||||
|  |  | ||||||
|             services.AddScoped<IUserProvider, UserProvider>(); |             services.AddScoped<IUserProvider, UserProvider>(); | ||||||
|             services.AddScoped<IRoleService, RoleService>(); |             services.AddScoped<IRoleProvider, RoleProvider>(); | ||||||
|             services.AddScoped<IPermissionService, PermissionService>(); |             services.AddScoped<IPermissionProvider, PermissionProvider>(); | ||||||
|             services.AddScoped<IPermissionService, PermissionService>(); |             services.AddScoped<IPermissionProvider, PermissionProvider>(); | ||||||
|             services.AddScoped<IModuleProvider, ModuleProvider>(); |             services.AddScoped<IModuleProvider, ModuleProvider>(); | ||||||
|             return services; |             return services; | ||||||
|         } |         } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Oscar Morales
					Oscar Morales