11using DynamicAuthorization . Mvc . Core . Models ;
22using Microsoft . AspNetCore . Authorization ;
3+ using Microsoft . AspNetCore . Identity ;
34using Microsoft . AspNetCore . Identity . EntityFrameworkCore ;
45using Microsoft . AspNetCore . Mvc ;
56using Microsoft . AspNetCore . Mvc . Authorization ;
1314
1415namespace 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 ;
0 commit comments