Skip to content

Commit 6084fef

Browse files
committed
Improvements to cookie filter handling
1 parent d29dd72 commit 6084fef

File tree

3 files changed

+56
-35
lines changed

3 files changed

+56
-35
lines changed

Build/CommonAssemblyInfo.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
//
1616
// You can specify all the values or you can default the Revision and Build Numbers
1717
// by using the '*' as shown below:
18-
[assembly: AssemblyVersion("3.2.19")]
19-
[assembly: AssemblyFileVersion("3.2.19")]
18+
[assembly: AssemblyVersion("3.2.20")]
19+
[assembly: AssemblyFileVersion("3.2.20")]
2020
//[assembly: AssemblyInformationalVersion("2.5-filters")]

Griddly.Mvc/GriddlyCookieFilterValueProvider.cs

+27-25
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public bool ContainsPrefix(string prefix)
2828

2929
public ValueProviderResult GetValue(string key)
3030
{
31+
int pos = key.LastIndexOf(".", StringComparison.Ordinal);
32+
if (pos != -1)
33+
key = key.Substring(pos + 1);
34+
3135
string[] value = null;
3236

3337
if (_context.CookieData.Values?.TryGetValue(key, out value) != true)
@@ -44,44 +48,42 @@ public ValueProviderResult GetValue(string key)
4448

4549
public class GriddlyCookieFilterValueProviderFactory : ValueProviderFactory
4650
{
47-
Func<ControllerContext, bool> _canProvide = null;
51+
Func<ControllerContext, bool> _canProvide = (controllerContext) => controllerContext.HttpContext.Request.QueryString.Count == 0;
4852

4953
public GriddlyCookieFilterValueProviderFactory(Func<ControllerContext, bool> canProvide = null)
5054
{
51-
_canProvide = canProvide;
55+
if (canProvide != null)
56+
_canProvide = canProvide;
5257
}
5358

5459
public override IValueProvider GetValueProvider(ControllerContext controllerContext)
5560
{
56-
if (controllerContext.IsChildAction && controllerContext.HttpContext.Request.QueryString.Count == 0)
61+
if (controllerContext.IsChildAction && _canProvide.Invoke(controllerContext))
5762
{
58-
if (_canProvide?.Invoke(controllerContext) != false)
59-
{
60-
var context = controllerContext.Controller.GetOrCreateGriddlyContext();
61-
var cookie = controllerContext.HttpContext.Request.Cookies[context.CookieName];
63+
var context = controllerContext.Controller.GetOrCreateGriddlyContext();
64+
var cookie = controllerContext.HttpContext.Request.Cookies[context.CookieName];
6265

63-
if (cookie != null && !string.IsNullOrWhiteSpace(cookie.Value))
66+
if (cookie != null && !string.IsNullOrWhiteSpace(cookie.Value))
67+
{
68+
try
6469
{
65-
try
66-
{
67-
var data = JsonConvert.DeserializeObject<GriddlyFilterCookieData>(cookie.Value);
68-
69-
// chrome/ff don't delete session cookies if they're set to "continue where you left off"
70-
// https://stackoverflow.com/questions/10617954/chrome-doesnt-delete-session-cookies
71-
// only use a cookie if it's new within 100 minutes
72-
if (data.CreatedUtc != null && (DateTime.UtcNow - data.CreatedUtc.Value).TotalMinutes < 100)
73-
{
74-
context.CookieData = data;
75-
context.IsDefaultSkipped = true;
76-
77-
return new GriddlyCookieFilterValueProvider(context);
78-
}
79-
}
80-
catch
70+
var data = JsonConvert.DeserializeObject<GriddlyFilterCookieData>(cookie.Value);
71+
72+
// chrome/ff don't delete session cookies if they're set to "continue where you left off"
73+
// https://stackoverflow.com/questions/10617954/chrome-doesnt-delete-session-cookies
74+
// only use a cookie if it's new within 100 minutes
75+
if (data.CreatedUtc != null && (DateTime.UtcNow - data.CreatedUtc.Value).TotalMinutes < 100)
8176
{
82-
// TODO: log it?
77+
context.CookieData = data;
78+
context.IsDefaultSkipped = true;
79+
80+
return new GriddlyCookieFilterValueProvider(context);
8381
}
8482
}
83+
catch
84+
{
85+
// TODO: log it?
86+
}
8587
}
8688
}
8789

Griddly.Mvc/GriddlyParameterAttribute.cs

+27-8
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ public override void OnActionExecuting(ActionExecutingContext filterContext)
3535
context.IsDeepLink = true;
3636
}
3737

38-
foreach (var param in filterContext.ActionParameters.ToList())
38+
var actionParams = new List<KeyValuePair<string, object>>(filterContext.ActionParameters);
39+
//add any properties of model classes
40+
foreach (var ap in filterContext.ActionParameters.Where(x => x.Value?.GetType().IsClass == true))
41+
foreach (var pi in ap.Value.GetType().GetProperties().Where(x => x.CanRead))
42+
actionParams.Add(new KeyValuePair<string, object>(pi.Name, pi.GetValue(ap.Value)));
43+
44+
foreach (var param in actionParams)
3945
{
4046
if (param.Value != null && (param.Value.GetType().IsValueType || typeof(IEnumerable).IsAssignableFrom(param.Value.GetType())))
4147
{
@@ -97,14 +103,14 @@ public override void OnActionExecuted(ActionExecutedContext filterContext)
97103
// now, we could use the context.Parameters... but the raw string values seems more like what we want here...
98104
foreach (var param in filterContext.ActionDescriptor.GetParameters())
99105
{
100-
var valueResult = filterContext.Controller.ValueProvider.GetValue(param.ParameterName);
101-
102-
if (valueResult?.RawValue != null)
106+
if (param.ParameterType.IsClass)
107+
{
108+
foreach (var pi in param.ParameterType.GetProperties())
109+
AddParameter(filterContext, data, pi.Name);
110+
}
111+
else
103112
{
104-
if (valueResult.RawValue is string[] array)
105-
data.Values[param.ParameterName] = array;
106-
else
107-
data.Values[param.ParameterName] = new[] { valueResult.RawValue.ToString() };
113+
AddParameter(filterContext, data, param.ParameterName);
108114
}
109115
}
110116

@@ -132,5 +138,18 @@ public override void OnActionExecuted(ActionExecutedContext filterContext)
132138

133139
base.OnActionExecuted(filterContext);
134140
}
141+
142+
void AddParameter(ActionExecutedContext filterContext, GriddlyFilterCookieData data, string parameterName)
143+
{
144+
var valueResult = filterContext.Controller.ValueProvider.GetValue(parameterName);
145+
146+
if (valueResult?.RawValue != null)
147+
{
148+
if (valueResult.RawValue is string[] array)
149+
data.Values[parameterName] = array;
150+
else
151+
data.Values[parameterName] = new[] { valueResult.RawValue.ToString() };
152+
}
153+
}
135154
}
136155
}

0 commit comments

Comments
 (0)