/// <inheritdoc /> public void OnProvidersExecuting(FilterProviderContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } var services = context.ActionContext.HttpContext.RequestServices; var filters = services.GetService <IEnumerable <IFilterMetadata> >(); if (context.ActionContext.ActionDescriptor.FilterDescriptors != null) { foreach (var filter in filters) { var filterItem = new FilterItem(new FilterDescriptor(filter, FilterScope.Global), filter) { IsReusable = false }; // TODO: Remove this when https://github.com/aspnet/Mvc/issues/4504 // is fixed for (var i = 0; i < context.Results.Count; i++) { if (context.Results[i].Filter.GetType() == filter.GetType()) { context.Results.RemoveAt(i); break; } } context.Results.Add(filterItem); } } }
public void OnProvidersExecuting(FilterProviderContext context) { if (context.ActionContext.ActionDescriptor.FilterDescriptors == null) { return; } var overrideFilters = context.Results.Where(filterItem => filterItem.Filter is OverrideFilter).ToArray(); foreach (var overrideFilter in overrideFilters) { var filter = (overrideFilter.Filter as OverrideFilter); if (filter == null) { continue; } if (filter.GetType() == filter.Type) { throw new ProviderException($"It is not allowed to override the type itself. Filter Type: {filter.GetType()} -> Overwritten Filter: {filter.Type}"); } var filters = context.Results.Where(filterItem => filterItem.Filter.GetType() == filter.Type && filterItem.Descriptor.Scope <= overrideFilter.Descriptor.Scope).ToList(); foreach (var filterItem in filters) { context.Results.Remove(filterItem); } } }
public virtual void ProvideFilter(FilterProviderContext context, FilterItem filterItem) { if (filterItem.Filter != null) { return; } var filter = filterItem.Descriptor.Filter; var filterFactory = filter as IFilterFactory; if (filterFactory == null) { filterItem.Filter = filter; filterItem.IsReusable = true; } else { var services = context.ActionContext.HttpContext.RequestServices; filterItem.Filter = filterFactory.CreateInstance(services); filterItem.IsReusable = filterFactory.IsReusable; if (filterItem.Filter == null) { throw new InvalidOperationException(Resources.FormatTypeMethodMustReturnNotNullValue( "CreateInstance", typeof(IFilterFactory).Name)); } ApplyFilterToContainer(filterItem.Filter, filterFactory); } }
public static bool ContainsMatch(this List <AreaNode> routeTree, FilterProviderContext context) { ControllerActionDescriptor cad = (ControllerActionDescriptor)context.ActionContext.ActionDescriptor; string area = (string)context.ActionContext.RouteData.Values["area"]; var areaNode = routeTree.Find(x => { if (string.IsNullOrEmpty(x.Area) && string.IsNullOrEmpty(area)) { return(true); } return(String.Compare(area, x.Area, StringComparison.OrdinalIgnoreCase) == 0); }); if (areaNode != null) { if (areaNode.Controllers == null || !areaNode.Controllers.Any()) { return(true); } var controllerNode = areaNode.Controllers.Find(x => String.Compare(cad.ControllerName, x.Controller, StringComparison.OrdinalIgnoreCase) == 0); if (controllerNode != null) { if (controllerNode.Actions == null || !controllerNode.Actions.Any()) { return(true); } var action = controllerNode.Actions.Find(x => String.Compare(cad.Name, x.Action, StringComparison.OrdinalIgnoreCase) == 0); return(action != null); } } return(false); }
public static bool ContainsMatch(this List<AreaNode> routeTree, FilterProviderContext context) { ControllerActionDescriptor cad = (ControllerActionDescriptor)context.ActionContext.ActionDescriptor; string area = (string)context.ActionContext.RouteData.Values["area"]; var areaNode = routeTree.Find(x => { if (string.IsNullOrEmpty(x.Area) && string.IsNullOrEmpty(area)) return true; return String.Compare(area, x.Area, StringComparison.OrdinalIgnoreCase) == 0; }); if (areaNode != null) { if (areaNode.Controllers == null || !areaNode.Controllers.Any()) return true; var controllerNode = areaNode.Controllers.Find(x => String.Compare(cad.ControllerName, x.Controller, StringComparison.OrdinalIgnoreCase) == 0); if (controllerNode != null) { if (controllerNode.Actions == null || !controllerNode.Actions.Any()) return true; var action = controllerNode.Actions.Find(x => String.Compare(cad.Name, x.Action, StringComparison.OrdinalIgnoreCase) == 0); return action != null; } } return false; }
public static bool ContainsMatch(this List <AreaNode> routeTree, FilterProviderContext context) { ControllerActionDescriptor cad = (ControllerActionDescriptor)context.ActionContext.ActionDescriptor; string area = (string)context.ActionContext.RouteData.Values["area"]; return(routeTree.ContainsMatch(area, cad.ControllerName, cad.ActionName)); }
public void OnProvidersExecuted(FilterProviderContext context) { if (WrappedFilter != null) { QueryFilterProvider filterProvider = new QueryFilterProvider(WrappedFilter); filterProvider.OnProvidersExecuted(context); } }
public void OnProvidersExecuting(FilterProviderContext context) { // remove authorize filters var authFilters = context.Results.Where(x => x.Descriptor.Filter.GetType() == typeof(AuthorizeFilter)).ToList(); foreach (var filter in authFilters) { context.Results.Remove(filter); } }
private bool TryFetchAuthFilterItem(FilterProviderContext context, out FilterItem filterItem) { if (RequiresAuth(context)) { filterItem = TypeToFilterItem[_settings.Value.Authorization.Filter]; return(true); } filterItem = null; return(false); }
public virtual void ProvideFilter(FilterProviderContext context, FilterItem filterItem) { if (filterItem.Descriptor.Filter is AuthorizeFilter && (object)filterItem.Filter == null) { AuthorizeFilter val = filterItem.Descriptor.Filter as AuthorizeFilter; IEnumerable <IAuthorizeData> enumerable = val.AuthorizeData; if (enumerable == null && (object)val.Policy != null) { List <IAuthorizeData> list = new List <IAuthorizeData>(); string text = val.Policy.AuthenticationSchemes.FirstOrDefault(); if (!string.IsNullOrEmpty(text)) { List <IAuthorizeData> list2 = list; MvcAuthorizeAttribute mvcAuthorizeAttribute = new MvcAuthorizeAttribute(); mvcAuthorizeAttribute.AuthenticationSchemes = text; list2.Add(mvcAuthorizeAttribute); } enumerable = list; } filterItem.Filter = new MvcAuthorizeFilter(enumerable).CreateInstance(context.ActionContext.HttpContext.RequestServices); filterItem.IsReusable = true; } if ((object)filterItem.Filter != null) { AuthorizeFilter val2; MvcAuthorizeFilter mvcAuthorizeFilter = default(MvcAuthorizeFilter); if ((val2 = (filterItem.Filter as AuthorizeFilter)) != null && (mvcAuthorizeFilter = (filterItem.Descriptor.Filter as MvcAuthorizeFilter)) != null) { filterItem.Filter = mvcAuthorizeFilter.CreateInstance(context.ActionContext.HttpContext.RequestServices); } } else { IFilterMetadata val3 = filterItem.Descriptor.Filter; IFilterFactory val4; if ((val4 = (val3 as IFilterFactory)) == null) { filterItem.Filter = val3; filterItem.IsReusable = true; } else { IServiceProvider requestServices = context.ActionContext.HttpContext.RequestServices; filterItem.Filter = val4.CreateInstance(requestServices); filterItem.IsReusable = val4.IsReusable; if ((object)filterItem.Filter == null) { throw new InvalidOperationException("The 'CreateInstance' method of type '" + typeof(IFilterFactory).Name + "' cannot return a null value."); } ApplyFilterToContainer((object)filterItem.Filter, val4); } } }
public override void ProvideFilter(FilterProviderContext context, FilterItem filterItem) { FilterRecord record; FilterCollectionExtensions.Records.TryGetValue(filterItem.Descriptor.Filter, out record); if (record != null && !record.Match(context)) { return; } base.ProvideFilter(context, filterItem); }
public bool Match(FilterProviderContext context) { var currentArea = context.ActionContext.RouteData.DataTokens["area"]; // Apply if root area if (currentArea == null && string.IsNullOrEmpty(_areaName)) { return(true); } // Apply for specified area return(currentArea != null && string.Equals(currentArea.ToString(), _areaName, StringComparison.OrdinalIgnoreCase)); }
private IFilterMetadata[] GetFilters() { var filterDescriptors = Context.ActionDescriptor.FilterDescriptors; var items = new List <FilterItem>(filterDescriptors.Count); for (var i = 0; i < filterDescriptors.Count; i++) { items.Add(new FilterItem(filterDescriptors[i])); } var context = new FilterProviderContext(Context, items); for (var i = 0; i < _filterProviders.Count; i++) { _filterProviders[i].OnProvidersExecuting(context); } for (var i = _filterProviders.Count - 1; i >= 0; i--) { _filterProviders[i].OnProvidersExecuted(context); } var count = 0; for (var i = 0; i < items.Count; i++) { if (items[i].Filter != null) { count++; } } if (count == 0) { return(new IFilterMetadata[0]); } else { var filters = new IFilterMetadata[count]; for (int i = 0, j = 0; i < items.Count; i++) { var filter = items[i].Filter; if (filter != null) { filters[j++] = filter; } } return(filters); } }
private void ExecuteProviders(ActionContext actionContext, List <FilterItem> items) { var context = new FilterProviderContext(actionContext, items); for (var i = 0; i < _filterProviders.Length; i++) { _filterProviders[i].OnProvidersExecuting(context); } for (var i = _filterProviders.Length - 1; i >= 0; i--) { _filterProviders[i].OnProvidersExecuted(context); } }
private IFilterMetadata[] GetFilters(ActionContext actionContext, List <FilterItem> staticFilterItems) { // Create a separate collection as we want to hold onto the statically defined filter items // in order to cache them var filterItems = new List <FilterItem>(staticFilterItems); // Execute providers var context = new FilterProviderContext(actionContext, filterItems); for (var i = 0; i < _filterProviders.Length; i++) { _filterProviders[i].OnProvidersExecuting(context); } for (var i = _filterProviders.Length - 1; i >= 0; i--) { _filterProviders[i].OnProvidersExecuted(context); } // Extract filter instances from statically defined filters and filter providers var count = 0; for (var i = 0; i < filterItems.Count; i++) { if (filterItems[i].Filter != null) { count++; } } if (count == 0) { return(EmptyFilterArray); } else { var filters = new IFilterMetadata[count]; var filterIndex = 0; for (int i = 0; i < filterItems.Count; i++) { var filter = filterItems[i].Filter; if (filter != null) { filters[filterIndex++] = filter; } } return(filters); } }
public static void ProvideFilter(FilterProviderContext context, FilterItem filterItem) { if (filterItem.Filter != null) { return; } var filter = filterItem.Descriptor.Filter; if (filter is not IFilterFactory filterFactory) { filterItem.Filter = filter; filterItem.IsReusable = true; }
public void OnProvidersExecuting(FilterProviderContext context) { if (context == null) { throw new ArgumentNullException("context"); } if (context.ActionContext.ActionDescriptor.FilterDescriptors != null) { for (int i = 0; i < context.Results.Count; i++) { ProvideFilter(context, context.Results[i]); } } }
private static IFilterMetadata[] CreateUncachedFiltersCore( IFilterProvider[] filterProviders, ActionContext actionContext, List <FilterItem> filterItems) { // Execute providers var context = new FilterProviderContext(actionContext, filterItems); for (var i = 0; i < filterProviders.Length; i++) { filterProviders[i].OnProvidersExecuting(context); } for (var i = filterProviders.Length - 1; i >= 0; i--) { filterProviders[i].OnProvidersExecuted(context); } // Extract filter instances from statically defined filters and filter providers var count = 0; for (var i = 0; i < filterItems.Count; i++) { if (filterItems[i].Filter != null) { count++; } } if (count == 0) { return(Array.Empty <IFilterMetadata>()); } else { var filters = new IFilterMetadata[count]; var filterIndex = 0; for (int i = 0; i < filterItems.Count; i++) { var filter = filterItems[i].Filter; if (filter != null) { filters[filterIndex++] = filter; } } return(filters); } }
private List <FilterItem> FetchFilters(FilterProviderContext context) { lock (locker) { List <FilterItem> filters; if (!ActionFilterMap.TryGetValue(context.ActionContext.ActionDescriptor.DisplayName, out filters)) { ControllerActionDescriptor cad = (ControllerActionDescriptor)context.ActionContext.ActionDescriptor; string area = (string)context.ActionContext.RouteData.Values["area"]; filters = _authorizeStore.GetFilterItems(area, cad.ControllerName, cad.ActionName).ToList(); ActionFilterMap.Add(context.ActionContext.ActionDescriptor.DisplayName, filters); } return(filters); } }
public override void ProvideFilter(FilterProviderContext context, FilterItem filterItem) { var route = context.ActionContext.ActionDescriptor.AttributeRouteInfo.Template; var filter = options.FirstOrDefault(option => route.StartsWith(option.RoutePrefix))?.Filter; if (filter != null) { if (context.Results.All(r => r.Descriptor.Filter != filter)) { context.Results.Add(new FilterItem(new FilterDescriptor(filter, (int)FilterScope.Controller))); } } base.ProvideFilter(context, filterItem); }
/// <summary> /// Provides filters to apply to the specified action. /// </summary> /// <param name="context">The filter context.</param> public void OnProvidersExecuting(FilterProviderContext context) { // Actions with a bound parameter of type ODataQueryOptions do not support the query filter // The assumption is that the action will handle the querying within the action implementation ControllerActionDescriptor controllerActionDescriptor = context.ActionContext.ActionDescriptor as ControllerActionDescriptor; if (controllerActionDescriptor != null) { Type returnType = controllerActionDescriptor.MethodInfo.ReturnType; if (ShouldAddFilter(context, returnType, controllerActionDescriptor)) { var filterDesc = new FilterDescriptor(QueryFilter, FilterScope.Global); context.Results.Add(new FilterItem(filterDesc, QueryFilter)); } } }
/// <inheritdoc /> public void OnProvidersExecuting(FilterProviderContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (context.ActionContext.ActionDescriptor.FilterDescriptors != null) { // Perf: Avoid allocations for (var i = 0; i < context.Results.Count; i++) { ProvideFilter(context, context.Results[i]); } } }
//all framework providers have negative orders, so ours will come later public void OnProvidersExecuting(FilterProviderContext context) { ControllerActionDescriptor cad = (ControllerActionDescriptor)context.ActionContext.ActionDescriptor; System.Diagnostics.Debug.WriteLine("Controller: " + cad.ControllerTypeInfo.FullName); System.Diagnostics.Debug.WriteLine("ActionName: " + cad.ActionName); System.Diagnostics.Debug.WriteLine("DisplayName: " + cad.DisplayName); System.Diagnostics.Debug.WriteLine("Area: " + context.ActionContext.RouteData.Values["area"]); var filters = FetchFilters(context); foreach (var filter in filters) { context.Results.Add(filter); } }
private bool ShouldAddFilter(FilterProviderContext context, Type returnType, ControllerActionDescriptor controllerActionDescriptor) { if (returnType == null) { return(false); } // Get the inner return type if type is a task. Type innerReturnType = returnType; if (returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(Task <>)) { innerReturnType = returnType.GetGenericArguments().First(); } // See if this type is a SingleResult or is derived from SingleResult. bool isSingleResult = false; if (innerReturnType.IsGenericType) { Type genericType = innerReturnType.GetGenericTypeDefinition(); Type baseType = innerReturnType.BaseType; isSingleResult = (genericType == typeof(SingleResult <>) || baseType == typeof(SingleResult)); } // Don't apply the filter if the result is not IQueryable() or SingleReult(). if (!IsIQueryable(innerReturnType) && !isSingleResult) { return(false); } // If the controller takes a ODataQueryOptions, don't apply the filter. if (controllerActionDescriptor.Parameters .Any(parameter => TypeHelper.IsTypeAssignableFrom(typeof(ODataQueryOptions), parameter.ParameterType))) { return(false); } // Don't apply a global filter if one of the same type exists. if (context.Results.Where(f => f.Filter?.GetType() == QueryFilter.GetType()).Any()) { return(false); } return(true); }
private IFilterMetadata[] GetFilters(ActionContext actionContext, List<FilterItem> filterItems) { // Execute providers var context = new FilterProviderContext(actionContext, filterItems); for (var i = 0; i < _filterProviders.Length; i++) { _filterProviders[i].OnProvidersExecuting(context); } for (var i = _filterProviders.Length - 1; i >= 0; i--) { _filterProviders[i].OnProvidersExecuted(context); } // Extract filter instances from statically defined filters and filter providers var count = 0; for (var i = 0; i < filterItems.Count; i++) { if (filterItems[i].Filter != null) { count++; } } if (count == 0) { return EmptyArray<IFilterMetadata>.Instance; } else { var filters = new IFilterMetadata[count]; var filterIndex = 0; for (int i = 0; i < filterItems.Count; i++) { var filter = filterItems[i].Filter; if (filter != null) { filters[filterIndex++] = filter; } } return filters; } }
public bool Match(FilterProviderContext context) { int lastLevel = _requireCriteria.Aggregate(0, (prev, c) => c.Level > prev ? c.Level : prev); for (int level = 0; level <= lastLevel; level++) { // Gets criteria results by current level var criteria = _requireCriteria.Where(c => c.Level.Equals(level)); if (!criteria.Count().Equals(0)) { bool match = criteria.Aggregate(true, (prev, f) => prev ? f.Criteria.Match(context) : prev); // If match exit from loop if (match) { break; } // If not match and this last level, then return false if (level.Equals(lastLevel)) { return(false); } } } lastLevel = _excludeCriteria.Aggregate(0, (prev, c) => c.Level > prev ? c.Level : prev); for (int level = 0; level <= lastLevel; level++) { // Gets criteria results by current level var criteria = _excludeCriteria.Where(c => c.Level.Equals(level)); if (!criteria.Count().Equals(0)) { bool exclude = criteria.Aggregate(true, (prev, f) => prev ? f.Criteria.Match(context) : prev); // If match return false if (exclude) { return(false); } } } return(true); }
/// <inheritdoc /> public void OnProvidersExecuting(FilterProviderContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (context.ActionContext.ActionDescriptor.FilterDescriptors != null) { var results = context.Results; // Perf: Avoid allocating enumerator and read interface .Count once rather than per iteration var resultsCount = results.Count; for (var i = 0; i < resultsCount; i++) { ProvideFilter(context, results[i]); } } }
private IFilterMetadata[] GetFilters() { var context = new FilterProviderContext( ActionContext, ActionContext.ActionDescriptor.FilterDescriptors.Select(fd => new FilterItem(fd)).ToList()); foreach (var provider in _filterProviders) { provider.OnProvidersExecuting(context); } for (var i = _filterProviders.Count - 1; i >= 0; i--) { _filterProviders[i].OnProvidersExecuted(context); } return(context.Results.Select(item => item.Filter).Where(filter => filter != null).ToArray()); }
/// <inheritdoc /> public void OnProvidersExecuting(FilterProviderContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } var services = context.ActionContext.HttpContext.RequestServices; var filters = services.GetService <IEnumerable <IFilterMetadata> >(); if (context.ActionContext.ActionDescriptor.FilterDescriptors != null) { foreach (var filter in filters) { var filterItem = new FilterItem(new FilterDescriptor(filter, FilterScope.Global), filter); context.Results.Add(filterItem); } } }
private List <FilterItem> FetchFilters(FilterProviderContext context) { lock (locker) { List <FilterItem> filters; if (!ActionFilterMap.TryGetValue(context.ActionContext.ActionDescriptor.DisplayName, out filters)) { filters = new List <FilterItem>(); FilterItem filterItem; // Authorzation is a special case. if (RequiresAuth(context)) { filterItem = TypeToFilterItem[_settings.Value.Authorization.Filter]; filters.Add(filterItem); } // the following are generic optin and optout foreach (var record in _settings.Value.SimpleMany.OptOut) { var match = record.RouteTree.ContainsMatch(context); if (!match) { filterItem = TypeToFilterItem[record.Filter]; filters.Add(filterItem); } } foreach (var record in _settings.Value.SimpleMany.OptIn) { var match = record.RouteTree.ContainsMatch(context); if (match) { filterItem = TypeToFilterItem[record.Filter]; filters.Add(filterItem); } } ActionFilterMap.Add(context.ActionContext.ActionDescriptor.DisplayName, filters); } return(filters); } }
public void OnProvidersExecuting(FilterProviderContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (context.ActionContext.ActionDescriptor.FilterDescriptors != null) { var factory = context.ActionContext.HttpContext.RequestServices.GetRequiredService <IFeatureActionFilterFactory <TFeature> >(); var results = context.Results; // Perf: Avoid allocating enumerator and read interface .Count once rather than per iteration var resultsCount = results.Count; for (var i = 0; i < resultsCount; i++) { ProvideFilter(results[i], factory); } } }
private bool TryFetchAuthFilterItem(FilterProviderContext context, out FilterItem filterItem) { if (RequiresAuth(context)) { filterItem = TypeToFilterItem[_settings.Value.Authorization.Filter]; return true; } filterItem = null; return false; }
private List<FilterItem> FetchFilters(FilterProviderContext context) { lock (locker) { List<FilterItem> filters; if (!ActionFilterMap.TryGetValue(context.ActionContext.ActionDescriptor.DisplayName, out filters)) { filters = new List<FilterItem>(); FilterItem filterItem; // Authorzation is a special case. if (RequiresAuth(context)) { filterItem = TypeToFilterItem[_settings.Value.Authorization.Filter]; filters.Add(filterItem); } // the following are generic optin and optout foreach (var record in _settings.Value.SimpleMany.OptOut) { var match = record.RouteTree.ContainsMatch(context); if (!match) { filterItem = TypeToFilterItem[record.Filter]; filters.Add(filterItem); } } foreach (var record in _settings.Value.SimpleMany.OptIn) { var match = record.RouteTree.ContainsMatch(context); if (match) { filterItem = TypeToFilterItem[record.Filter]; filters.Add(filterItem); } } ActionFilterMap.Add(context.ActionContext.ActionDescriptor.DisplayName, filters); } return filters; } }
//all framework providers have negative orders, so ours will come later public void OnProvidersExecuting(FilterProviderContext context) { ControllerActionDescriptor cad = (ControllerActionDescriptor) context.ActionContext.ActionDescriptor; System.Diagnostics.Debug.WriteLine("Controller: " + cad.ControllerTypeInfo.FullName); System.Diagnostics.Debug.WriteLine("ActionName: " + cad.Name); System.Diagnostics.Debug.WriteLine("DisplayName: " + cad.DisplayName); System.Diagnostics.Debug.WriteLine("Area: " + context.ActionContext.RouteData.Values["area"]); var filters = FetchFilters(context); foreach (var filter in filters) { context.Results.Add(filter); } }
public void OnProvidersExecuted(FilterProviderContext context) { }
private bool RequiresAuth(FilterProviderContext context) { return !_settings.Value.Authorization.OptOut.RouteTree.ContainsMatch(context); }