added permission for fetch menu according feature permission
This commit is contained in:
parent
ff5f673475
commit
732cfbef3e
@ -6,6 +6,7 @@ using Marco.Pms.Model.Dtos.AppMenu;
|
|||||||
using Marco.Pms.Model.Employees;
|
using Marco.Pms.Model.Employees;
|
||||||
using Marco.Pms.Model.Entitlements;
|
using Marco.Pms.Model.Entitlements;
|
||||||
using Marco.Pms.Model.Utilities;
|
using Marco.Pms.Model.Utilities;
|
||||||
|
using Marco.Pms.Model.ViewModels.AppMenu;
|
||||||
using Marco.Pms.Services.Service;
|
using Marco.Pms.Services.Service;
|
||||||
using Marco.Pms.Services.Service.ServiceInterfaces;
|
using Marco.Pms.Services.Service.ServiceInterfaces;
|
||||||
using MarcoBMS.Services.Helpers;
|
using MarcoBMS.Services.Helpers;
|
||||||
@ -15,6 +16,7 @@ using Microsoft.AspNetCore.Http.HttpResults;
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
using Org.BouncyCastle.Asn1.Ocsp;
|
using Org.BouncyCastle.Asn1.Ocsp;
|
||||||
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using static System.Collections.Specialized.BitVector32;
|
using static System.Collections.Specialized.BitVector32;
|
||||||
|
|
||||||
@ -34,7 +36,7 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
private readonly ILoggingService _logger;
|
private readonly ILoggingService _logger;
|
||||||
private readonly PermissionServices _permissions;
|
private readonly PermissionServices _permissions;
|
||||||
|
|
||||||
public AppMenuController(EmployeeHelper employeeHelper, IProjectServices projectServices, UserHelper userHelper, RolesHelper rolesHelper, SideBarMenu sideBarMenuHelper, IMapper mapper, ILoggingService logger, PermissionServices permissions = null)
|
public AppMenuController(EmployeeHelper employeeHelper, IProjectServices projectServices, UserHelper userHelper, RolesHelper rolesHelper, SideBarMenu sideBarMenuHelper, IMapper mapper, ILoggingService logger, PermissionServices permissions)
|
||||||
{
|
{
|
||||||
|
|
||||||
_userHelper = userHelper;
|
_userHelper = userHelper;
|
||||||
@ -71,7 +73,7 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
return StatusCode(500, ApiResponse<object>.ErrorResponse("Server Error", ex, 500));
|
return StatusCode(500, ApiResponse<object>.ErrorResponse("Server Error", ex, 500));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sideMenuSection == null) {
|
if (sideMenuSection == null) {
|
||||||
_logger.LogWarning("Error Occurred while creating Menu");
|
_logger.LogWarning("Error Occurred while creating Menu");
|
||||||
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid MenuSection", 400));
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid MenuSection", 400));
|
||||||
}
|
}
|
||||||
@ -82,7 +84,7 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpPut("sidebar/menu-section/{sectionId}")]
|
[HttpPut("sidebar/menu-section/{sectionId}")]
|
||||||
public async Task<IActionResult> UpdateMenuSection(Guid sectionId,[FromBody] MenuSection updatedSection)
|
public async Task<IActionResult> UpdateMenuSection(Guid sectionId, [FromBody] MenuSection updatedSection)
|
||||||
{
|
{
|
||||||
if (sectionId == Guid.Empty || updatedSection == null)
|
if (sectionId == Guid.Empty || updatedSection == null)
|
||||||
{
|
{
|
||||||
@ -92,7 +94,7 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
var UpdatedMenuSection = _mapper.Map<MenuSection>(updatedSection);
|
var UpdatedMenuSection = _mapper.Map<MenuSection>(updatedSection);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
UpdatedMenuSection = await _sideBarMenuHelper.UpdateMenuSectionAsync(sectionId, UpdatedMenuSection);
|
UpdatedMenuSection = await _sideBarMenuHelper.UpdateMenuSectionAsync(sectionId, UpdatedMenuSection);
|
||||||
|
|
||||||
if (UpdatedMenuSection == null)
|
if (UpdatedMenuSection == null)
|
||||||
return NotFound(ApiResponse<object>.ErrorResponse("Menu section not found", 404));
|
return NotFound(ApiResponse<object>.ErrorResponse("Menu section not found", 404));
|
||||||
@ -142,7 +144,7 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
{
|
{
|
||||||
_logger.LogWarning("Error Occurred while Updating Menu Item");
|
_logger.LogWarning("Error Occurred while Updating Menu Item");
|
||||||
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid section ID, item ID, or menu item payload.", 400));
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid section ID, item ID, or menu item payload.", 400));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var sideMenuItem = _mapper.Map<MenuItem>(updatedMenuItem);
|
var sideMenuItem = _mapper.Map<MenuItem>(updatedMenuItem);
|
||||||
@ -200,7 +202,7 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
|
|
||||||
|
|
||||||
[HttpPut("sidebar/{sectionId}/items/{itemId}/subitems/{subItemId}")]
|
[HttpPut("sidebar/{sectionId}/items/{itemId}/subitems/{subItemId}")]
|
||||||
public async Task<IActionResult> UpdateSubmenuItem(Guid sectionId,Guid itemId,Guid subItemId,[FromBody] SubMenuItemDto updatedSubMenuItem)
|
public async Task<IActionResult> UpdateSubmenuItem(Guid sectionId, Guid itemId, Guid subItemId, [FromBody] SubMenuItemDto updatedSubMenuItem)
|
||||||
{
|
{
|
||||||
if (sectionId == Guid.Empty || itemId == Guid.Empty || subItemId == Guid.Empty || updatedSubMenuItem == null)
|
if (sectionId == Guid.Empty || itemId == Guid.Empty || subItemId == Guid.Empty || updatedSubMenuItem == null)
|
||||||
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid input", 400));
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid input", 400));
|
||||||
@ -226,18 +228,84 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
[HttpGet("sidebar/menu-section")]
|
[HttpGet("sidebar/menu-section")]
|
||||||
public async Task<IActionResult> GetAppSideBarMenu()
|
public async Task<IActionResult> GetAppSideBarMenu()
|
||||||
{
|
{
|
||||||
var LoggedUser = await _userHelper.GetCurrentUserAsync();
|
var loggedUser = await _userHelper.GetCurrentUserAsync();
|
||||||
|
var employeeId = Guid.Parse(loggedUser.Id);
|
||||||
|
|
||||||
|
|
||||||
var menus = await _sideBarMenuHelper.GetAllMenuSectionsAsync();
|
var menus = await _sideBarMenuHelper.GetAllMenuSectionsAsync();
|
||||||
|
|
||||||
|
foreach (var menu in menus)
|
||||||
|
{
|
||||||
|
var allowedItems = new List<MenuItem>();
|
||||||
|
|
||||||
return Ok(menus);
|
foreach (var item in menu.Items)
|
||||||
|
{
|
||||||
|
bool isAllowed = false;
|
||||||
|
|
||||||
|
if (item.PermissionKeys == null || !item.PermissionKeys.Any())
|
||||||
|
{
|
||||||
|
isAllowed = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
foreach (var pk in item.PermissionKeys)
|
||||||
|
{
|
||||||
|
if (Guid.TryParse(pk, out var permissionId))
|
||||||
|
{
|
||||||
|
if (await _permissions.HasPermission(employeeId, permissionId))
|
||||||
|
{
|
||||||
|
isAllowed = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isAllowed)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (item.Submenu != null && item.Submenu.Any())
|
||||||
|
{
|
||||||
|
var allowedSubmenus = new List<SubMenuItem>();
|
||||||
|
foreach (var sm in item.Submenu)
|
||||||
|
{
|
||||||
|
bool smAllowed = false;
|
||||||
|
if (sm.PermissionKeys == null || !sm.PermissionKeys.Any())
|
||||||
|
{
|
||||||
|
smAllowed = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
foreach (var pk in sm.PermissionKeys)
|
||||||
|
{
|
||||||
|
if (Guid.TryParse(pk, out var permissionId))
|
||||||
|
{
|
||||||
|
if (await _permissions.HasPermission(employeeId, permissionId))
|
||||||
|
{
|
||||||
|
smAllowed = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (smAllowed)
|
||||||
|
allowedSubmenus.Add(sm);
|
||||||
|
}
|
||||||
|
item.Submenu = allowedSubmenus;
|
||||||
|
}
|
||||||
|
|
||||||
|
allowedItems.Add(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
menu.Items = allowedItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(menus);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user