From 4660c50ece48978e2066489a01f946e145765959 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Fri, 24 May 2019 09:09:10 +0300 Subject: [PATCH] widget permissions & refactor --- .../Dashboard/DashboardViewModel.cs | 14 +++++++++++ .../Components/Dashboard/Default.cshtml | 17 ++++++++----- .../Mvc/UI/Widgets/WidgetDefinition.cs | 20 ++++++++++++--- .../Mvc/UI/Widgets/WidgetRenderer.cs | 4 +-- .../Pages/widgets/RoleListWidget.js | 19 +++++++------- .../Pages/widgets/UserCountWidget.js | 12 ++++----- .../Widgets/WidgetDefinitionProvider.cs | 25 ++++++++++++------- 7 files changed, 76 insertions(+), 35 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Dashboards/Volo/Abp/AspNetCore/Mvc/UI/Dashboards/Components/Dashboard/DashboardViewModel.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Dashboards/Volo/Abp/AspNetCore/Mvc/UI/Dashboards/Components/Dashboard/DashboardViewModel.cs index 723fe46dcf..47405ae63c 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Dashboards/Volo/Abp/AspNetCore/Mvc/UI/Dashboards/Components/Dashboard/DashboardViewModel.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Dashboards/Volo/Abp/AspNetCore/Mvc/UI/Dashboards/Components/Dashboard/DashboardViewModel.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Microsoft.AspNetCore.Authorization; using Volo.Abp.AspNetCore.Mvc.UI.Widgets; namespace Volo.Abp.AspNetCore.Mvc.UI.Dashboards.Components.Dashboard @@ -22,5 +23,18 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Dashboards.Components.Dashboard { return Widgets.Single(d => d.Name.Equals(name)); } + + public async Task CheckPermissionsAsync(IAuthorizationService authorizationService, WidgetDefinition widget) + { + foreach (var permission in widget.RequiredPermissions) + { + if (!await authorizationService.IsGrantedAsync(permission)) + { + return false; + } + } + + return true; + } } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Dashboards/Volo/Abp/AspNetCore/Mvc/UI/Dashboards/Components/Dashboard/Default.cshtml b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Dashboards/Volo/Abp/AspNetCore/Mvc/UI/Dashboards/Components/Dashboard/Default.cshtml index 4cf357d830..89fecb905c 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Dashboards/Volo/Abp/AspNetCore/Mvc/UI/Dashboards/Components/Dashboard/Default.cshtml +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Dashboards/Volo/Abp/AspNetCore/Mvc/UI/Dashboards/Components/Dashboard/Default.cshtml @@ -1,5 +1,7 @@ -@using Microsoft.Extensions.Localization +@using Microsoft.AspNetCore.Authorization +@using Microsoft.Extensions.Localization @using Volo.Abp.AspNetCore.Mvc.UI.Widgets +@inject IAuthorizationService AuthorizationService @inject IWidgetRenderer WidgetRenderer @inject IStringLocalizerFactory localizer @model Volo.Abp.AspNetCore.Mvc.UI.Dashboards.Components.Dashboard.DashboardViewModel @@ -10,12 +12,15 @@ @foreach (var widgetConfiguration in Model.Dashboard.AvailableWidgets) { var widgetDefinition = Model.GetWidget(widgetConfiguration.WidgetName); - widgetDefinition.DefaultDimensions = widgetConfiguration.Dimensions ?? widgetDefinition.DefaultDimensions ?? new WidgetDimensions(6, 4); + if (await Model.CheckPermissionsAsync(AuthorizationService, widgetDefinition)) + { + widgetDefinition.DefaultDimensions = widgetConfiguration.Dimensions ?? widgetDefinition.DefaultDimensions ?? new WidgetDimensions(6, 4); - + - @await WidgetRenderer.RenderAsync(Component, widgetDefinition.Name) - + @await WidgetRenderer.RenderAsync(Component, widgetDefinition.Name) + + } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetDefinition.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetDefinition.cs index 2296cfb4e4..b1b404215e 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetDefinition.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetDefinition.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using JetBrains.Annotations; using Volo.Abp.Localization; @@ -29,16 +30,29 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Widgets [CanBeNull] public WidgetDimensions DefaultDimensions { get; set; } + public List RequiredPermissions { get; set; } + public WidgetDefinition( [NotNull] string name, - [NotNull] Type viewComponentType, [CanBeNull] ILocalizableString displayName, - [CanBeNull] WidgetDimensions defaultDimensions = null) + [NotNull] Type viewComponentType) { - DefaultDimensions = defaultDimensions; Name = Check.NotNullOrWhiteSpace(name, nameof(name)); ViewComponentType = Check.NotNull(viewComponentType, nameof(viewComponentType)); DisplayName = displayName ?? new FixedLocalizableString(name); + RequiredPermissions = new List(); + } + + public WidgetDefinition AddRequiredPermission(string permissionName) + { + RequiredPermissions.Add(permissionName); + return this; + } + + public WidgetDefinition SetDefaultDimension(int width, int height) + { + DefaultDimensions = new WidgetDimensions(width, height); + return this; } } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetRenderer.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetRenderer.cs index 2bb8c58fe9..821e53e853 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetRenderer.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetRenderer.cs @@ -17,9 +17,9 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Widgets public async Task RenderAsync(IViewComponentHelper componentHelper, string widgetName, object args = null) { - var componentType = _widgetOptions.Widgets.Single(w=>w.Name.Equals(widgetName)).ViewComponentType; + var widget = _widgetOptions.Widgets.Single(w=>w.Name.Equals(widgetName)); - return await componentHelper.InvokeAsync(componentType, args ?? new object()); + return await componentHelper.InvokeAsync(widget.ViewComponentType, args ?? new object()); } } } \ No newline at end of file diff --git a/samples/DashboardDemo/src/DashboardDemo.Web/Pages/widgets/RoleListWidget.js b/samples/DashboardDemo/src/DashboardDemo.Web/Pages/widgets/RoleListWidget.js index b2a6e23279..fdc0587f3d 100644 --- a/samples/DashboardDemo/src/DashboardDemo.Web/Pages/widgets/RoleListWidget.js +++ b/samples/DashboardDemo/src/DashboardDemo.Web/Pages/widgets/RoleListWidget.js @@ -1,14 +1,15 @@ (function ($) { var $container = $('#RoleListWidgetContainer'); + if ($container.length > 0) { + var _identityUserAppService = volo.abp.identity.identityRole; - var _identityUserAppService = volo.abp.identity.identityRole; + _identityUserAppService.getList({}).then(function (result) { + var html = ''; + for (var i = 0; i < result.items.length; i++) { + html += '
  • ' + result.items[i].name + '
  • '; + } - _identityUserAppService.getList({}).then(function (result) { - var html = ''; - for (var i = 0; i < result.items.length; i++) { - html += '
  • '+ result.items[i].name+'
  • '; - } - - $container.find('#RoleList').html(html); - }); + $container.find('#RoleList').html(html); + }); + } })(jQuery); diff --git a/samples/DashboardDemo/src/DashboardDemo.Web/Pages/widgets/UserCountWidget.js b/samples/DashboardDemo/src/DashboardDemo.Web/Pages/widgets/UserCountWidget.js index 08ee70a0e8..02e5dddcec 100644 --- a/samples/DashboardDemo/src/DashboardDemo.Web/Pages/widgets/UserCountWidget.js +++ b/samples/DashboardDemo/src/DashboardDemo.Web/Pages/widgets/UserCountWidget.js @@ -1,10 +1,10 @@ (function ($) { var $container = $('#UserCountWidgetContainer'); - - var _identityUserAppService = volo.abp.identity.identityUser; - - _identityUserAppService.getList({}).then(function(result) { - $container.find('#UserCount').text(result.items.length); - }); + if ($container.length > 0) { + var _identityUserAppService = volo.abp.identity.identityUser; + _identityUserAppService.getList({}).then(function(result) { + $container.find('#UserCount').text(result.items.length); + }); + } })(jQuery); diff --git a/samples/DashboardDemo/src/DashboardDemo.Web/Widgets/WidgetDefinitionProvider.cs b/samples/DashboardDemo/src/DashboardDemo.Web/Widgets/WidgetDefinitionProvider.cs index bd595d2a07..2df0bfdf99 100644 --- a/samples/DashboardDemo/src/DashboardDemo.Web/Widgets/WidgetDefinitionProvider.cs +++ b/samples/DashboardDemo/src/DashboardDemo.Web/Widgets/WidgetDefinitionProvider.cs @@ -2,6 +2,7 @@ using DashboardDemo.Localization.DashboardDemo; using DashboardDemo.Pages.widgets; using Volo.Abp.AspNetCore.Mvc.UI.Widgets; +using Volo.Abp.Identity; using Volo.Abp.Localization; namespace DashboardDemo.Widgets @@ -10,20 +11,26 @@ namespace DashboardDemo.Widgets { public static List GetDefinitions() { - return new List - { - new WidgetDefinition( + var userCountWidget = new WidgetDefinition( UserCountWidgetViewComponent.WidgetName, - typeof(UserCountWidgetViewComponent), LocalizableString.Create(UserCountWidgetViewComponent.DisplayName), - new WidgetDimensions(4,2) - ), - new WidgetDefinition( + typeof(UserCountWidgetViewComponent) + ) + .SetDefaultDimension(6, 3) + .AddRequiredPermission(IdentityPermissions.Users.Default); + + var roleListWidget = new WidgetDefinition( RoleListWidgetViewComponent.WidgetName, - typeof(RoleListWidgetViewComponent), LocalizableString.Create(RoleListWidgetViewComponent.DisplayName), - new WidgetDimensions(6,3) + typeof(RoleListWidgetViewComponent) ) + .SetDefaultDimension(6, 3) + .AddRequiredPermission(IdentityPermissions.Roles.Default); + + return new List + { + userCountWidget, + roleListWidget }; } }