Skip to content

Commit 1b40d49

Browse files
committed
Make DynamicAuthorizationFilter generic to pass IdentityDbContext.
1 parent cf37e99 commit 1b40d49

File tree

4 files changed

+64
-15
lines changed

4 files changed

+64
-15
lines changed
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netstandard2.0;netcoreapp3.0</TargetFrameworks>
4+
<TargetFrameworks>netstandard2.0;netcoreapp3.0;netcoreapp3.1</TargetFrameworks>
55
</PropertyGroup>
66

7-
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
7+
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
88
<DefineConstants>NETCORE2;NETSTANDARD;NETSTANDARD2_0</DefineConstants>
99
</PropertyGroup>
1010

11-
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp3'">
11+
<PropertyGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
1212
<DefineConstants>NETCORE3;NETSTANDARD;NETSTANDARD2_1</DefineConstants>
1313
</PropertyGroup>
1414

1515
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
1616
<FrameworkReference Include="Microsoft.AspNetCore.App" />
17+
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.0.0" />
1718
</ItemGroup>
1819

19-
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
20-
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.0" />
20+
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
21+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
22+
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.0" />
23+
</ItemGroup>
24+
25+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
26+
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
27+
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="2.2.0" />
2128
</ItemGroup>
2229
</Project>

src/DynamicAuthorization.Mvc.Core/Extensions/ServiceCollectionExtensions.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using DynamicAuthorization.Mvc.Core.Builder;
22
using DynamicAuthorization.Mvc.Core.Models;
3+
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
34
using Microsoft.AspNetCore.Mvc;
45
using Microsoft.Extensions.DependencyInjection;
56
using System;
@@ -8,7 +9,26 @@ namespace DynamicAuthorization.Mvc.Core.Extensions
89
{
910
public static class ServiceCollectionExtensions
1011
{
11-
public static IDynamicAuthorizationBuilder AddDynamicAuthorization(this IServiceCollection services, Action<DynamicAuthorizationOptions> options)
12+
//public static IDynamicAuthorizationBuilder AddDynamicAuthorization(this IServiceCollection services, Action<DynamicAuthorizationOptions> options)
13+
//{
14+
// var dynamicAuthorizationOptions = new DynamicAuthorizationOptions();
15+
// options.Invoke(dynamicAuthorizationOptions);
16+
// services.AddSingleton(dynamicAuthorizationOptions);
17+
18+
// services.AddSingleton<IMvcControllerDiscovery, MvcControllerDiscovery>();
19+
20+
// services.Configure<MvcOptions>(mvcOptions =>
21+
// {
22+
// mvcOptions.Filters.Add(typeof(DynamicAuthorizationFilter));
23+
// });
24+
25+
// IDynamicAuthorizationBuilder builder = new DynamicAuthorizationBuilder(services);
26+
27+
// return builder;
28+
//}
29+
30+
public static IDynamicAuthorizationBuilder AddDynamicAuthorization<TDbContext>(this IServiceCollection services, Action<DynamicAuthorizationOptions> options)
31+
where TDbContext : IdentityDbContext
1232
{
1333
var dynamicAuthorizationOptions = new DynamicAuthorizationOptions();
1434
options.Invoke(dynamicAuthorizationOptions);
@@ -18,7 +38,7 @@ public static IDynamicAuthorizationBuilder AddDynamicAuthorization(this IService
1838

1939
services.Configure<MvcOptions>(mvcOptions =>
2040
{
21-
mvcOptions.Filters.Add(typeof(DynamicAuthorizationFilter));
41+
mvcOptions.Filters.Add(typeof(DynamicAuthorizationFilter<TDbContext>));
2242
});
2343

2444
IDynamicAuthorizationBuilder builder = new DynamicAuthorizationBuilder(services);

src/DynamicAuthorization.Mvc.Core/Filters/DynamicAuthorizationFilter.cs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
11
using DynamicAuthorization.Mvc.Core.Models;
22
using Microsoft.AspNetCore.Authorization;
3+
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
34
using Microsoft.AspNetCore.Mvc;
45
using Microsoft.AspNetCore.Mvc.Authorization;
56
using Microsoft.AspNetCore.Mvc.Controllers;
67
using Microsoft.AspNetCore.Mvc.Filters;
8+
using Microsoft.EntityFrameworkCore;
79
using System;
810
using System.Linq;
911
using System.Reflection;
1012
using System.Threading.Tasks;
1113

1214
namespace DynamicAuthorization.Mvc.Core
1315
{
14-
public class DynamicAuthorizationFilter : IAuthorizationFilter, IAsyncAuthorizationFilter
16+
public class DynamicAuthorizationFilter<TDbContext> : IAuthorizationFilter, IAsyncAuthorizationFilter
17+
where TDbContext : IdentityDbContext
1518
{
1619
private readonly DynamicAuthorizationOptions _authorizationOptions;
20+
private readonly TDbContext _identityDbContext;
1721
private readonly IRoleAccessStore _roleAccessStore;
1822

19-
public DynamicAuthorizationFilter(DynamicAuthorizationOptions authorizationOptions, IRoleAccessStore roleAccessStore)
23+
public DynamicAuthorizationFilter(
24+
DynamicAuthorizationOptions authorizationOptions,
25+
TDbContext identityDbContext,
26+
IRoleAccessStore roleAccessStore
27+
)
2028
{
2129
_authorizationOptions = authorizationOptions;
2230
_roleAccessStore = roleAccessStore;
31+
_identityDbContext = identityDbContext;
2332
}
2433

2534
public void OnAuthorization(AuthorizationFilterContext context)
@@ -43,8 +52,15 @@ public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
4352
return;
4453

4554
var actionId = GetActionId(context);
46-
47-
if (await _roleAccessStore.HasAccessToActionAsync(userName, actionId))
55+
var roles = await (
56+
from user in _identityDbContext.Users
57+
join userRole in _identityDbContext.UserRoles on user.Id equals userRole.UserId
58+
join role in _identityDbContext.Roles on userRole.RoleId equals role.Id
59+
where user.UserName == userName
60+
select role.Id
61+
).ToArrayAsync();
62+
63+
if (await _roleAccessStore.HasAccessToActionAsync(actionId, roles))
4864
return;
4965

5066
context.Result = new ForbidResult();
@@ -54,7 +70,10 @@ public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
5470

5571
private static bool IsProtectedAction(AuthorizationFilterContext context)
5672
{
57-
var controllerActionDescriptor = (ControllerActionDescriptor)context.ActionDescriptor;
73+
var controllerActionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;
74+
if(controllerActionDescriptor == null)
75+
return false;
76+
5877
var controllerTypeInfo = controllerActionDescriptor.ControllerTypeInfo;
5978

6079
var anonymousAttribute = controllerTypeInfo.GetCustomAttribute<AllowAnonymousAttribute>();
@@ -84,7 +103,10 @@ private bool IsProtectedAction(AuthorizationFilterContext context)
84103
if (context.Filters.Any(item => item is IAllowAnonymousFilter))
85104
return false;
86105

87-
var controllerActionDescriptor = (ControllerActionDescriptor)context.ActionDescriptor;
106+
var controllerActionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;
107+
if (controllerActionDescriptor == null)
108+
return false;
109+
88110
var controllerTypeInfo = controllerActionDescriptor.ControllerTypeInfo;
89111
var actionMethodInfo = controllerActionDescriptor.MethodInfo;
90112

src/DynamicAuthorization.Mvc.Core/MvcControllerDiscovery.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public IEnumerable<MvcControllerInfo> GetControllers()
2727
_mvcControllers = new List<MvcControllerInfo>();
2828
var items = _actionDescriptorCollectionProvider
2929
.ActionDescriptors.Items
30-
.Where(descriptor => descriptor.GetType() == typeof(ControllerActionDescriptor))
31-
.Select(descriptor => (ControllerActionDescriptor)descriptor)
30+
.OfType<ControllerActionDescriptor>()
31+
.Select(descriptor => descriptor)
3232
.GroupBy(descriptor => descriptor.ControllerTypeInfo.FullName)
3333
.ToList();
3434

0 commit comments

Comments
 (0)