This repository has been archived by the owner on Dec 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
ConfigurationExpression.cs
260 lines (210 loc) · 9.61 KB
/
ConfigurationExpression.cs
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web.Mvc;
using FluentSecurity.Caching;
using FluentSecurity.Configuration;
using FluentSecurity.Diagnostics;
using FluentSecurity.Internals;
using FluentSecurity.Policy.ViolationHandlers.Conventions;
using FluentSecurity.Scanning;
using FluentSecurity.Scanning.TypeScanners;
using FluentSecurity.ServiceLocation;
namespace FluentSecurity
{
public abstract class ConfigurationExpression
{
public AdvancedConfiguration Advanced { get; private set; }
internal SecurityRuntime Runtime { get; private set; }
internal IPolicyAppender PolicyAppender { get; set; }
internal void Initialize(SecurityRuntime runtime)
{
Runtime = runtime;
Advanced = new AdvancedConfiguration(Runtime);
PolicyAppender = new DefaultPolicyAppender();
}
public IPolicyContainerConfiguration For<TController>(Expression<Func<TController, object>> actionExpression) where TController : Controller
{
var controllerName = typeof(TController).GetControllerName();
var actionName = actionExpression.GetActionName();
return AddPolicyContainerFor(controllerName, actionName);
}
public IPolicyContainerConfiguration For<TController>(Expression<Action<TController>> actionExpression) where TController : Controller
{
var controllerName = typeof(TController).GetControllerName();
var actionName = actionExpression.GetActionName();
return AddPolicyContainerFor(controllerName, actionName);
}
public IPolicyContainerConfiguration For<TController>() where TController : Controller
{
var controllerType = typeof(TController);
var controllerTypes = new[] { controllerType };
return CreateConventionPolicyContainerFor(controllerTypes, defaultCacheLevel: By.Controller);
}
public virtual IPolicyContainerConfiguration ForAllControllers()
{
var assemblyScanner = new AssemblyScanner();
assemblyScanner.TheCallingAssembly();
assemblyScanner.With<ControllerTypeScanner>();
var controllerTypes = assemblyScanner.Scan();
return CreateConventionPolicyContainerFor(controllerTypes);
}
public IPolicyContainerConfiguration ForAllControllersInAssembly(params Assembly[] assemblies)
{
var assemblyScanner = new AssemblyScanner();
assemblyScanner.Assemblies(assemblies);
assemblyScanner.With<ControllerTypeScanner>();
var controllerTypes = assemblyScanner.Scan();
return CreateConventionPolicyContainerFor(controllerTypes);
}
public IPolicyContainerConfiguration ForAllControllersInAssemblyContainingType<TType>()
{
var assembly = typeof (TType).Assembly;
return ForAllControllersInAssembly(assembly);
}
public IPolicyContainerConfiguration ForAllControllersInheriting<TController>(Expression<Func<TController, object>> actionExpression, params Assembly[] assemblies) where TController : Controller
{
if (actionExpression == null) throw new ArgumentNullException("actionExpression");
var actionName = actionExpression.GetActionName();
return ForAllControllersInheriting<TController>(action => action == actionName, assemblies);
}
public IPolicyContainerConfiguration ForAllControllersInheriting<TController>(params Assembly[] assemblies) where TController : Controller
{
Func<string, bool> actionFilter = actionName => true;
return ForAllControllersInheriting<TController>(actionFilter, assemblies);
}
private IPolicyContainerConfiguration ForAllControllersInheriting<TController>(Func<string, bool> actionFilter, IEnumerable<Assembly> assemblies) where TController : Controller
{
var controllerType = typeof (TController);
var assembliesToScan = assemblies.ToList();
if (!assembliesToScan.Any())
assembliesToScan.Add(controllerType.Assembly);
var assemblyScanner = new AssemblyScanner();
assemblyScanner.Assemblies(assembliesToScan);
assemblyScanner.With(new ControllerTypeScanner(controllerType));
var controllerTypes = assemblyScanner.Scan();
Func<ControllerActionInfo, bool> filter = info => actionFilter.Invoke(info.ActionName);
return CreateConventionPolicyContainerFor(controllerTypes, filter);
}
public IPolicyContainerConfiguration ForAllControllersInNamespaceContainingType<TType>()
{
var assembly = typeof (TType).Assembly;
var assemblyScanner = new AssemblyScanner();
assemblyScanner.Assembly(assembly);
assemblyScanner.With<ControllerTypeScanner>();
assemblyScanner.IncludeNamespaceContainingType<TType>();
var controllerTypes = assemblyScanner.Scan();
return CreateConventionPolicyContainerFor(controllerTypes);
}
public IPolicyContainerConfiguration ForActionsMatching(Func<ControllerActionInfo, bool> actionFilter, params Assembly[] assemblies)
{
var assemblyScanner = new AssemblyScanner();
var assembliesToScan = assemblies.ToList();
if (assembliesToScan.Any())
assemblyScanner.Assemblies(assemblies);
else
assemblyScanner.TheCallingAssembly();
assemblyScanner.With<ControllerTypeScanner>();
var controllerTypes = assemblyScanner.Scan();
return CreateConventionPolicyContainerFor(controllerTypes, actionFilter);
}
public void Scan(Action<ProfileScanner> scan)
{
Publish.ConfigurationEvent(() => "Scanning for profiles");
var profileScanner = new ProfileScanner();
scan.Invoke(profileScanner);
var profiles = profileScanner.Scan().ToList();
profiles.ForEach(ApplyProfile);
}
public void ApplyProfile<TSecurityProfile>() where TSecurityProfile : SecurityProfile, new()
{
var profile = new TSecurityProfile();
Publish.ConfigurationEvent(() => "Applying profile {0}.".FormatWith(profile.GetType().FullName));
Runtime.ApplyConfiguration(profile);
}
private void ApplyProfile(Type profileType)
{
var profile = Activator.CreateInstance(profileType) as SecurityProfile;
if (profile != null)
{
Publish.ConfigurationEvent(() => "Applying profile {0}.".FormatWith(profile.GetType().FullName));
Runtime.ApplyConfiguration(profile);
}
}
internal IPolicyContainerConfiguration CreateConventionPolicyContainerFor(IEnumerable<Type> controllerTypes, Func<ControllerActionInfo, bool> actionFilter = null, By defaultCacheLevel = By.Policy)
{
var policyContainers = new List<IPolicyContainerConfiguration>();
foreach (var controllerType in controllerTypes)
{
var controllerName = controllerType.GetControllerName();
var actionMethods = controllerType.GetActionMethods(actionFilter);
policyContainers.AddRange(
actionMethods.Select(actionMethod => AddPolicyContainerFor(controllerName, actionMethod.GetActionName()))
);
}
return new ConventionPolicyContainer(policyContainers, defaultCacheLevel);
}
private PolicyContainer AddPolicyContainerFor(string controllerName, string actionName)
{
return Runtime.AddPolicyContainer(new PolicyContainer(controllerName, actionName, PolicyAppender));
}
public void GetAuthenticationStatusFrom(Func<bool> authenticationExpression)
{
if (authenticationExpression == null)
throw new ArgumentNullException("authenticationExpression");
Runtime.IsAuthenticated = authenticationExpression;
}
public void GetRolesFrom(Func<IEnumerable<object>> rolesExpression)
{
if (rolesExpression == null)
throw new ArgumentNullException("rolesExpression");
if (Runtime.PolicyContainers.Any())
throw new ConfigurationErrorsException("You must set the rolesfunction before adding policies.");
Runtime.Roles = rolesExpression;
}
public void SetPolicyAppender(IPolicyAppender policyAppender)
{
if (policyAppender == null)
throw new ArgumentNullException("policyAppender");
PolicyAppender = policyAppender;
}
public void ResolveServicesUsing(Func<Type, IEnumerable<object>> servicesLocator, Func<Type, object> singleServiceLocator = null)
{
if (servicesLocator == null)
throw new ArgumentNullException("servicesLocator");
ResolveServicesUsing(new ExternalServiceLocator(servicesLocator, singleServiceLocator));
}
public void ResolveServicesUsing(ISecurityServiceLocator securityServiceLocator)
{
if (securityServiceLocator == null)
throw new ArgumentNullException("securityServiceLocator");
Runtime.ExternalServiceLocator = securityServiceLocator;
}
public void DefaultPolicyViolationHandlerIs<TPolicyViolationHandler>() where TPolicyViolationHandler : class, IPolicyViolationHandler
{
RemoveDefaultPolicyViolationHandlerConventions();
Advanced.Conventions(conventions =>
conventions.Add(new DefaultPolicyViolationHandlerIsOfTypeConvention<TPolicyViolationHandler>())
);
}
public void DefaultPolicyViolationHandlerIs<TPolicyViolationHandler>(Func<TPolicyViolationHandler> policyViolationHandler) where TPolicyViolationHandler : class, IPolicyViolationHandler
{
RemoveDefaultPolicyViolationHandlerConventions();
Advanced.Conventions(conventions =>
conventions.Add(new DefaultPolicyViolationHandlerIsInstanceConvention<TPolicyViolationHandler>(policyViolationHandler))
);
}
private void RemoveDefaultPolicyViolationHandlerConventions()
{
Advanced.Conventions(conventions =>
{
conventions.RemoveAll(c => c is FindDefaultPolicyViolationHandlerByNameConvention);
conventions.RemoveAll(c => c.IsMatchForGenericType(typeof (DefaultPolicyViolationHandlerIsOfTypeConvention<>)));
conventions.RemoveAll(c => c.IsMatchForGenericType(typeof (DefaultPolicyViolationHandlerIsInstanceConvention<>)));
});
}
}
}