Skip to content

Commit

Permalink
Merge branch 'dev' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
neozhu committed Sep 5, 2021
2 parents a05fe79 + 758e90b commit ce2bf00
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
19 changes: 12 additions & 7 deletions src/SmartAdmin.WebUI/Models/NavigationModel.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using SmartAdmin.WebUI.Extensions;

namespace SmartAdmin.WebUI.Models
Expand All @@ -17,20 +18,24 @@ public static class NavigationModel
public static SmartNavigation Seed => BuildNavigation();
public static SmartNavigation Full => BuildNavigation(seedOnly: false);

private static SmartNavigation BuildNavigation(bool seedOnly = true)
private static SmartNavigation BuildNavigation(bool seedOnly = true, Expression<Func<ListItem, bool>> criteria = null)
{
var jsonText = File.ReadAllText("nav.json");
var navigation = NavigationBuilder.FromJson(jsonText);
var menu = FillProperties(navigation.Lists, seedOnly);
var menu = FillProperties(navigation.Lists, seedOnly,null, criteria);

return new SmartNavigation(menu);
}
public static SmartNavigation GetNavigation(Expression<Func<ListItem, bool>> criteria)
{
return BuildNavigation(seedOnly: false, criteria:criteria);
}

private static List<ListItem> FillProperties(IEnumerable<ListItem> items, bool seedOnly, ListItem parent = null)
private static List<ListItem> FillProperties(IEnumerable<ListItem> items, bool seedOnly, ListItem parent = null, Expression<Func<ListItem, bool>> criteria = null)
{
var result = new List<ListItem>();

foreach (var item in items)
foreach (var item in items.Where(criteria.Compile()))
{
item.Text ??= item.Title;
item.Tags = string.Concat(parent?.Tags, Space, item.Title.ToLower()).Trim();
Expand All @@ -45,12 +50,12 @@ private static List<ListItem> FillProperties(IEnumerable<ListItem> items, bool s
? $"nav.{item.Title.ToLower().Replace(Space, Underscore)}"
: $"{parent.I18n}_{item.Title.ToLower().Replace(Space, Underscore)}";
item.Type = parent == null ? item.Href == null ? ItemType.Category : ItemType.Single : item.Items.Any() ? ItemType.Parent : ItemType.Child;
item.Items = FillProperties(item.Items, seedOnly, item);
item.Items = FillProperties(item.Items, seedOnly, item, criteria);

if (item.Href.IsVoid() && item.Items.Any())
item.Type = ItemType.Sibling;

if (!seedOnly || item.ShowOnSeed)
if ((!seedOnly || item.ShowOnSeed))
result.Add(item);
}

Expand Down
26 changes: 24 additions & 2 deletions src/SmartAdmin.WebUI/ViewComponents/NavigationViewComponent.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CleanArchitecture.Razor.Application.Common.Interfaces;
using CleanArchitecture.Razor.Application.Common.Interfaces.Identity;
using CleanArchitecture.Razor.Infrastructure.Identity;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SmartAdmin.WebUI.Models;

namespace SmartAdmin.WebUI.ViewComponents
{
public class NavigationViewComponent : ViewComponent
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly ICurrentUserService _currentUserService;
private readonly IIdentityService _identityService;
private readonly string[] _roles;
public NavigationViewComponent(
UserManager<ApplicationUser> userManager,
ICurrentUserService currentUserService,
IIdentityService identityService
)
{
_userManager = userManager;
_currentUserService = currentUserService;
_identityService = identityService;
var userId = _currentUserService.UserId;
_roles = _userManager.Users.Where(x=>x.Id== userId).Include(x=>x.UserRoles).ThenInclude(x=>x.Role).SelectMany(x=>x.UserRoles).Select(x=>x.Role.Name).ToArray();
}
public IViewComponentResult Invoke()
{
var items = NavigationModel.Full;

var items = NavigationModel.GetNavigation(x=>!x.Roles.Any() || (x.Roles.Any() && _roles.Any() && x.Roles.Where(x=>_roles.Contains(x)).Any()) );

return View(items);
}
Expand Down
18 changes: 5 additions & 13 deletions src/SmartAdmin.WebUI/nav.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{
"title": "Application Intel",
"icon": "fal fa-info-circle",
"roles": [],
"roles": [ "Admin", "Basic" ],
"items": [
{
"title": "Analytics Dashboard",
Expand Down Expand Up @@ -106,9 +106,7 @@
{
"title": "Workflow",
"icon": "fal fa-clipboard-check",
"roles": [
"Administrator"
],
"roles": [ "Admin", "Basic" ],
"items": [
{
"title": "Approval",
Expand All @@ -125,9 +123,7 @@
{
"title": "Master Data",
"icon": "fal fa-cubes",
"roles": [
"Administrator"
],
"roles": [ "Admin", "Basic" ],
"items": [
{
"title": "Customers",
Expand All @@ -144,9 +140,7 @@
{
"title": "Documents",
"icon": "fal fa-paperclip",
"roles": [
"Administrator"
],
"roles": [ "Admin", "Basic" ],
"items": [
{
"title": "Documents",
Expand All @@ -173,9 +167,7 @@
{
"title": "Authorization",
"icon": "fal fa-shield-alt",
"roles": [
"Administrator"
],
"roles": [ "Admin" ],
"items": [
{
"title": "Users",
Expand Down

0 comments on commit ce2bf00

Please sign in to comment.