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);
            }
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        public void PrefixPropertyDefaultsToNull() {
            // Arrange
            BindAttribute attr = new BindAttribute();

            // Act & assert
            Assert.IsNull(attr.Prefix);
        }
Exemplo n.º 4
0
        public void PrefixProperty() {
            // Arrange
            BindAttribute attr = new BindAttribute { Prefix = "somePrefix" };

            // Act & assert
            Assert.AreEqual("somePrefix", attr.Prefix);
        }
Exemplo n.º 5
0
        public void ExcludePropertyDefaultsToEmptyString() {
            // Arrange
            BindAttribute attr = new BindAttribute { Exclude = null };

            // Act & assert
            Assert.AreEqual(String.Empty, attr.Exclude);
        }
        // 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);
        }
Exemplo 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);
        }
Exemplo n.º 8
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"));
        }
Exemplo n.º 9
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"));
        }
Exemplo n.º 10
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"));
        }
Exemplo n.º 11
0
 private Predicate<string> GetPropertyFilter(ParameterDescriptor parameterDescriptor)
 {
     ParameterBindingInfo bindingInfo = parameterDescriptor.BindingInfo;
     var ba = new BindAttribute()
     {
         Exclude = string.Join(",", bindingInfo.Exclude),
         Include = string.Join(",", bindingInfo.Include)
     };
     return ba.IsPropertyAllowed;
 }
Exemplo n.º 12
0
        private static Predicate <string> GetPropertyFilter(ParameterDescriptor parameterDescriptor)
        {
            ParameterBindingInfo bindingInfo = parameterDescriptor.BindingInfo;

            return(propertyName =>
                   BindAttribute.IsPropertyAllowed(
                       propertyName,
                       bindingInfo.Include,
                       bindingInfo.Exclude
                       ));
        }
Exemplo n.º 13
0
        private void ReadSettingsFromBindAttribute()
        {
            BindAttribute attr = (BindAttribute)Attribute.GetCustomAttribute(_parameterInfo, typeof(BindAttribute));

            if (attr == null)
            {
                return;
            }

            _exclude = new ReadOnlyCollection <string>(AuthorizeAttribute.SplitString(attr.Exclude));
            _include = new ReadOnlyCollection <string>(AuthorizeAttribute.SplitString(attr.Include));
            _prefix  = attr.Prefix;
        }
Exemplo n.º 14
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);
        }