Created
March 30, 2011 19:20
-
-
Save jmarnold/895100 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 class RequiresAuthenticationConvention : IConfigurationAction | |
{ | |
public void Configure(BehaviorGraph graph) | |
{ | |
graph | |
.Actions() | |
.Where(action => action.HasInput && !action.InputType().HasAttribute<AllowUnauthenticatedAccessAttribute>()) | |
.Each(call => | |
{ | |
var log = graph.Observer; | |
if (log.IsRecording) | |
{ | |
log.RecordCallStatus(call, "{0} has an input model that requires authentication.".ToFormat(call)); | |
} | |
var modelType = call.InputType(); | |
var policyType = typeof (AuthorizationPolicy<>).MakeGenericType(modelType); | |
var ruleType = typeof (RequiresAuthenticationRule<>).MakeGenericType(modelType); | |
var policyDef = call | |
.ParentChain() | |
.Authorization | |
.AddPolicy(policyType); | |
var ruleObjectDef = new ObjectDef(ruleType); | |
policyDef.Dependencies.Add(new ConfiguredDependency() | |
{ | |
DependencyType = typeof(IAuthorizationRule<>).MakeGenericType(modelType), | |
Definition = ruleObjectDef | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment