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.Entitlements;
|
||||
using Marco.Pms.Model.Utilities;
|
||||
using Marco.Pms.Model.ViewModels.AppMenu;
|
||||
using Marco.Pms.Services.Service;
|
||||
using Marco.Pms.Services.Service.ServiceInterfaces;
|
||||
using MarcoBMS.Services.Helpers;
|
||||
@ -15,6 +16,7 @@ using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using MongoDB.Driver;
|
||||
using Org.BouncyCastle.Asn1.Ocsp;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using static System.Collections.Specialized.BitVector32;
|
||||
|
||||
@ -34,7 +36,7 @@ namespace Marco.Pms.Services.Controllers
|
||||
private readonly ILoggingService _logger;
|
||||
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;
|
||||
@ -82,7 +84,7 @@ namespace Marco.Pms.Services.Controllers
|
||||
}
|
||||
|
||||
[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)
|
||||
{
|
||||
@ -92,7 +94,7 @@ namespace Marco.Pms.Services.Controllers
|
||||
var UpdatedMenuSection = _mapper.Map<MenuSection>(updatedSection);
|
||||
try
|
||||
{
|
||||
UpdatedMenuSection = await _sideBarMenuHelper.UpdateMenuSectionAsync(sectionId, UpdatedMenuSection);
|
||||
UpdatedMenuSection = await _sideBarMenuHelper.UpdateMenuSectionAsync(sectionId, UpdatedMenuSection);
|
||||
|
||||
if (UpdatedMenuSection == null)
|
||||
return NotFound(ApiResponse<object>.ErrorResponse("Menu section not found", 404));
|
||||
@ -200,7 +202,7 @@ namespace Marco.Pms.Services.Controllers
|
||||
|
||||
|
||||
[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)
|
||||
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid input", 400));
|
||||
@ -226,18 +228,84 @@ namespace Marco.Pms.Services.Controllers
|
||||
[HttpGet("sidebar/menu-section")]
|
||||
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();
|
||||
|
||||
foreach (var menu in menus)
|
||||
{
|
||||
var allowedItems = new List<MenuItem>();
|
||||
|
||||
foreach (var item in menu.Items)
|
||||
{
|
||||
bool isAllowed = false;
|
||||
|
||||
return Ok(menus);
|
||||
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