Skip to content

Commit 5a0a202

Browse files
committed
Register DbContext with eight generic parameter.
1 parent 27a025c commit 5a0a202

File tree

4 files changed

+187
-7
lines changed

4 files changed

+187
-7
lines changed

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public static IDynamicAuthorizationBuilder AddDynamicAuthorization<TDbContext>(t
2121
var baseType = typeof(TDbContext).BaseType;
2222
var paramsLength = baseType.GetGenericArguments().Length;
2323
Type userType;
24+
Type roleType;
25+
Type keyType;
2426

2527
switch (paramsLength)
2628
{
@@ -37,8 +39,8 @@ public static IDynamicAuthorizationBuilder AddDynamicAuthorization<TDbContext>(t
3739

3840
case 3:
3941
userType = baseType.GetGenericArguments()[0];
40-
var roleType = baseType.GetGenericArguments()[1];
41-
var keyType = baseType.GetGenericArguments()[2];
42+
roleType = baseType.GetGenericArguments()[1];
43+
keyType = baseType.GetGenericArguments()[2];
4244
DynamicAuthorizationOptions.UserType = userType;
4345
DynamicAuthorizationOptions.RoleType = roleType;
4446
DynamicAuthorizationOptions.KeyType = keyType;
@@ -49,6 +51,25 @@ public static IDynamicAuthorizationBuilder AddDynamicAuthorization<TDbContext>(t
4951
});
5052
break;
5153

54+
case 8:
55+
userType = baseType.GetGenericArguments()[0];
56+
roleType = baseType.GetGenericArguments()[1];
57+
keyType = baseType.GetGenericArguments()[2];
58+
var userClaimType = baseType.GetGenericArguments()[3];
59+
var userRoleType = baseType.GetGenericArguments()[4];
60+
var userLoginType = baseType.GetGenericArguments()[5];
61+
var roleClaimType = baseType.GetGenericArguments()[6];
62+
var userTokenType = baseType.GetGenericArguments()[7];
63+
DynamicAuthorizationOptions.UserType = userType;
64+
DynamicAuthorizationOptions.RoleType = roleType;
65+
DynamicAuthorizationOptions.KeyType = keyType;
66+
services.Configure<MvcOptions>(mvcOptions =>
67+
{
68+
mvcOptions.Filters.Add(typeof(DynamicAuthorizationFilter<,,,,,,,,>)
69+
.MakeGenericType(typeof(TDbContext), userType, roleType, keyType, userClaimType, userRoleType, userLoginType, roleClaimType, userTokenType));
70+
});
71+
break;
72+
5273
default:
5374
DynamicAuthorizationOptions.UserType = typeof(IdentityUser);
5475
DynamicAuthorizationOptions.RoleType = typeof(IdentityRole);

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,38 @@ public class DynamicAuthorizationFilter<TDbContext, TUser> : DynamicAuthorizatio
3333
public DynamicAuthorizationFilter(
3434
DynamicAuthorizationOptions authorizationOptions,
3535
TDbContext dbContext,
36-
IRoleAccessStore roleAccessStore
37-
) : base(authorizationOptions, dbContext, roleAccessStore)
36+
IRoleAccessStore roleAccessStore)
37+
: base(authorizationOptions, dbContext, roleAccessStore)
3838
{
3939
}
4040
}
4141

42-
public class DynamicAuthorizationFilter<TDbContext, TUser, TRole, TKey> : IAsyncAuthorizationFilter
42+
public class DynamicAuthorizationFilter<TDbContext, TUser, TRole, TKey>
43+
: DynamicAuthorizationFilter<TDbContext, TUser, TRole, TKey, IdentityUserClaim<TKey>, IdentityUserRole<TKey>, IdentityUserLogin<TKey>, IdentityRoleClaim<TKey>, IdentityUserToken<TKey>>
4344
where TDbContext : IdentityDbContext<TUser, TRole, TKey>
4445
where TUser : IdentityUser<TKey>
4546
where TRole : IdentityRole<TKey>
4647
where TKey : IEquatable<TKey>
48+
{
49+
public DynamicAuthorizationFilter(
50+
DynamicAuthorizationOptions authorizationOptions,
51+
TDbContext dbContext,
52+
IRoleAccessStore roleAccessStore)
53+
: base(authorizationOptions, dbContext, roleAccessStore)
54+
{
55+
}
56+
}
4757

58+
public class DynamicAuthorizationFilter<TDbContext, TUser, TRole, TKey, TUserClaim, TUserRole, TUserLogin, TRoleClaim, TUserToken> : IAsyncAuthorizationFilter
59+
where TDbContext : IdentityDbContext<TUser, TRole, TKey, TUserClaim, TUserRole, TUserLogin, TRoleClaim, TUserToken>
60+
where TUser : IdentityUser<TKey>
61+
where TRole : IdentityRole<TKey>
62+
where TKey : IEquatable<TKey>
63+
where TUserClaim : IdentityUserClaim<TKey>
64+
where TUserRole : IdentityUserRole<TKey>
65+
where TUserLogin : IdentityUserLogin<TKey>
66+
where TRoleClaim : IdentityRoleClaim<TKey>
67+
where TUserToken : IdentityUserToken<TKey>
4868
{
4969
private readonly DynamicAuthorizationOptions _authorizationOptions;
5070
private readonly TDbContext _dbContext;
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
using DynamicAuthorization.Mvc.Core.Extensions;
2+
using DynamicAuthorization.Mvc.JsonStore.Extensions;
3+
using DynamicRoleBasedAuthorization.Tests.TestSetup;
4+
using Microsoft.AspNetCore.Builder;
5+
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.AspNetCore.Http;
7+
using Microsoft.AspNetCore.Identity;
8+
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
9+
using Microsoft.AspNetCore.TestHost;
10+
using Microsoft.EntityFrameworkCore;
11+
using Microsoft.Extensions.Configuration;
12+
using Microsoft.Extensions.DependencyInjection;
13+
using Microsoft.Extensions.Hosting;
14+
using System.Collections.Generic;
15+
using System.Threading.Tasks;
16+
using Xunit;
17+
18+
namespace DynamicRoleBasedAuthorization.Tests
19+
{
20+
public class ServiceRegistrationTests
21+
{
22+
[Fact]
23+
public void Register_DynamicAuthorization_With_TypeOf_DbContext_With_Eight_Generic_Parameter()
24+
{
25+
//Arrange
26+
var builder = new WebHostBuilder().UseStartup<CustomStartup>();
27+
28+
// Act
29+
var exception = Record.Exception(() => new TestServer(builder));
30+
31+
// Assert
32+
Assert.Null(exception);
33+
}
34+
}
35+
36+
internal class ApplicationUser : IdentityUser<int>
37+
{
38+
public string Name { get; set; }
39+
40+
public virtual ICollection<ApplicationUserRole> UserRoles { get; set; }
41+
}
42+
43+
internal class ApplicationRole : IdentityRole<int>
44+
{
45+
}
46+
47+
internal class ApplicationUserRole : IdentityUserRole<int>
48+
{
49+
internal ApplicationUser User { get; set; }
50+
51+
internal ApplicationRole Role { get; set; }
52+
}
53+
54+
internal class ApplicationUserClaim : IdentityUserClaim<int>
55+
{
56+
}
57+
58+
internal class ApplicationUserLogin : IdentityUserLogin<int>
59+
{
60+
}
61+
62+
internal class ApplicationURoleClaim : IdentityRoleClaim<int>
63+
{
64+
}
65+
66+
internal class ApplicationUserToken : IdentityUserToken<int>
67+
{
68+
}
69+
70+
internal class CustomDbContext : IdentityDbContext<
71+
ApplicationUser, ApplicationRole, int,
72+
ApplicationUserClaim, ApplicationUserRole, ApplicationUserLogin,
73+
ApplicationURoleClaim, ApplicationUserToken>
74+
{
75+
public CustomDbContext(DbContextOptions<ApplicationDbContext> options)
76+
: base(options)
77+
{
78+
}
79+
}
80+
81+
internal class CustomStartup
82+
{
83+
public CustomStartup(IConfiguration configuration)
84+
{
85+
Configuration = configuration;
86+
}
87+
88+
public IConfiguration Configuration { get; }
89+
90+
// This method gets called by the runtime. Use this method to add services to the container.
91+
public void ConfigureServices(IServiceCollection services)
92+
{
93+
services.AddControllersWithViews();
94+
95+
services.AddDbContext<CustomDbContext>(options => options.UseInMemoryDatabase("InMemoryDbForTesting"));
96+
97+
services.AddIdentity<ApplicationUser, ApplicationRole>(options => options.SignIn.RequireConfirmedAccount = false)
98+
.AddEntityFrameworkStores<CustomDbContext>()
99+
.AddDefaultTokenProviders();
100+
101+
services.ConfigureApplicationCookie(options =>
102+
{
103+
options.Events.OnRedirectToLogin = context =>
104+
{
105+
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
106+
return Task.CompletedTask;
107+
};
108+
options.Events.OnRedirectToAccessDenied = context =>
109+
{
110+
context.Response.StatusCode = StatusCodes.Status403Forbidden;
111+
return Task.CompletedTask;
112+
};
113+
});
114+
115+
services.AddDynamicAuthorization<CustomDbContext>(options => options.DefaultAdminUser = InitialData.SuperUser.UserName)
116+
.AddJsonStore();
117+
118+
services.AddScoped<DbInitializer>();
119+
}
120+
121+
public void Configure(IApplicationBuilder app, IHostApplicationLifetime applicationLifetime)
122+
{
123+
app.UseRouting();
124+
125+
app.UseAuthentication();
126+
app.UseAuthorization();
127+
128+
app.UseEndpoints(endpoints =>
129+
{
130+
endpoints.MapControllerRoute(
131+
name: "default-area",
132+
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
133+
endpoints.MapControllerRoute(
134+
name: "default",
135+
pattern: "{controller=Home}/{action=Index}/{id?}");
136+
});
137+
}
138+
}
139+
}

test/DynamicRoleBasedAuthorization.Tests/TestSetup/DbInitializer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using System.Threading.Tasks;
2-
using Microsoft.AspNetCore.Identity;
1+
using Microsoft.AspNetCore.Identity;
2+
using System.Threading.Tasks;
33

44
namespace DynamicRoleBasedAuthorization.Tests.TestSetup
55
{

0 commit comments

Comments
 (0)