Skip to content

Instantly share code, notes, and snippets.

@jmarnold
Created March 30, 2011 19:20
Show Gist options
  • Save jmarnold/895100 to your computer and use it in GitHub Desktop.
Save jmarnold/895100 to your computer and use it in GitHub Desktop.
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