-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathSmartModelBinder.cs
More file actions
147 lines (123 loc) · 4.66 KB
/
SmartModelBinder.cs
File metadata and controls
147 lines (123 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web.Mvc;
using SmartStore.Core.Html;
using SmartStore.Web.Framework.Security;
namespace SmartStore.Web.Framework.Modelling
{
public class SmartModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var modelType = bindingContext.ModelType;
if (modelType == typeof(CustomPropertiesDictionary))
{
return BindCustomPropertiesDictioary(controllerContext, bindingContext);
}
var model = base.BindModel(controllerContext, bindingContext);
if (model is ModelBase)
{
((ModelBase)model).BindModel(controllerContext, bindingContext);
}
return model;
}
protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor property, object value)
{
var attrs = property.Attributes;
var sanitizeHtmlAttr = attrs.OfType<SanitizeHtmlAttribute>().FirstOrDefault();
if (property.PropertyType == typeof(string) && sanitizeHtmlAttr != null && value is string input && !string.IsNullOrWhiteSpace(input))
{
value = HtmlUtils.SanitizeHtml(input, sanitizeHtmlAttr.IsFragment);
}
base.SetProperty(controllerContext, bindingContext, property, value);
}
private CustomPropertiesDictionary BindCustomPropertiesDictioary(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var model = bindingContext.Model as CustomPropertiesDictionary ?? new CustomPropertiesDictionary();
var keys = GetValueProviderKeys(controllerContext, bindingContext.ModelName + "[");
if (keys.Count == 0)
{
return model;
}
foreach (var key in keys)
{
var keyName = GetKeyName(key);
if (keyName == null || model.ContainsKey(keyName))
continue;
var valueBinder = this.Binders.DefaultBinder;
var subPropertyName = GetSubPropertyName(key);
if (subPropertyName.IsCaseInsensitiveEqual("__Type__"))
continue;
if (subPropertyName == null)
{
var simpleBindingContext = new ModelBindingContext
{
ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, GetValueType(keys, key, bindingContext.ValueProvider)),
ModelName = key,
ModelState = bindingContext.ModelState,
PropertyFilter = bindingContext.PropertyFilter,
ValueProvider = bindingContext.ValueProvider
};
var value = valueBinder.BindModel(controllerContext, simpleBindingContext);
model[keyName] = value;
}
else
{
// Is Complex type
var modelName = key.Substring(0, key.Length - subPropertyName.Length - 1);
var valueType = GetValueType(keys, modelName, bindingContext.ValueProvider);
valueBinder = this.Binders.GetBinder(valueType);
var complexBindingContext = new ModelBindingContext
{
ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, valueType),
ModelName = key.Substring(0, key.Length - subPropertyName.Length - 1),
ModelState = bindingContext.ModelState,
PropertyFilter = bindingContext.PropertyFilter,
ValueProvider = bindingContext.ValueProvider
};
var value = valueBinder.BindModel(controllerContext, complexBindingContext);
model[keyName] = value;
}
}
return model;
}
private HashSet<string> GetValueProviderKeys(ControllerContext context, string prefix)
{
var keys = context.HttpContext.Request.Form.Keys.Cast<string>()
.Concat(((IDictionary<string, object>)context.RouteData.Values).Keys.Cast<string>())
.Concat(context.HttpContext.Request.QueryString.Keys.Cast<string>())
.Concat(context.HttpContext.Request.Files.Keys.Cast<string>())
.Where(x => x.StartsWith(prefix, StringComparison.InvariantCultureIgnoreCase));
return new HashSet<string>(keys, StringComparer.InvariantCultureIgnoreCase);
}
private string GetKeyName(string key)
{
int startBracket = key.IndexOf("[");
int endBracket = key.IndexOf("]", startBracket);
if (endBracket == -1)
return null;
return key.Substring(startBracket + 1, endBracket - startBracket - 1);
}
private string GetSubPropertyName(string key)
{
var parts = key.Split('.');
if (parts.Length > 1)
{
return parts[1];
}
return null;
}
private Type GetValueType(HashSet<string> keys, string prefix, IValueProvider valueProvider)
{
var typeKey = prefix + ".__Type__";
if (keys.Contains(typeKey))
{
var type = Type.GetType(valueProvider.GetValue(typeKey).AttemptedValue, true);
return type;
}
return typeof(object);
}
}
}