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.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<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
@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);
<abp-column class="col-@(widgetDefinition.DefaultDimensions.Width) p-2"
style="height: @(widgetDefinition.DefaultDimensions.Height * 100)px">
<abp-column class="col-@(widgetDefinition.DefaultDimensions.Width) p-2"
style="height: @(widgetDefinition.DefaultDimensions.Height * 100)px">
@await WidgetRenderer.RenderAsync(Component, widgetDefinition.Name)
</abp-column>
@await WidgetRenderer.RenderAsync(Component, widgetDefinition.Name)
</abp-column>
}
}
</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.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<string> 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<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)
{
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 ($) {
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) {
var html = '';
for (var i = 0; i < result.items.length; i++) {
html += '<li>'+ result.items[i].name+'</li>';
}
$container.find('#RoleList').html(html);
});
$container.find('#RoleList').html(html);
});
}
})(jQuery);

12
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);

25
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<WidgetDefinition> GetDefinitions()
{
return new List<WidgetDefinition>
{
new WidgetDefinition(
var userCountWidget = new WidgetDefinition(
UserCountWidgetViewComponent.WidgetName,
typeof(UserCountWidgetViewComponent),
LocalizableString.Create<DashboardDemoResource>(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<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