Ejemplo n.º 1
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"));
        }
Ejemplo n.º 2
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.º 3
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.º 4
0
        protected internal bool TryUpdateModel <TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties, IValueProvider valueProvider) where TModel : class
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }
            if (valueProvider == null)
            {
                throw new ArgumentNullException("valueProvider");
            }

            Predicate <string> propertyFilter = propertyName => BindAttribute.IsPropertyAllowed(propertyName, includeProperties, excludeProperties);
            IModelBinder       binder         = Binders.GetBinder(typeof(TModel));

            ModelBindingContext bindingContext = new ModelBindingContext()
            {
                ModelMetadata  = ModelMetadataProviders.Current.GetMetadataForType(() => model, typeof(TModel)),
                ModelName      = prefix,
                ModelState     = ModelState,
                PropertyFilter = propertyFilter,
                ValueProvider  = valueProvider
            };

            binder.BindModel(ControllerContext, bindingContext);
            return(ModelState.IsValid);
        }
        internal void BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, object model)
        {
            // need to replace the property filter + model object and create an inner binding context
            BindAttribute      bindAttr          = (BindAttribute)TypeDescriptor.GetAttributes(bindingContext.ModelType)[typeof(BindAttribute)];
            Predicate <string> newPropertyFilter = (bindAttr != null)
                                              ? propertyName => bindAttr.IsPropertyAllowed(propertyName) && bindingContext.PropertyFilter(propertyName)
                                              : bindingContext.PropertyFilter;

            ModelBindingContext newBindingContext = new ModelBindingContext()
            {
                Model          = model,
                ModelName      = bindingContext.ModelName,
                ModelState     = bindingContext.ModelState,
                ModelType      = bindingContext.ModelType,
                PropertyFilter = newPropertyFilter,
                ValueProvider  = bindingContext.ValueProvider
            };

            // validation
            if (OnModelUpdating(controllerContext, newBindingContext))
            {
                BindProperties(controllerContext, newBindingContext);
                OnModelUpdated(controllerContext, newBindingContext);
            }
        }
        // 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.º 7
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);
        }
        private static Predicate <string> GetPropertyFilter(ParameterDescriptor parameterDescriptor)
        {
            ParameterBindingInfo bindingInfo = parameterDescriptor.BindingInfo;

            return(propertyName =>
                   BindAttribute.IsPropertyAllowed(
                       propertyName,
                       bindingInfo.Include,
                       bindingInfo.Exclude
                       ));
        }
Ejemplo n.º 9
0
        protected internal bool TryUpdateModel <TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties, IValueProvider valueProvider) where TModel : class
        {
            if ((object)model == null)
            {
                throw new ArgumentNullException("model");
            }
            if (valueProvider == null)
            {
                throw new ArgumentNullException("valueProvider");
            }
            Predicate <string> predicate = (Predicate <string>)(propertyName => BindAttribute.IsPropertyAllowed(propertyName, includeProperties, excludeProperties));

            this.Binders.GetBinder(typeof(TModel)).BindModel(this.ControllerContext, new ModelBindingContext()
            {
                ModelMetadata  = ModelMetadataProviders.Current.GetMetadataForType((Func <object>)(() => (object)(TModel)model), typeof(TModel)),
                ModelName      = prefix,
                ModelState     = this.ModelState,
                PropertyFilter = predicate,
                ValueProvider  = valueProvider
            });
            return(this.ModelState.IsValid);
        }