Skip to content

Commit f2e77d4

Browse files
committed
Fix registring IIdentityService with DbContext that has eight generic parameters.
1 parent 00b17e5 commit f2e77d4

File tree

6 files changed

+62
-10
lines changed

6 files changed

+62
-10
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ public static IDynamicAuthorizationBuilder AddDynamicAuthorization<TDbContext>(t
6363
DynamicAuthorizationOptions.UserType = userType;
6464
DynamicAuthorizationOptions.RoleType = roleType;
6565
DynamicAuthorizationOptions.KeyType = keyType;
66+
DynamicAuthorizationOptions.UserClaimType = userClaimType;
67+
DynamicAuthorizationOptions.UserRoleType = userRoleType;
68+
DynamicAuthorizationOptions.UserLoginType = userLoginType;
69+
DynamicAuthorizationOptions.RoleClaimType = roleClaimType;
70+
DynamicAuthorizationOptions.UserTokenType = userTokenType;
6671
services.Configure<MvcOptions>(mvcOptions =>
6772
{
6873
mvcOptions.Filters.Add(typeof(DynamicAuthorizationFilter<,,,,,,,,>)

src/DynamicAuthorization.Mvc.Core/Models/DynamicAuthorizationOptions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,15 @@ public class DynamicAuthorizationOptions
1717
internal static Type RoleType { get; set; }
1818

1919
internal static Type KeyType { get; set; }
20+
21+
internal static Type UserClaimType { get; set; }
22+
23+
internal static Type UserRoleType { get; set; }
24+
25+
internal static Type UserLoginType { get; set; }
26+
27+
internal static Type RoleClaimType { get; set; }
28+
29+
internal static Type UserTokenType { get; set; }
2030
}
2131
}

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,29 @@ public static IDynamicAuthorizationBuilder AddUi(this IDynamicAuthorizationBuild
3232
}
3333

3434
builder.Services.AddScoped<AddResourcesToViewFilter>();
35-
builder.Services.AddScoped(
36-
typeof(IIdentityService),
37-
typeof(IdentityService<,,,>).MakeGenericType(
38-
DynamicAuthorizationOptions.DbContextType,
39-
DynamicAuthorizationOptions.UserType,
40-
DynamicAuthorizationOptions.RoleType,
41-
DynamicAuthorizationOptions.KeyType
35+
36+
if (DynamicAuthorizationOptions.UserClaimType == null)
37+
builder.Services.AddScoped(
38+
typeof(IIdentityService),
39+
typeof(IdentityService<,,,>).MakeGenericType(
40+
DynamicAuthorizationOptions.DbContextType,
41+
DynamicAuthorizationOptions.UserType,
42+
DynamicAuthorizationOptions.RoleType,
43+
DynamicAuthorizationOptions.KeyType
44+
));
45+
else
46+
builder.Services.AddScoped(
47+
typeof(IIdentityService),
48+
typeof(IdentityService<,,,,,,,,>).MakeGenericType(
49+
DynamicAuthorizationOptions.DbContextType,
50+
DynamicAuthorizationOptions.UserType,
51+
DynamicAuthorizationOptions.RoleType,
52+
DynamicAuthorizationOptions.KeyType,
53+
DynamicAuthorizationOptions.UserClaimType,
54+
DynamicAuthorizationOptions.UserRoleType,
55+
DynamicAuthorizationOptions.UserLoginType,
56+
DynamicAuthorizationOptions.RoleClaimType,
57+
DynamicAuthorizationOptions.UserTokenType
4258
));
4359

4460
return builder;

src/DynamicAuthorization.Mvc.Ui/Services/IdentityService.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,29 @@ public IdentityService(TDbContext context) : base(context)
2626
}
2727
}
2828

29-
internal class IdentityService<TDbContext, TUser, TRole, TKey> : IIdentityService
29+
internal class
30+
IdentityService<TDbContext, TUser, TRole, TKey> : IdentityService<TDbContext, TUser, TRole, TKey, IdentityUserClaim<TKey>, IdentityUserRole<TKey>, IdentityUserLogin<TKey>, IdentityRoleClaim<TKey>, IdentityUserToken<TKey>>
3031
where TDbContext : IdentityDbContext<TUser, TRole, TKey>
3132
where TUser : IdentityUser<TKey>
3233
where TRole : IdentityRole<TKey>
3334
where TKey : IEquatable<TKey>
35+
36+
{
37+
public IdentityService(TDbContext context) : base(context)
38+
{
39+
}
40+
}
41+
42+
internal class IdentityService<TDbContext, TUser, TRole, TKey, TUserClaim, TUserRole, TUserLogin, TRoleClaim, TUserToken> : IIdentityService
43+
where TDbContext : IdentityDbContext<TUser, TRole, TKey, TUserClaim, TUserRole, TUserLogin, TRoleClaim, TUserToken>
44+
where TUser : IdentityUser<TKey>
45+
where TRole : IdentityRole<TKey>
46+
where TKey : IEquatable<TKey>
47+
where TUserClaim : IdentityUserClaim<TKey>
48+
where TUserRole : IdentityUserRole<TKey>
49+
where TUserLogin : IdentityUserLogin<TKey>
50+
where TRoleClaim : IdentityRoleClaim<TKey>
51+
where TUserToken : IdentityUserToken<TKey>
3452
{
3553
private readonly TDbContext _context;
3654

test/DynamicRoleBasedAuthorization.Tests/DynamicRoleBasedAuthorization.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@
3030
<ItemGroup>
3131
<ProjectReference Include="..\..\src\DynamicAuthorization.Mvc.Core\DynamicAuthorization.Mvc.Core.csproj" />
3232
<ProjectReference Include="..\..\src\DynamicAuthorization.Mvc.JsonStore\DynamicAuthorization.Mvc.JsonStore.csproj" />
33+
<ProjectReference Include="..\..\src\DynamicAuthorization.Mvc.Ui\DynamicAuthorization.Mvc.Ui.csproj" />
3334
</ItemGroup>
3435
</Project>

test/DynamicRoleBasedAuthorization.Tests/ServiceRegistrationTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using DynamicAuthorization.Mvc.Core.Extensions;
22
using DynamicAuthorization.Mvc.JsonStore.Extensions;
3+
using DynamicAuthorization.Mvc.Ui;
34
using DynamicRoleBasedAuthorization.Tests.TestSetup;
45
using Microsoft.AspNetCore.Builder;
56
using Microsoft.AspNetCore.Hosting;
@@ -90,7 +91,7 @@ public CustomStartup(IConfiguration configuration)
9091
// This method gets called by the runtime. Use this method to add services to the container.
9192
public void ConfigureServices(IServiceCollection services)
9293
{
93-
services.AddControllersWithViews();
94+
var mvcBuilder = services.AddControllersWithViews();
9495

9596
services.AddDbContext<CustomDbContext>(options => options.UseInMemoryDatabase("InMemoryDbForTesting"));
9697

@@ -113,7 +114,8 @@ public void ConfigureServices(IServiceCollection services)
113114
});
114115

115116
services.AddDynamicAuthorization<CustomDbContext>(options => options.DefaultAdminUser = InitialData.SuperUser.UserName)
116-
.AddJsonStore();
117+
.AddJsonStore()
118+
.AddUi(mvcBuilder);
117119

118120
services.AddScoped<DbInitializer>();
119121
}

0 commit comments

Comments
 (0)