Skip to content

Instantly share code, notes, and snippets.

@tiesont
Created May 10, 2014 21:55
Show Gist options
  • Save tiesont/23d371370548b6ce3db2 to your computer and use it in GitHub Desktop.
Save tiesont/23d371370548b6ce3db2 to your computer and use it in GitHub Desktop.
Partial class for plugging into FluentSecurity
public partial class SecurityProvider
{
public static bool ActionIsAllowedForUser(string controllerName, string actionName)
{
var configuration = SecurityConfiguration.Get<MvcConfiguration>();
var policyContainer = configuration.Runtime.PolicyContainers.GetContainerFor(controllerName, actionName);
if (policyContainer != null)
{
var results = policyContainer.EnforcePolicies(configuration.CreateContext());
return results.All(x => x.ViolationOccured == false);
}
// Assume for now that no policies means no restrictions
return true;
}
public static bool HasAnyRole(params string[] roles)
{
var configuration = SecurityConfiguration.Get<MvcConfiguration>();
var context = configuration.CreateContext();
return context.CurrentUserIsAuthenticated() && context.CurrentUserRoles().Intersect(roles) != null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment