Search code examples
asp.net-mvc-3mvcsitemapproviderfluent-security

FluentSecurity with MVCSiteMapProvider


I was hoping to use this with MvcSiteMapProvider to hide / show menu items instead of doubling up and defining roles in my mvc.sitemap file.

I have gone through the source for 2.0alpha1 but can't seem to figure out how to do something like:

bool hasAccess = SecurityConfiguration.Current.HasAccess(controller, action, area)

Can anyone point me in the right direction?

Thanks


Solution

  • I was able to solve this with the help of kristoffer-ahl on the actual github project page

    Here is the solution

    public static bool ActionIsAllowedForUser(string area, string controllerName, string actionName)
    {
        var configuration = SecurityConfiguration.Current;
    
        string fullControllerName = string.Format("Web.Controllers.{0}Controller", controllerName);
        if (!string.IsNullOrEmpty(area))
        {
            fullControllerName = string.Format("Web.Areas.{0}.Controllers.{1}Controller", area, controllerName);
        }
    
        var policyContainer = configuration.PolicyContainers.GetContainerFor(fullControllerName, actionName);
        if (policyContainer != null)
        {
            var context = SecurityContext.Current;
            var results = policyContainer.EnforcePolicies(context);
            return results.All(x => x.ViolationOccured == false);
        }
        return true;
    }