using Microsoft.AspNetCore.Authorization; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Threading.Tasks; using Lsw.Abp.AspnetCore.Components.Web.AntDesignTheme.PageToolbars; namespace Lsw.Abp.AspnetCore.Components.Web.AntDesignTheme.PageToolbars; public class SimplePageToolbarContributor : IPageToolbarContributor { public Type ComponentType { get; } public Dictionary Arguments { get; set; } public int Order { get; } public string RequiredPolicyName { get; } public SimplePageToolbarContributor( Type componentType, Dictionary arguments = null, int order = 0, string requiredPolicyName = null) { ComponentType = componentType; Arguments = arguments; Order = order; RequiredPolicyName = requiredPolicyName; } public async Task ContributeAsync(PageToolbarContributionContext context) { if (await ShouldAddComponentAsync(context)) { context.Items.Add(new PageToolbarItem(ComponentType, Arguments, Order)); } } protected virtual async Task ShouldAddComponentAsync(PageToolbarContributionContext context) { if (RequiredPolicyName != null) { var authorizationService = context.ServiceProvider.GetRequiredService(); if (!await authorizationService.IsGrantedAsync(RequiredPolicyName)) { return false; } } return true; } }