Skip to content

Commit

Permalink
Fix for issue maartenba#14 (Use GET to determine node accessibility).
Browse files Browse the repository at this point in the history
Adds 'routeMethod' attribute to sitemap provider initialization attributes. The value set in the attribute is used when determining current route. If the value is empty or not specified the method of the current request is used.
  • Loading branch information
Max Kiselev committed Aug 8, 2012
1 parent e7fc03c commit 1b72c3a
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/MvcSiteMapProvider/MvcMusicStore/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
controllerTypeResolver="MvcSiteMapProvider.DefaultControllerTypeResolver, MvcSiteMapProvider"
actionMethodParameterResolver="MvcSiteMapProvider.DefaultActionMethodParameterResolver, MvcSiteMapProvider"
aclModule="MvcSiteMapProvider.DefaultAclModule, MvcSiteMapProvider"
routeMethod=""
siteMapNodeUrlResolver="MvcSiteMapProvider.DefaultSiteMapNodeUrlResolver, MvcSiteMapProvider"
siteMapNodeVisibilityProvider="MvcSiteMapProvider.DefaultSiteMapNodeVisibilityProvider, MvcSiteMapProvider"
siteMapProviderEventHandler="MvcSiteMapProvider.DefaultSiteMapProviderEventHandler, MvcSiteMapProvider"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public bool IsAccessibleToUser(IControllerTypeResolver controllerTypeResolver, D
}

// Find routes for the sitemap node's url
HttpContextBase httpContext = new HttpContextWrapper(context);
HttpContextBase httpContext = new HttpContextMethodOverrider(context, provider.RouteMethod);
string originalPath = httpContext.Request.Path;
var originalRoutes = RouteTable.Routes.GetRouteData(httpContext);
httpContext.RewritePath(nodeUrl, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ public class DefaultSiteMapProvider
/// <value>The duration of the cache.</value>
public int CacheDuration { get; private set; }

/// <summary>
/// Get or sets the name of HTTP method to use when checking node accessibility.
/// </summary>
/// <value>
/// The name of HTTP method to use when checking node accessibility or null to use
/// the method of the current request. Defaults to null.
/// </value>
public string RouteMethod { get; private set; }

/// <summary>
/// Gets the RootNode for the current SiteMapProvider.
/// </summary>
Expand Down Expand Up @@ -346,6 +355,11 @@ public override void Initialize(string name, NameValueCollection attributes)
new DefaultAclModule();
}

if (!string.IsNullOrEmpty(attributes["routeMethod"]))
{
RouteMethod = attributes["routeMethod"];
}

// Is a SiteMapNode URL resolver given?
if (!string.IsNullOrEmpty(attributes["siteMapNodeUrlResolver"]))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#region Using directives

using System;
using System.Web;

#endregion

namespace MvcSiteMapProvider.External
{
/// <summary>
/// Wraps an <see cref="HttpContext"/> and overrides <see cref="HttpRequestBase.HttpMethod"/>
/// value of the <see cref="Request"/> property.
/// </summary>
public class HttpContextMethodOverrider : HttpContextWrapper
{
private readonly HttpContext httpContext;

private readonly string httpMethod;

/// <summary>
/// Initializes a new instance of the <see cref="HttpContextMethodOverrider"/> class.
/// </summary>
/// <param name="httpContext">The object that this wrapper class provides access to.</param>
/// <param name="httpMethod">
/// The <see cref="HttpRequestBase.HttpMethod"/> override value or <c>null</c> if the value
/// should not be overriden.
/// </param>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="httpContext"/> is null.
/// </exception>
public HttpContextMethodOverrider(HttpContext httpContext, string httpMethod)
: base(httpContext)
{
this.httpContext = httpContext;
this.httpMethod = httpMethod;
}

/// <summary>
/// Gets the <see cref="T:System.Web.HttpRequestBase"/> object for the current HTTP request.
/// </summary>
/// <value></value>
/// <returns>
/// The current HTTP request.
/// </returns>
public override HttpRequestBase Request
{
get { return new HttpRequestMethodOverrider(this.httpContext.Request, httpMethod); }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#region Using directives

using System;
using System.Reflection;
using System.Web;

#endregion

namespace MvcSiteMapProvider.External
{
/// <summary>
/// Wraps an <see cref="HttpRequest"/> and overrides <see cref="HttpMethod"/> value.
/// </summary>
public class HttpRequestMethodOverrider : HttpRequestWrapper
{
private readonly string httpMethod;

/// <summary>
/// Initializes a new instance of the <see cref="HttpRequest2"/> class.
/// </summary>
/// <param name="httpRequest">The object that this wrapper class provides access to.</param>
/// <param name="httpMethod">The <see cref="HttpMethod"/></param>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="httpRequest"/> is null.
/// </exception>
public HttpRequestMethodOverrider(HttpRequest httpRequest, string httpMethod)
: base(httpRequest)
{
this.httpMethod = httpMethod;
}

/// <summary>
/// Gets the HTTP data-transfer method (such as GET, POST, or HEAD) that was used by the client.
/// </summary>
/// <returns>
/// The HTTP data-transfer method that was used by the client.
/// </returns>
public override string HttpMethod
{
get { return this.httpMethod ?? base.HttpMethod; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
<Compile Include="AuthorizeAttributeAclModule.cs" />
<Compile Include="AclModuleNotSupportedException.cs" />
<Compile Include="ControllerExtensions.cs" />
<Compile Include="External\HttpContextMethodOverrider.cs" />
<Compile Include="External\HttpRequestMethodOverrider.cs" />
<Compile Include="External\Dynamic.cs" />
<Compile Include="External\ObjectCopier.cs" />
<Compile Include="Filters\AttributeTarget.cs" />
Expand Down

0 comments on commit 1b72c3a

Please sign in to comment.