-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathServiceCollectionExtensions.cs
More file actions
121 lines (109 loc) · 5.97 KB
/
ServiceCollectionExtensions.cs
File metadata and controls
121 lines (109 loc) · 5.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
namespace DynamicAuthorization.Mvc.Core
{
/// <summary>
/// Extension methods for setting up Dynamic Authorization related services in an <see cref="IServiceCollection"/>.
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// Registers the Dynamic Authorization as a service in the <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services">The services.</param>
/// <param name="optionsBuilder">
/// An action to configure the <see cref="DynamicAuthorizationOptionBuilder"/> for the
/// Dynamic Authorization.
/// </param>
/// <param name="defaultAdminUser">
/// The default user to access all controllers without needs for creating role and related
/// accesses in database.
/// </param>
/// <exception cref="ArgumentNullException">services</exception>
/// <exception cref="ArgumentNullException">optionsBuilder</exception>
/// <returns>The same service collection so that multiple calls can be chained.</returns>
public static IDynamicAuthorizationOptionBuilder AddDynamicAuthorization<TDbContext>(
this IServiceCollection services,
Action<DynamicAuthorizationOptionBuilder> optionsBuilder,
string defaultAdminUser
) where TDbContext : DbContext
{
if (services == null)
throw new ArgumentNullException(nameof(services));
if (optionsBuilder == null)
throw new ArgumentNullException(nameof(optionsBuilder));
if (defaultAdminUser == null)
throw new ArgumentNullException(nameof(defaultAdminUser));
var baseType = typeof(TDbContext).BaseType;
var paramsLength = baseType.GetGenericArguments().Length;
Type userType;
Type roleType;
Type keyType;
switch (paramsLength)
{
case 1:
userType = baseType.GetGenericArguments()[0];
DynamicAuthorizationOptionsInternals.UserType = userType;
DynamicAuthorizationOptionsInternals.RoleType = typeof(IdentityRole);
DynamicAuthorizationOptionsInternals.KeyType = typeof(string);
services.Configure<MvcOptions>(mvcOptions =>
{
mvcOptions.Filters.Add(typeof(DynamicAuthorizationFilter<,>).MakeGenericType(typeof(TDbContext), userType));
});
break;
case 3:
userType = baseType.GetGenericArguments()[0];
roleType = baseType.GetGenericArguments()[1];
keyType = baseType.GetGenericArguments()[2];
DynamicAuthorizationOptionsInternals.UserType = userType;
DynamicAuthorizationOptionsInternals.RoleType = roleType;
DynamicAuthorizationOptionsInternals.KeyType = keyType;
services.Configure<MvcOptions>(mvcOptions =>
{
mvcOptions.Filters.Add(typeof(DynamicAuthorizationFilter<,,,>)
.MakeGenericType(typeof(TDbContext), userType, roleType, keyType));
});
break;
case 8:
userType = baseType.GetGenericArguments()[0];
roleType = baseType.GetGenericArguments()[1];
keyType = baseType.GetGenericArguments()[2];
var userClaimType = baseType.GetGenericArguments()[3];
var userRoleType = baseType.GetGenericArguments()[4];
var userLoginType = baseType.GetGenericArguments()[5];
var roleClaimType = baseType.GetGenericArguments()[6];
var userTokenType = baseType.GetGenericArguments()[7];
DynamicAuthorizationOptionsInternals.UserType = userType;
DynamicAuthorizationOptionsInternals.RoleType = roleType;
DynamicAuthorizationOptionsInternals.KeyType = keyType;
DynamicAuthorizationOptionsInternals.UserClaimType = userClaimType;
DynamicAuthorizationOptionsInternals.UserRoleType = userRoleType;
DynamicAuthorizationOptionsInternals.UserLoginType = userLoginType;
DynamicAuthorizationOptionsInternals.RoleClaimType = roleClaimType;
DynamicAuthorizationOptionsInternals.UserTokenType = userTokenType;
services.Configure<MvcOptions>(mvcOptions =>
{
mvcOptions.Filters.Add(typeof(DynamicAuthorizationFilter<,,,,,,,,>)
.MakeGenericType(typeof(TDbContext), userType, roleType, keyType, userClaimType, userRoleType, userLoginType, roleClaimType, userTokenType));
});
break;
default:
DynamicAuthorizationOptionsInternals.UserType = typeof(IdentityUser);
DynamicAuthorizationOptionsInternals.RoleType = typeof(IdentityRole);
DynamicAuthorizationOptionsInternals.KeyType = typeof(string);
services.Configure<MvcOptions>(mvcOptions =>
{
mvcOptions.Filters.Add(typeof(DynamicAuthorizationFilter<>).MakeGenericType(typeof(TDbContext)));
});
break;
}
services.AddSingleton<IMvcControllerDiscovery, MvcControllerDiscovery>();
IDynamicAuthorizationOptionBuilder builder = new DynamicAuthorizationOptionBuilder(services);
DynamicAuthorizationOptionsInternals.DbContextType = typeof(TDbContext);
return builder;
}
}
}