Browse Source

widget permissions & refactor

pull/1156/head
Yunus Emre Kalkan 7 years ago
parent
commit
4660c50ece
  1. 14
      framework/src/Volo.Abp.AspNetCore.Mvc.UI.Dashboards/Volo/Abp/AspNetCore/Mvc/UI/Dashboards/Components/Dashboard/DashboardViewModel.cs
  2. 17
      framework/src/Volo.Abp.AspNetCore.Mvc.UI.Dashboards/Volo/Abp/AspNetCore/Mvc/UI/Dashboards/Components/Dashboard/Default.cshtml
  3. 20
      framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetDefinition.cs
  4. 4
      framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetRenderer.cs
  5. 19
      samples/DashboardDemo/src/DashboardDemo.Web/Pages/widgets/RoleListWidget.js
  6. 12
      samples/DashboardDemo/src/DashboardDemo.Web/Pages/widgets/UserCountWidget.js
  7. 25
      samples/DashboardDemo/src/DashboardDemo.Web/Widgets/WidgetDefinitionProvider.cs

14
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.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Volo.Abp.AspNetCore.Mvc.UI.Widgets; using Volo.Abp.AspNetCore.Mvc.UI.Widgets;
namespace Volo.Abp.AspNetCore.Mvc.UI.Dashboards.Components.Dashboard 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)); return Widgets.Single(d => d.Name.Equals(name));
} }
public async Task<bool> CheckPermissionsAsync(IAuthorizationService authorizationService, WidgetDefinition widget)
{
foreach (var permission in widget.RequiredPermissions)
{
if (!await authorizationService.IsGrantedAsync(permission))
{
return false;
}
}
return true;
}
} }
} }

17
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 @using Volo.Abp.AspNetCore.Mvc.UI.Widgets
@inject IAuthorizationService AuthorizationService
@inject IWidgetRenderer WidgetRenderer @inject IWidgetRenderer WidgetRenderer
@inject IStringLocalizerFactory localizer @inject IStringLocalizerFactory localizer
@model Volo.Abp.AspNetCore.Mvc.UI.Dashboards.Components.Dashboard.DashboardViewModel @model Volo.Abp.AspNetCore.Mvc.UI.Dashboards.Components.Dashboard.DashboardViewModel
@ -10,12 +12,15 @@
@foreach (var widgetConfiguration in Model.Dashboard.AvailableWidgets) @foreach (var widgetConfiguration in Model.Dashboard.AvailableWidgets)
{ {
var widgetDefinition = Model.GetWidget(widgetConfiguration.WidgetName); 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);
<abp-column class="col-@(widgetDefinition.DefaultDimensions.Width) p-2" <abp-column class="col-@(widgetDefinition.DefaultDimensions.Width) p-2"
style="height: @(widgetDefinition.DefaultDimensions.Height * 100)px"> style="height: @(widgetDefinition.DefaultDimensions.Height * 100)px">
@await WidgetRenderer.RenderAsync(Component, widgetDefinition.Name) @await WidgetRenderer.RenderAsync(Component, widgetDefinition.Name)
</abp-column> </abp-column>
}
} }
</abp-row> </abp-row>

20
framework/src/Volo.Abp.AspNetCore.Mvc.UI.Widgets/Volo/Abp/AspNetCore/Mvc/UI/Widgets/WidgetDefinition.cs

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using JetBrains.Annotations; using JetBrains.Annotations;
using Volo.Abp.Localization; using Volo.Abp.Localization;
@ -29,16 +30,29 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Widgets
[CanBeNull] [CanBeNull]
public WidgetDimensions DefaultDimensions { get; set; } public WidgetDimensions DefaultDimensions { get; set; }
public List<string> RequiredPermissions { get; set; }
public WidgetDefinition( public WidgetDefinition(
[NotNull] string name, [NotNull] string name,
[NotNull] Type viewComponentType,
[CanBeNull] ILocalizableString displayName, [CanBeNull] ILocalizableString displayName,
[CanBeNull] WidgetDimensions defaultDimensions = null) [NotNull] Type viewComponentType)
{ {
DefaultDimensions = defaultDimensions;
Name = Check.NotNullOrWhiteSpace(name, nameof(name)); Name = Check.NotNullOrWhiteSpace(name, nameof(name));
ViewComponentType = Check.NotNull(viewComponentType, nameof(viewComponentType)); ViewComponentType = Check.NotNull(viewComponentType, nameof(viewComponentType));
DisplayName = displayName ?? new FixedLocalizableString(name); DisplayName = displayName ?? new FixedLocalizableString(name);
RequiredPermissions = new List<string>();
}
public WidgetDefinition AddRequiredPermission(string permissionName)
{
RequiredPermissions.Add(permissionName);
return this;
}
public WidgetDefinition SetDefaultDimension(int width, int height)
{
DefaultDimensions = new WidgetDimensions(width, height);
return this;
} }
} }
} }

4
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<IHtmlContent> RenderAsync(IViewComponentHelper componentHelper, string widgetName, object args = null) public async Task<IHtmlContent> 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());
} }
} }
} }

19
samples/DashboardDemo/src/DashboardDemo.Web/Pages/widgets/RoleListWidget.js

@ -1,14 +1,15 @@
(function ($) { (function ($) {
var $container = $('#RoleListWidgetContainer'); 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 += '<li>' + result.items[i].name + '</li>';
}
_identityUserAppService.getList({}).then(function (result) { $container.find('#RoleList').html(html);
var html = ''; });
for (var i = 0; i < result.items.length; i++) { }
html += '<li>'+ result.items[i].name+'</li>';
}
$container.find('#RoleList').html(html);
});
})(jQuery); })(jQuery);

12
samples/DashboardDemo/src/DashboardDemo.Web/Pages/widgets/UserCountWidget.js

@ -1,10 +1,10 @@
(function ($) { (function ($) {
var $container = $('#UserCountWidgetContainer'); var $container = $('#UserCountWidgetContainer');
if ($container.length > 0) {
var _identityUserAppService = volo.abp.identity.identityUser; var _identityUserAppService = volo.abp.identity.identityUser;
_identityUserAppService.getList({}).then(function(result) {
_identityUserAppService.getList({}).then(function(result) { $container.find('#UserCount').text(result.items.length);
$container.find('#UserCount').text(result.items.length); });
}); }
})(jQuery); })(jQuery);

25
samples/DashboardDemo/src/DashboardDemo.Web/Widgets/WidgetDefinitionProvider.cs

@ -2,6 +2,7 @@
using DashboardDemo.Localization.DashboardDemo; using DashboardDemo.Localization.DashboardDemo;
using DashboardDemo.Pages.widgets; using DashboardDemo.Pages.widgets;
using Volo.Abp.AspNetCore.Mvc.UI.Widgets; using Volo.Abp.AspNetCore.Mvc.UI.Widgets;
using Volo.Abp.Identity;
using Volo.Abp.Localization; using Volo.Abp.Localization;
namespace DashboardDemo.Widgets namespace DashboardDemo.Widgets
@ -10,20 +11,26 @@ namespace DashboardDemo.Widgets
{ {
public static List<WidgetDefinition> GetDefinitions() public static List<WidgetDefinition> GetDefinitions()
{ {
return new List<WidgetDefinition> var userCountWidget = new WidgetDefinition(
{
new WidgetDefinition(
UserCountWidgetViewComponent.WidgetName, UserCountWidgetViewComponent.WidgetName,
typeof(UserCountWidgetViewComponent),
LocalizableString.Create<DashboardDemoResource>(UserCountWidgetViewComponent.DisplayName), LocalizableString.Create<DashboardDemoResource>(UserCountWidgetViewComponent.DisplayName),
new WidgetDimensions(4,2) typeof(UserCountWidgetViewComponent)
), )
new WidgetDefinition( .SetDefaultDimension(6, 3)
.AddRequiredPermission(IdentityPermissions.Users.Default);
var roleListWidget = new WidgetDefinition(
RoleListWidgetViewComponent.WidgetName, RoleListWidgetViewComponent.WidgetName,
typeof(RoleListWidgetViewComponent),
LocalizableString.Create<DashboardDemoResource>(RoleListWidgetViewComponent.DisplayName), LocalizableString.Create<DashboardDemoResource>(RoleListWidgetViewComponent.DisplayName),
new WidgetDimensions(6,3) typeof(RoleListWidgetViewComponent)
) )
.SetDefaultDimension(6, 3)
.AddRequiredPermission(IdentityPermissions.Roles.Default);
return new List<WidgetDefinition>
{
userCountWidget,
roleListWidget
}; };
} }
} }

Loading…
Cancel
Save