added IWidgetPolicyChecker

pull/1584/head
Yunus Emre Kalkan 6 years ago
parent d003a4761d
commit c8875c5ea0

@ -1,17 +1,13 @@
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewComponents;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.Extensions.Options;
using Volo.Abp.Authorization;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Users;
namespace Volo.Abp.AspNetCore.Mvc.UI.Widgets
{
@ -20,21 +16,15 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Widgets
{
protected WidgetOptions Options { get; }
protected IPageWidgetManager PageWidgetManager { get; }
protected IAuthorizationService AuthorizationService { get; }
protected ICurrentUser CurrentUser { get; }
protected DefaultViewComponentHelper DefaultViewComponentHelper { get; }
public AbpViewComponentHelper(
DefaultViewComponentHelper defaultViewComponentHelper,
IOptions<WidgetOptions> widgetOptions,
IPageWidgetManager pageWidgetManager,
IAuthorizationService authorizationService,
ICurrentUser currentUser)
IPageWidgetManager pageWidgetManager)
{
DefaultViewComponentHelper = defaultViewComponentHelper;
PageWidgetManager = pageWidgetManager;
AuthorizationService = authorizationService;
CurrentUser = currentUser;
Options = widgetOptions.Value;
}
@ -67,18 +57,6 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Widgets
protected virtual async Task<IHtmlContent> InvokeWidgetAsync(object arguments, WidgetDefinition widget)
{
if (widget.RequiredPolicies.Any())
{
foreach (var requiredPolicy in widget.RequiredPolicies)
{
await AuthorizationService.AuthorizeAsync(requiredPolicy);
}
}
else if (widget.RequiresAuthentication && !CurrentUser.IsAuthenticated)
{
throw new AbpAuthorizationException("Authorization failed! User has not logged in.");
}
PageWidgetManager.TryAdd(widget);
var wrapperAttributesBuilder = new StringBuilder($"class=\"abp-widget-wrapper\" data-widget-name=\"{widget.Name}\"");
@ -87,7 +65,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Widgets
{
wrapperAttributesBuilder.Append($" data-refresh-url=\"{widget.RefreshUrl}\"");
}
return new HtmlContentBuilder()
.AppendHtml($"<div {wrapperAttributesBuilder}>")
.AppendHtml(await DefaultViewComponentHelper.InvokeAsync(widget.ViewComponentType, arguments))

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.AspNetCore.Mvc.UI.Widgets
{
public interface IWidgetPolicyChecker : ITransientDependency
{
Task<bool> CheckAsync(Type widgetComponentType);
Task<bool> CheckAsync(string name);
}
}

@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.ViewComponents;
using Microsoft.Extensions.Options;
using Volo.Abp.Users;
namespace Volo.Abp.AspNetCore.Mvc.UI.Widgets
{
public class WidgetPolicyChecker : IWidgetPolicyChecker
{
protected WidgetOptions Options { get; }
protected IAuthorizationService AuthorizationService { get; }
protected ICurrentUser CurrentUser { get; }
public WidgetPolicyChecker(
IOptions<WidgetOptions> widgetOptions,
IAuthorizationService authorizationService,
ICurrentUser currentUser)
{
AuthorizationService = authorizationService;
CurrentUser = currentUser;
Options = widgetOptions.Value;
}
public async Task<bool> CheckAsync(Type widgetComponentType)
{
var widget = Options.Widgets.Find(widgetComponentType);
return await CheckAsyncInternal(widget, widgetComponentType.FullName);
}
public async Task<bool> CheckAsync(string name)
{
var widget = Options.Widgets.Find(name);
return await CheckAsyncInternal(widget, name);
}
public async Task<bool> CheckAsyncInternal(WidgetDefinition widget, string wantedWidgetName)
{
if (widget == null)
{
throw new ArgumentNullException(wantedWidgetName);
}
if (widget.RequiredPolicies.Any())
{
foreach (var requiredPolicy in widget.RequiredPolicies)
{
if (!(await AuthorizationService.AuthorizeAsync(requiredPolicy)).Succeeded)
{
return false;
}
}
}
else if (widget.RequiresAuthentication && !CurrentUser.IsAuthenticated)
{
return false;
}
return true;
}
}
}

@ -14,7 +14,7 @@
<abp-column size-md="_5">
<abp-input asp-for="StartDate" />
</abp-column>
<abp-column size-md="_5">
<abp-column size-md="_5">
<abp-input asp-for="EndDate" />
</abp-column>
<abp-column size-md="_2">

Loading…
Cancel
Save