Created
May 10, 2014 21:55
-
-
Save tiesont/23d371370548b6ce3db2 to your computer and use it in GitHub Desktop.
Partial class for plugging into FluentSecurity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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