public void IsPropertyAllowedReturnsTrueAlwaysIfBindPropertiesIsAll() { // Setup BindAttribute attr = new BindAttribute(); // Act & assert Assert.IsTrue(attr.IsPropertyAllowed("foo")); Assert.IsTrue(attr.IsPropertyAllowed("bar")); Assert.IsTrue(attr.IsPropertyAllowed("baz")); }
public void IsPropertyAllowedReturnsTrueForWhitelistedPropertiesIfBindPropertiesIsInclude() { // Setup BindAttribute attr = new BindAttribute { Include = "FOO,BAR" }; // Act & assert Assert.True(attr.IsPropertyAllowed("foo")); Assert.True(attr.IsPropertyAllowed("bar")); Assert.False(attr.IsPropertyAllowed("baz")); }
public void IsPropertyAllowedReturnsTrueAlwaysIfBindPropertiesIsAll() { // Setup BindAttribute attr = new BindAttribute(); // Act & assert Assert.True(attr.IsPropertyAllowed("foo")); Assert.True(attr.IsPropertyAllowed("bar")); Assert.True(attr.IsPropertyAllowed("baz")); }
public void IsPropertyAllowedReturnsFalseForBlacklistedPropertiesIfBindPropertiesIsExclude() { // Setup BindAttribute attr = new BindAttribute { Exclude = "FOO,BAZ" }; // Act & assert Assert.False(attr.IsPropertyAllowed("foo")); Assert.True(attr.IsPropertyAllowed("bar")); Assert.False(attr.IsPropertyAllowed("baz")); }
public void IsPropertyAllowedReturnsTrueForWhitelistedPropertiesIfBindPropertiesIsInclude() { // Setup BindAttribute attr = new BindAttribute { Include = "FOO,BAR" }; // Act & assert Assert.IsTrue(attr.IsPropertyAllowed("foo")); Assert.IsTrue(attr.IsPropertyAllowed("bar")); Assert.IsFalse(attr.IsPropertyAllowed("baz")); }
public void IsPropertyAllowedReturnsFalseForBlacklistedPropertiesIfBindPropertiesIsExclude() { // Setup BindAttribute attr = new BindAttribute { Exclude = "FOO,BAZ" }; // Act & assert Assert.False(attr.IsPropertyAllowed("foo")); Assert.True(attr.IsPropertyAllowed("bar")); Assert.False(attr.IsPropertyAllowed("baz")); }
public void IsPropertyAllowedReturnsFalseForBlacklistOverridingWhitelistedProperties() { // Setup BindAttribute attr = new BindAttribute { Include = "FOO,BAR", Exclude = "bar,QUx" }; // Act & assert Assert.IsTrue(attr.IsPropertyAllowed("foo")); Assert.IsFalse(attr.IsPropertyAllowed("bar")); Assert.IsFalse(attr.IsPropertyAllowed("baz")); Assert.IsFalse(attr.IsPropertyAllowed("qux")); }
// add property filter described by BindAttribute, override prefix protected override object BindCslaModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { if (string.IsNullOrEmpty(BindCriteria.Include) && string.IsNullOrEmpty(BindCriteria.Exclude) && string.IsNullOrEmpty(BindCriteria.Prefix)) { return(base.BindCslaModel(controllerContext, bindingContext)); } Predicate <string> propFilter = bindingContext.PropertyFilter; if (!string.IsNullOrEmpty(BindCriteria.Include) || !string.IsNullOrEmpty(BindCriteria.Exclude)) { var bindAttr = new BindAttribute() { Exclude = BindCriteria.Exclude, Include = BindCriteria.Include }; propFilter = (propName) => bindAttr.IsPropertyAllowed(propName) && bindingContext.PropertyFilter(propName); } var newPrefix = BindCriteria.Prefix ?? bindingContext.ModelName; var newBindingContext = new ModelBindingContext() { Model = bindingContext.Model, ModelName = newPrefix, ModelState = bindingContext.ModelState, ModelType = bindingContext.ModelType, PropertyFilter = propFilter, ValueProvider = bindingContext.ValueProvider }; return(base.BindCslaModel(controllerContext, newBindingContext)); }
/// <summary> /// Reflect into the routeValueObject and remove any keys from /// routeValues that are not bound by the Bind attribute /// </summary> private static void RemoveUnboundValues(RouteValueDictionary routeValues , object source) { if (source == null) { return; } Type type = source.GetType(); BindAttribute b = type.GetCustomAttributes(true).OfType <BindAttribute>().FirstOrDefault(); if (b == null) { return; } foreach (PropertyInfo property in type.GetProperties()) { string propertyName = property.Name; if (!b.IsPropertyAllowed(propertyName)) { routeValues.Remove(propertyName); } } }
internal ModelBindingContext CreateComplexElementalModelBindingContext(ControllerContext controllerContext, ModelBindingContext bindingContext, object model) { BindAttribute bindAttr = (BindAttribute)GetTypeDescriptor(controllerContext, bindingContext).GetAttributes()[typeof(BindAttribute)]; Predicate <string> newPropertyFilter = (bindAttr != null) ? propertyName => bindAttr.IsPropertyAllowed(propertyName) && bindingContext.PropertyFilter(propertyName) : bindingContext.PropertyFilter; ModelBindingContext newBindingContext = new ModelBindingContext() { ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, bindingContext.ModelType), ModelName = bindingContext.ModelName, ModelState = bindingContext.ModelState, PropertyFilter = newPropertyFilter, ValueProvider = bindingContext.ValueProvider }; return(newBindingContext); }
protected virtual IEnumerable <ModelMetadata> GetMetadataForProperties(ModelBindingContext bindingContext) { var validationInfo = GetPropertyValidationInfo(bindingContext); var propertyTypeMetadata = bindingContext.MetadataProvider .GetMetadataForType(null, bindingContext.ModelType); Predicate <string> newPropertyFilter = propertyName => bindingContext.PropertyFilter(propertyName) && BindAttribute.IsPropertyAllowed( propertyName, propertyTypeMetadata.IncludedProperties, propertyTypeMetadata.ExcludedProperties); return(bindingContext.ModelMetadata.Properties .Where(propertyMetadata => newPropertyFilter(propertyMetadata.PropertyName) && (validationInfo.RequiredProperties.Contains(propertyMetadata.PropertyName) || !validationInfo.SkipProperties.Contains(propertyMetadata.PropertyName)) && CanUpdateProperty(propertyMetadata))); }
public void IsPropertyAllowedReturnsFalseForBlacklistOverridingWhitelistedProperties() { // Setup BindAttribute attr = new BindAttribute { Include = "FOO,BAR", Exclude = "bar,QUx" }; // Act & assert Assert.True(attr.IsPropertyAllowed("foo")); Assert.False(attr.IsPropertyAllowed("bar")); Assert.False(attr.IsPropertyAllowed("baz")); Assert.False(attr.IsPropertyAllowed("qux")); }