Ejemplo n.º 1
0
        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"));
        }
Ejemplo n.º 2
0
        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"));
        }
Ejemplo n.º 3
0
        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"));
        }
Ejemplo n.º 4
0
        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"));
        }
Ejemplo n.º 5
0
        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"));
        }
Ejemplo n.º 7
0
        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"));
        }
Ejemplo n.º 8
0
        // 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));
        }
Ejemplo n.º 9
0
        /// <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);
                }
            }
        }
Ejemplo n.º 10
0
        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);
        }
Ejemplo n.º 11
0
        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)));
        }
Ejemplo n.º 12
0
        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"));
        }