Skip to content

Commit 63453c2

Browse files
committed
Fix registering customized identity dbcontext, user and role.
1 parent 98e42a1 commit 63453c2

File tree

3 files changed

+102
-22
lines changed

3 files changed

+102
-22
lines changed

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

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,71 @@
11
using DynamicAuthorization.Mvc.Core.Builder;
22
using DynamicAuthorization.Mvc.Core.Models;
3-
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
3+
using Microsoft.AspNetCore.Identity;
44
using Microsoft.AspNetCore.Mvc;
5+
using Microsoft.EntityFrameworkCore;
56
using Microsoft.Extensions.DependencyInjection;
67
using System;
78

89
namespace DynamicAuthorization.Mvc.Core.Extensions
910
{
1011
public static class ServiceCollectionExtensions
1112
{
12-
public static IDynamicAuthorizationBuilder AddDynamicAuthorization<TDbContext>(this IServiceCollection services, Action<DynamicAuthorizationOptions> options)
13-
where TDbContext : IdentityDbContext
13+
public static IDynamicAuthorizationBuilder AddDynamicAuthorization<TDbContext>(this IServiceCollection services,
14+
Action<DynamicAuthorizationOptions> options)
15+
where TDbContext : DbContext
1416
{
1517
var dynamicAuthorizationOptions = new DynamicAuthorizationOptions();
1618
options.Invoke(dynamicAuthorizationOptions);
1719
services.AddSingleton(dynamicAuthorizationOptions);
1820

19-
services.AddSingleton<IMvcControllerDiscovery, MvcControllerDiscovery>();
21+
var baseType = typeof(TDbContext).BaseType;
22+
var paramsLength = baseType.GetGenericArguments().Length;
23+
Type userType;
2024

21-
services.Configure<MvcOptions>(mvcOptions =>
25+
switch (paramsLength)
2226
{
23-
mvcOptions.Filters.Add(typeof(DynamicAuthorizationFilter<TDbContext>));
24-
});
27+
case 1:
28+
userType = baseType.GetGenericArguments()[0];
29+
DynamicAuthorizationOptions.UserType = userType;
30+
DynamicAuthorizationOptions.RoleType = typeof(IdentityRole);
31+
DynamicAuthorizationOptions.KeyType = typeof(string);
32+
services.Configure<MvcOptions>(mvcOptions =>
33+
{
34+
mvcOptions.Filters.Add(typeof(DynamicAuthorizationFilter<,>).MakeGenericType(typeof(TDbContext), userType));
35+
});
36+
break;
37+
38+
case 3:
39+
userType = baseType.GetGenericArguments()[0];
40+
var roleType = baseType.GetGenericArguments()[1];
41+
var keyType = baseType.GetGenericArguments()[2];
42+
DynamicAuthorizationOptions.UserType = userType;
43+
DynamicAuthorizationOptions.RoleType = roleType;
44+
DynamicAuthorizationOptions.KeyType = keyType;
45+
services.Configure<MvcOptions>(mvcOptions =>
46+
{
47+
mvcOptions.Filters.Add(typeof(DynamicAuthorizationFilter<,,,>)
48+
.MakeGenericType(typeof(TDbContext), userType, roleType, keyType));
49+
});
50+
break;
51+
52+
default:
53+
DynamicAuthorizationOptions.UserType = typeof(IdentityUser);
54+
DynamicAuthorizationOptions.RoleType = typeof(IdentityRole);
55+
DynamicAuthorizationOptions.KeyType = typeof(string);
56+
services.Configure<MvcOptions>(mvcOptions =>
57+
{
58+
mvcOptions.Filters.Add(typeof(DynamicAuthorizationFilter<>).MakeGenericType(typeof(TDbContext)));
59+
});
60+
break;
61+
}
62+
63+
services.AddSingleton<IMvcControllerDiscovery, MvcControllerDiscovery>();
2564

2665
IDynamicAuthorizationBuilder builder = new DynamicAuthorizationBuilder(services);
2766

67+
DynamicAuthorizationOptions.DbContextType = typeof(TDbContext);
68+
2869
return builder;
2970
}
3071
}

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

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using DynamicAuthorization.Mvc.Core.Models;
22
using Microsoft.AspNetCore.Authorization;
3+
using Microsoft.AspNetCore.Identity;
34
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
45
using Microsoft.AspNetCore.Mvc;
56
using Microsoft.AspNetCore.Mvc.Authorization;
@@ -13,27 +14,51 @@
1314

1415
namespace DynamicAuthorization.Mvc.Core
1516
{
16-
public class DynamicAuthorizationFilter<TDbContext> : IAuthorizationFilter, IAsyncAuthorizationFilter
17+
public class DynamicAuthorizationFilter<TDbContext> : DynamicAuthorizationFilter<TDbContext, IdentityUser, IdentityRole, string>
1718
where TDbContext : IdentityDbContext
19+
{
20+
public DynamicAuthorizationFilter(
21+
DynamicAuthorizationOptions authorizationOptions,
22+
TDbContext dbContext,
23+
IRoleAccessStore roleAccessStore
24+
) : base(authorizationOptions, dbContext, roleAccessStore)
25+
{
26+
}
27+
}
28+
29+
public class DynamicAuthorizationFilter<TDbContext, TUser> : DynamicAuthorizationFilter<TDbContext, TUser, IdentityRole, string>
30+
where TDbContext : IdentityDbContext<TUser>
31+
where TUser : IdentityUser
32+
{
33+
public DynamicAuthorizationFilter(
34+
DynamicAuthorizationOptions authorizationOptions,
35+
TDbContext dbContext,
36+
IRoleAccessStore roleAccessStore
37+
) : base(authorizationOptions, dbContext, roleAccessStore)
38+
{
39+
}
40+
}
41+
42+
public class DynamicAuthorizationFilter<TDbContext, TUser, TRole, TKey> : IAsyncAuthorizationFilter
43+
where TDbContext : IdentityDbContext<TUser, TRole, TKey>
44+
where TUser : IdentityUser<TKey>
45+
where TRole : IdentityRole<TKey>
46+
where TKey : IEquatable<TKey>
47+
1848
{
1949
private readonly DynamicAuthorizationOptions _authorizationOptions;
20-
private readonly TDbContext _identityDbContext;
50+
private readonly TDbContext _dbContext;
2151
private readonly IRoleAccessStore _roleAccessStore;
2252

2353
public DynamicAuthorizationFilter(
2454
DynamicAuthorizationOptions authorizationOptions,
25-
TDbContext identityDbContext,
55+
TDbContext dbContext,
2656
IRoleAccessStore roleAccessStore
2757
)
2858
{
2959
_authorizationOptions = authorizationOptions;
3060
_roleAccessStore = roleAccessStore;
31-
_identityDbContext = identityDbContext;
32-
}
33-
34-
public void OnAuthorization(AuthorizationFilterContext context)
35-
{
36-
OnAuthorizationAsync(context).RunSynchronously();
61+
_dbContext = dbContext;
3762
}
3863

3964
public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
@@ -52,12 +77,13 @@ public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
5277
return;
5378

5479
var actionId = GetActionId(context);
80+
5581
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
82+
from user in _dbContext.Users
83+
join userRole in _dbContext.UserRoles on user.Id equals userRole.UserId
84+
join role in _dbContext.Roles on userRole.RoleId equals role.Id
5985
where user.UserName == userName
60-
select role.Id
86+
select role.Id.ToString()
6187
).ToArrayAsync();
6288

6389
if (await _roleAccessStore.HasAccessToActionAsync(actionId, roles))
@@ -71,7 +97,7 @@ select role.Id
7197
private static bool IsProtectedAction(AuthorizationFilterContext context)
7298
{
7399
var controllerActionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;
74-
if(controllerActionDescriptor == null)
100+
if (controllerActionDescriptor == null)
75101
return false;
76102

77103
var controllerTypeInfo = controllerActionDescriptor.ControllerTypeInfo;
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1-
namespace DynamicAuthorization.Mvc.Core.Models
1+
using System;
2+
using System.Runtime.CompilerServices;
3+
4+
[assembly: InternalsVisibleTo("DynamicAuthorization.Mvc.Ui")]
5+
6+
namespace DynamicAuthorization.Mvc.Core.Models
27
{
38
public class DynamicAuthorizationOptions
49
{
510
public string DefaultAdminUser { get; set; }
11+
12+
internal static Type DbContextType { get; set; }
13+
14+
internal static Type UserType { get; set; }
15+
16+
internal static Type RoleType { get; set; }
17+
18+
internal static Type KeyType { get; set; }
619
}
720
}

0 commit comments

Comments
 (0)