-
Notifications
You must be signed in to change notification settings - Fork 28
/
AutofacWebApiFilterProvider.cs
307 lines (260 loc) · 16.5 KB
/
AutofacWebApiFilterProvider.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// This software is part of the Autofac IoC container
// Copyright © 2011 Autofac Contributors
// http://autofac.org
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using Autofac.Core.Lifetime;
using Autofac.Features.Metadata;
namespace Autofac.Integration.WebApi
{
/// <summary>
/// A filter provider for performing property injection on filter attributes.
/// </summary>
public class AutofacWebApiFilterProvider : IFilterProvider
{
class FilterContext
{
public ILifetimeScope LifetimeScope { get; set; }
public Type ControllerType { get; set; }
public List<FilterInfo> Filters { get; set; }
public Dictionary<string, List<FilterMetadata>> AddedFilters { get; set; }
}
readonly ILifetimeScope _rootLifetimeScope;
readonly ActionDescriptorFilterProvider _filterProvider = new ActionDescriptorFilterProvider();
internal static string ActionFilterMetadataKey = "AutofacWebApiActionFilter";
internal static string ActionFilterOverrideMetadataKey = "AutofacWebApiActionFilterOverride";
internal static string AuthorizationFilterMetadataKey = "AutofacWebApiAuthorizationFilter";
internal static string AuthorizationFilterOverrideMetadataKey = "AutofacWebApiAuthorizationFilterOverride";
internal static string AuthenticationFilterMetadataKey = "AutofacWebApiAuthenticationFilter";
internal static string AuthenticationFilterOverrideMetadataKey = "AutofacWebApiAuthenticationFilterOverride";
internal static string ExceptionFilterMetadataKey = "AutofacWebApiExceptionFilter";
internal static string ExceptionFilterOverrideMetadataKey = "AutofacWebApiExceptionFilterOverride";
/// <summary>
/// Initializes a new instance of the <see cref="AutofacWebApiFilterProvider"/> class.
/// </summary>
public AutofacWebApiFilterProvider(ILifetimeScope lifetimeScope)
{
_rootLifetimeScope = lifetimeScope;
}
/// <summary>
/// Returns the collection of filters associated with <paramref name="actionDescriptor"/>.
/// </summary>
/// <param name="configuration">The configuration.</param>
/// <param name="actionDescriptor">The action descriptor.</param>
/// <returns>A collection of filters with instances property injected.</returns>
/// <exception cref="System.ArgumentNullException">
/// Thrown if <paramref name="configuration" /> is <see langword="null" />.
/// </exception>
public IEnumerable<FilterInfo> GetFilters(HttpConfiguration configuration, HttpActionDescriptor actionDescriptor)
{
if (configuration == null)
{
throw new ArgumentNullException("configuration");
}
var filters = _filterProvider.GetFilters(configuration, actionDescriptor).ToList();
foreach (var filterInfo in filters)
_rootLifetimeScope.InjectProperties(filterInfo.Instance);
var descriptor = actionDescriptor as ReflectedHttpActionDescriptor;
if (descriptor == null) return filters;
// Use a fake scope to resolve the metadata for the filter.
var rootLifetimeScope = configuration.DependencyResolver.GetRootLifetimeScope();
using (var lifetimeScope = rootLifetimeScope.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag))
{
var filterContext = new FilterContext
{
LifetimeScope = lifetimeScope,
ControllerType = actionDescriptor.ControllerDescriptor.ControllerType,
Filters = filters,
AddedFilters = new Dictionary<string, List<FilterMetadata>>
{
{ActionFilterMetadataKey, new List<FilterMetadata>()},
{ActionFilterOverrideMetadataKey, new List<FilterMetadata>()},
{AuthenticationFilterMetadataKey, new List<FilterMetadata>()},
{AuthenticationFilterOverrideMetadataKey, new List<FilterMetadata>()},
{AuthorizationFilterMetadataKey, new List<FilterMetadata>()},
{AuthorizationFilterOverrideMetadataKey, new List<FilterMetadata>()},
{ExceptionFilterMetadataKey, new List<FilterMetadata>()},
{ExceptionFilterOverrideMetadataKey, new List<FilterMetadata>()}
}
};
// Controller scoped override filters (NOOP kind).
ResolveControllerScopedNoopFilterOverrides(filterContext);
// Action scoped override filters (NOOP kind).
ResolveActionScopedNoopFilterOverrides(filterContext, descriptor);
// Controller scoped override filters.
ResolveControllerScopedFilterOverrides(filterContext);
// Action scoped override filters.
ResolveActionScopedFilterOverrides(filterContext, descriptor);
// Controller scoped filters.
ResolveControllerScopedFilters(filterContext);
// Action scoped filters.
ResolveActionScopedFilters(filterContext, descriptor);
}
return filters;
}
static void ResolveControllerScopedNoopFilterOverrides(FilterContext filterContext)
{
ResolveControllerScopedOverrideFilter(filterContext, ActionFilterOverrideMetadataKey);
ResolveControllerScopedOverrideFilter(filterContext, AuthenticationFilterOverrideMetadataKey);
ResolveControllerScopedOverrideFilter(filterContext, AuthorizationFilterOverrideMetadataKey);
ResolveControllerScopedOverrideFilter(filterContext, ExceptionFilterOverrideMetadataKey);
}
static void ResolveActionScopedNoopFilterOverrides(FilterContext filterContext, ReflectedHttpActionDescriptor descriptor)
{
ResolveActionScopedOverrideFilter(filterContext, descriptor.MethodInfo, ActionFilterOverrideMetadataKey);
ResolveActionScopedOverrideFilter(filterContext, descriptor.MethodInfo, AuthenticationFilterOverrideMetadataKey);
ResolveActionScopedOverrideFilter(filterContext, descriptor.MethodInfo, AuthorizationFilterOverrideMetadataKey);
ResolveActionScopedOverrideFilter(filterContext, descriptor.MethodInfo, ExceptionFilterOverrideMetadataKey);
}
static void ResolveControllerScopedFilterOverrides(FilterContext filterContext)
{
ResolveControllerScopedFilter<IAutofacActionFilter, ActionFilterOverrideWrapper>(
filterContext, m => new ActionFilterOverrideWrapper(m), ActionFilterOverrideMetadataKey);
ResolveControllerScopedFilter<IAutofacAuthenticationFilter, AuthenticationFilterOverrideWrapper>(
filterContext, m => new AuthenticationFilterOverrideWrapper(m), AuthenticationFilterOverrideMetadataKey);
ResolveControllerScopedFilter<IAutofacAuthorizationFilter, AuthorizationFilterOverrideWrapper>(
filterContext, m => new AuthorizationFilterOverrideWrapper(m), AuthorizationFilterOverrideMetadataKey);
ResolveControllerScopedFilter<IAutofacExceptionFilter, ExceptionFilterOverrideWrapper>(
filterContext, m => new ExceptionFilterOverrideWrapper(m), ExceptionFilterOverrideMetadataKey);
}
static void ResolveActionScopedFilterOverrides(FilterContext filterContext, ReflectedHttpActionDescriptor descriptor)
{
ResolveActionScopedFilter<IAutofacActionFilter, ActionFilterOverrideWrapper>(
filterContext, descriptor.MethodInfo, m => new ActionFilterOverrideWrapper(m), ActionFilterOverrideMetadataKey);
ResolveActionScopedFilter<IAutofacAuthenticationFilter, AuthenticationFilterOverrideWrapper>(
filterContext, descriptor.MethodInfo, m => new AuthenticationFilterOverrideWrapper(m),
AuthenticationFilterOverrideMetadataKey);
ResolveActionScopedFilter<IAutofacAuthorizationFilter, AuthorizationFilterOverrideWrapper>(
filterContext, descriptor.MethodInfo, m => new AuthorizationFilterOverrideWrapper(m),
AuthorizationFilterOverrideMetadataKey);
ResolveActionScopedFilter<IAutofacExceptionFilter, ExceptionFilterOverrideWrapper>(
filterContext, descriptor.MethodInfo, m => new ExceptionFilterOverrideWrapper(m),
ExceptionFilterOverrideMetadataKey);
}
static void ResolveControllerScopedFilters(FilterContext filterContext)
{
ResolveControllerScopedFilter<IAutofacActionFilter, ActionFilterWrapper>(
filterContext, m => new ActionFilterWrapper(m), ActionFilterMetadataKey);
ResolveControllerScopedFilter<IAutofacAuthenticationFilter, AuthenticationFilterWrapper>(
filterContext, m => new AuthenticationFilterWrapper(m), AuthenticationFilterMetadataKey);
ResolveControllerScopedFilter<IAutofacAuthorizationFilter, AuthorizationFilterWrapper>(
filterContext, m => new AuthorizationFilterWrapper(m), AuthorizationFilterMetadataKey);
ResolveControllerScopedFilter<IAutofacExceptionFilter, ExceptionFilterWrapper>(
filterContext, m => new ExceptionFilterWrapper(m), ExceptionFilterMetadataKey);
}
static void ResolveActionScopedFilters(FilterContext filterContext, ReflectedHttpActionDescriptor descriptor)
{
ResolveActionScopedFilter<IAutofacActionFilter, ActionFilterWrapper>(
filterContext, descriptor.MethodInfo, m => new ActionFilterWrapper(m), ActionFilterMetadataKey);
ResolveActionScopedFilter<IAutofacAuthenticationFilter, AuthenticationFilterWrapper>(
filterContext, descriptor.MethodInfo, m => new AuthenticationFilterWrapper(m), AuthenticationFilterMetadataKey);
ResolveActionScopedFilter<IAutofacAuthorizationFilter, AuthorizationFilterWrapper>(
filterContext, descriptor.MethodInfo, m => new AuthorizationFilterWrapper(m), AuthorizationFilterMetadataKey);
ResolveActionScopedFilter<IAutofacExceptionFilter, ExceptionFilterWrapper>(
filterContext, descriptor.MethodInfo, m => new ExceptionFilterWrapper(m), ExceptionFilterMetadataKey);
}
static void ResolveControllerScopedFilter<TFilter, TWrapper>(
FilterContext filterContext, Func<FilterMetadata, TWrapper> wrapperFactory, string metadataKey)
where TFilter : class
where TWrapper : IFilter
{
var filters = filterContext.LifetimeScope.Resolve<IEnumerable<Meta<Lazy<TFilter>>>>();
foreach (var filter in filters.Where(a => a.Metadata.ContainsKey(metadataKey) && a.Metadata[metadataKey] is FilterMetadata))
{
var metadata = (FilterMetadata)filter.Metadata[metadataKey];
if (!FilterMatchesController(filterContext, metadataKey, metadata)) continue;
var wrapper = wrapperFactory(metadata);
filterContext.Filters.Add(new FilterInfo(wrapper, metadata.FilterScope));
filterContext.AddedFilters[metadataKey].Add(metadata);
}
}
static void ResolveActionScopedFilter<TFilter, TWrapper>(
FilterContext filterContext, MethodInfo methodInfo, Func<FilterMetadata, TWrapper> wrapperFactory, string metadataKey)
where TFilter : class
where TWrapper : IFilter
{
var filters = filterContext.LifetimeScope.Resolve<IEnumerable<Meta<Lazy<TFilter>>>>();
foreach (var filter in filters.Where(a => a.Metadata.ContainsKey(metadataKey) && a.Metadata[metadataKey] is FilterMetadata))
{
var metadata = (FilterMetadata)filter.Metadata[metadataKey];
if (!FilterMatchesAction(filterContext, methodInfo, metadataKey, metadata)) continue;
var wrapper = wrapperFactory(metadata);
filterContext.Filters.Add(new FilterInfo(wrapper, metadata.FilterScope));
filterContext.AddedFilters[metadataKey].Add(metadata);
}
}
static void ResolveControllerScopedOverrideFilter(FilterContext filterContext, string metadataKey)
{
var filters = filterContext.LifetimeScope.Resolve<IEnumerable<Meta<IOverrideFilter>>>();
foreach (var filter in filters.Where(a => a.Metadata.ContainsKey(metadataKey) && a.Metadata[metadataKey] is FilterMetadata))
{
var metadata = (FilterMetadata)filter.Metadata[metadataKey];
if (!FilterMatchesController(filterContext, metadataKey, metadata)) continue;
filterContext.Filters.Add(new FilterInfo(filter.Value, metadata.FilterScope));
filterContext.AddedFilters[metadataKey].Add(metadata);
}
}
static void ResolveActionScopedOverrideFilter(FilterContext filterContext, MethodInfo methodInfo, string metadataKey)
{
var filters = filterContext.LifetimeScope.Resolve<IEnumerable<Meta<IOverrideFilter>>>();
foreach (var filter in filters.Where(a => a.Metadata.ContainsKey(metadataKey) && a.Metadata[metadataKey] is FilterMetadata))
{
var metadata = (FilterMetadata)filter.Metadata[metadataKey];
if (!FilterMatchesAction(filterContext, methodInfo, metadataKey, metadata)) continue;
filterContext.Filters.Add(new FilterInfo(filter.Value, metadata.FilterScope));
filterContext.AddedFilters[metadataKey].Add(metadata);
}
}
static bool MatchingFilterAdded(IEnumerable<FilterMetadata> filters, FilterMetadata metadata)
{
return filters.Any(filter => filter.ControllerType == metadata.ControllerType
&& filter.FilterScope == metadata.FilterScope
&& filter.MethodInfo == metadata.MethodInfo);
}
static bool FilterMatchesController(FilterContext filterContext, string metadataKey, FilterMetadata metadata)
{
return metadata.ControllerType != null
&& metadata.ControllerType.IsAssignableFrom(filterContext.ControllerType)
&& metadata.FilterScope == FilterScope.Controller
&& metadata.MethodInfo == null
&& !MatchingFilterAdded(filterContext.AddedFilters[metadataKey], metadata);
}
static bool FilterMatchesAction(FilterContext filterContext, MethodInfo methodInfo, string metadataKey, FilterMetadata metadata)
{
// Issue #10: Comparing MethodInfo.MethodHandle rather than just MethodInfo equality
// because MethodInfo equality fails on a derived controller if the base class method
// isn't marked virtual... but MethodHandle correctly compares regardless.
return metadata.ControllerType != null
&& metadata.ControllerType.IsAssignableFrom(filterContext.ControllerType)
&& metadata.FilterScope == FilterScope.Action
&& metadata.MethodInfo.GetBaseDefinition().MethodHandle == methodInfo.GetBaseDefinition().MethodHandle
&& !MatchingFilterAdded(filterContext.AddedFilters[metadataKey], metadata);
}
}
}