AppMenu : Implement Sidebar Menu with Permission-based Access Control #112
@ -1,11 +1,14 @@
|
||||
using Marco.Pms.Model.AppMenu;
|
||||
using Marco.Pms.Model.Dtos.AppMenu;
|
||||
using Marco.Pms.Model.ViewModels.AppMenu;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using static System.Collections.Specialized.BitVector32;
|
||||
|
||||
namespace Marco.Pms.CacheHelper
|
||||
{
|
||||
@ -46,7 +49,8 @@ namespace Marco.Pms.CacheHelper
|
||||
|
||||
var update = Builders<MenuSection>.Update
|
||||
.Set(s => s.Header, updatedSection.Header)
|
||||
.Set(s => s.Title, updatedSection.Title);
|
||||
.Set(s => s.Title, updatedSection.Title)
|
||||
.Set(s => s.Items, updatedSection.Items);
|
||||
|
||||
var result = await _collection.UpdateOneAsync(filter, update);
|
||||
|
||||
@ -104,9 +108,10 @@ namespace Marco.Pms.CacheHelper
|
||||
.Set("Items.$.Icon", updatedItem.Icon)
|
||||
.Set("Items.$.Available", updatedItem.Available)
|
||||
.Set("Items.$.Link", updatedItem.Link)
|
||||
.Set("Items.$.PermissionKey",updatedItem.PermissionKey);
|
||||
.Set("Items.$.PermissionKeys", updatedItem.PermissionKeys); // <-- updated
|
||||
|
||||
var result = await _collection.UpdateOneAsync(filter, update);
|
||||
|
||||
if (result.ModifiedCount > 0)
|
||||
{
|
||||
// Re-fetch section and return the updated item
|
||||
@ -172,7 +177,7 @@ namespace Marco.Pms.CacheHelper
|
||||
.Set("Items.$[item].Submenu.$[sub].Text", updatedSub.Text)
|
||||
.Set("Items.$[item].Submenu.$[sub].Available", updatedSub.Available)
|
||||
.Set("Items.$[item].Submenu.$[sub].Link", updatedSub.Link)
|
||||
.Set("Items.$[item].Submenu.$[sub].PermissionKey", updatedSub.PermissionKey);
|
||||
.Set("Items.$[item].Submenu.$[sub].PermissionKeys", updatedSub.PermissionKeys); // <-- updated
|
||||
|
||||
var options = new UpdateOptions { ArrayFilters = arrayFilters };
|
||||
|
||||
@ -184,9 +189,9 @@ namespace Marco.Pms.CacheHelper
|
||||
var updatedSection = await _collection.Find(x => x.Id == sectionId).FirstOrDefaultAsync();
|
||||
|
||||
var subItem = updatedSection?.Items
|
||||
.FirstOrDefault(i => i.Id.ToString() == itemId.ToString())?
|
||||
.FirstOrDefault(i => i.Id == itemId)?
|
||||
.Submenu
|
||||
.FirstOrDefault(s => s.Id.ToString() == subItemId.ToString());
|
||||
.FirstOrDefault(s => s.Id == subItemId);
|
||||
|
||||
return subItem;
|
||||
}
|
||||
@ -197,5 +202,13 @@ namespace Marco.Pms.CacheHelper
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public async Task<List<MenuSection>> GetAllMenuSectionsAsync()
|
||||
{
|
||||
return await _collection.Find(_ => true).ToListAsync();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,4 @@
|
||||
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
|
||||
namespace Marco.Pms.Model.AppMenu
|
||||
@ -27,9 +26,10 @@ namespace Marco.Pms.Model.AppMenu
|
||||
|
||||
public string? Link { get; set; }
|
||||
|
||||
public string PermissionKey { get; set; } = string.Empty;
|
||||
// Changed from string → List<string>
|
||||
public List<string> PermissionKeys { get; set; } = new List<string>();
|
||||
|
||||
public List<SubMenuItem> Submenu { get; set; } = new List<SubMenuItem> ();
|
||||
public List<SubMenuItem> Submenu { get; set; } = new List<SubMenuItem>();
|
||||
}
|
||||
|
||||
public class SubMenuItem
|
||||
@ -38,12 +38,14 @@ namespace Marco.Pms.Model.AppMenu
|
||||
[BsonRepresentation(BsonType.String)]
|
||||
public Guid Id { get; set; } = Guid.NewGuid();
|
||||
|
||||
public string? Text { get; set; }
|
||||
public string? Text { get; set; }
|
||||
public bool Available { get; set; } = true;
|
||||
|
||||
public string Link { get; set; } = string.Empty;
|
||||
|
||||
public string PermissionKey { get; set; } = string.Empty;
|
||||
// Changed from string → List<string>
|
||||
public List<string> PermissionKeys { get; set; } = new List<string>();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -10,7 +10,6 @@ namespace Marco.Pms.Model.Dtos.AppMenu
|
||||
{
|
||||
public class MenuSectionDto
|
||||
{
|
||||
|
||||
public string? Header { get; set; }
|
||||
public string? Title { get; set; }
|
||||
public List<MenuItemDto> Items { get; set; } = new List<MenuItemDto>();
|
||||
@ -18,26 +17,26 @@ namespace Marco.Pms.Model.Dtos.AppMenu
|
||||
|
||||
public class MenuItemDto
|
||||
{
|
||||
|
||||
public string? Text { get; set; }
|
||||
public string? Icon { get; set; }
|
||||
public bool Available { get; set; } = true;
|
||||
|
||||
public string? Link { get; set; }
|
||||
|
||||
public string PermissionKey { get; set; } = string.Empty;
|
||||
// Changed from string → List<string>
|
||||
public List<string> PermissionKeys { get; set; } = new List<string>();
|
||||
|
||||
public List<SubMenuItemDto> Submenu { get; set; } = new List<SubMenuItemDto>();
|
||||
}
|
||||
|
||||
public class SubMenuItemDto
|
||||
{
|
||||
|
||||
|
||||
public string? Text { get; set; }
|
||||
public bool Available { get; set; } = true;
|
||||
|
||||
public string Link { get; set; } = string.Empty;
|
||||
|
||||
public string PermissionKey { get; set; } = string.Empty;
|
||||
// Changed from string → List<string>
|
||||
public List<string> PermissionKeys { get; set; } = new List<string>();
|
||||
}
|
||||
}
|
||||
}
|
28
Marco.Pms.Model/ViewModels/AppMenu/AppMenuVM.cs
Normal file
28
Marco.Pms.Model/ViewModels/AppMenu/AppMenuVM.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Marco.Pms.Model.ViewModels.AppMenu
|
||||
{
|
||||
public class MenuSectionVm
|
||||
{
|
||||
public string? Header { get; set; }
|
||||
public string? Title { get; set; }
|
||||
public List<MenuItemVm> Items { get; set; } = new();
|
||||
}
|
||||
|
||||
public class MenuItemVm
|
||||
{
|
||||
public string? Text { get; set; }
|
||||
public string? Icon { get; set; }
|
||||
public bool Available { get; set; } = true;
|
||||
public string? Link { get; set; }
|
||||
public List<SubMenuItemVm> Submenu { get; set; } = new();
|
||||
}
|
||||
|
||||
public class SubMenuItemVm
|
||||
{
|
||||
public string? Text { get; set; }
|
||||
public bool Available { get; set; } = true;
|
||||
public string Link { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
@ -32,8 +32,10 @@ namespace Marco.Pms.Services.Controllers
|
||||
private readonly SideBarMenu _sideBarMenuHelper;
|
||||
private readonly IMapper _mapper;
|
||||
private readonly ILoggingService _logger;
|
||||
private readonly PermissionServices _permissions;
|
||||
|
||||
public AppMenuController(EmployeeHelper employeeHelper, IProjectServices projectServices, UserHelper userHelper, RolesHelper rolesHelper, SideBarMenu sideBarMenuHelper, IMapper mapper, ILoggingService logger) {
|
||||
public AppMenuController(EmployeeHelper employeeHelper, IProjectServices projectServices, UserHelper userHelper, RolesHelper rolesHelper, SideBarMenu sideBarMenuHelper, IMapper mapper, ILoggingService logger, PermissionServices permissions = null)
|
||||
{
|
||||
|
||||
_userHelper = userHelper;
|
||||
_employeeHelper = employeeHelper;
|
||||
@ -41,6 +43,7 @@ namespace Marco.Pms.Services.Controllers
|
||||
_sideBarMenuHelper = sideBarMenuHelper;
|
||||
_mapper = mapper;
|
||||
_logger = logger;
|
||||
_permissions = permissions;
|
||||
}
|
||||
|
||||
|
||||
@ -227,11 +230,11 @@ namespace Marco.Pms.Services.Controllers
|
||||
|
||||
|
||||
|
||||
|
||||
var menus = await _sideBarMenuHelper.GetAllMenuSectionsAsync();
|
||||
|
||||
|
||||
|
||||
return Ok(LoggedUser);
|
||||
return Ok(menus);
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user