Skip to content
This repository has been archived by the owner on Dec 9, 2022. It is now read-only.

Commit

Permalink
Modified Configuration implementation to accept void Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
chandu committed Sep 18, 2013
1 parent e874d2d commit f3ecd17
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 3 deletions.
103 changes: 103 additions & 0 deletions FluentSecurity.Specification/ConfigurationExpressionSpec.cs
Expand Up @@ -141,6 +141,109 @@ public ActionResult ActualAction()
}
}

[TestFixture]
[Category("ConfigurationExpressionSpec")]
public class When_adding_a_policycontainter_for_void_action
{
[Test]
public void Should_have_policycontainer_for_controller_with_void_action()
{
// Arrange
var configurationExpression = new RootConfiguration();
configurationExpression.GetAuthenticationStatusFrom(StaticHelper.IsAuthenticatedReturnsFalse);

// Act
configurationExpression.For<ParentVoidActionController>(x => x.VoidAction());

// Assert
Assert.That(configurationExpression.Runtime.PolicyContainers.Count(), Is.EqualTo(1));
}

[Test]
public void Should_have_policycontainer_for_void_action_from_parent_controller()
{
// Arrange
var configurationExpression = new RootConfiguration();
configurationExpression.GetAuthenticationStatusFrom(StaticHelper.IsAuthenticatedReturnsFalse);

// Act
configurationExpression.For<ChildVoidActionController>(x => x.VoidAction());

// Assert
Assert.That(configurationExpression.Runtime.PolicyContainers.Count(), Is.EqualTo(1));
}

private class ParentVoidActionController : Controller
{
public void VoidAction()
{

}
}

private class ChildVoidActionController:ParentVoidActionController
{

}
}

[Category("ConfigurationExpressionSpec")]
public class When_adding_a_policycontainter_using_ByController_convention
{
[Test]
public void Should_have_policycontainer_for_all_actions_including_void_actions()
{
// Arrange
var configurationExpression = new RootConfiguration();
configurationExpression.GetAuthenticationStatusFrom(StaticHelper.IsAuthenticatedReturnsFalse);

// Act
configurationExpression.For<ParentVoidActionController>();

// Assert
Assert.That(configurationExpression.Runtime.PolicyContainers.Count(), Is.EqualTo(2));
}

public void Should_have_policycontainer_for_all_actions_including_inherited_void_actions()
{
// Arrange
var configurationExpression = new RootConfiguration();
configurationExpression.GetAuthenticationStatusFrom(StaticHelper.IsAuthenticatedReturnsFalse);

// Act
configurationExpression.For<ChildVoidActionController>();

// Assert
Assert.That(configurationExpression.Runtime.PolicyContainers.Count(), Is.EqualTo(2));
}

private class ParentVoidActionController : Controller
{
public void VoidAction()
{

}

public ActionResult DummyAction()
{
return new EmptyResult();
}
}

private class ChildVoidActionController : ParentVoidActionController
{
public void VoidAction()
{

}

public ActionResult DummyAction()
{
return new EmptyResult();
}
}
}

[TestFixture]
[Category("ConfigurationExpressionSpec")]
public class When_adding_a_policycontainter_for_Blog_Index_and_AddPost
Expand Down
8 changes: 8 additions & 0 deletions FluentSecurity/ConfigurationExpression.cs
Expand Up @@ -39,6 +39,14 @@ internal void Initialize(SecurityRuntime runtime)
return AddPolicyContainerFor(controllerName, actionName);
}

public IPolicyContainerConfiguration For<TController>(Expression<Action<TController>> actionExpression) where TController : Controller
{
var controllerName = typeof(TController).GetControllerName();
var actionName = actionExpression.GetActionName();

return AddPolicyContainerFor(controllerName, actionName);
}

public IPolicyContainerConfiguration For<TController>() where TController : Controller
{
var controllerType = typeof(TController);
Expand Down
30 changes: 27 additions & 3 deletions FluentSecurity/Extensions.cs
Expand Up @@ -80,21 +80,45 @@ internal static IEnumerable<MethodInfo> GetActionMethods(this Type controllerTyp
return controllerType
.GetMethods(
BindingFlags.Public |
BindingFlags.Instance
BindingFlags.Instance
)
.Where(methodInfo => methodInfo.ReturnType.IsControllerActionReturnType())
.Where(IsValidActionMethod)
.Where(action => actionFilter.Invoke(new ControllerActionInfo(controllerType, action)))
.ToList();
}

internal static bool IsValidActionMethod(this MethodInfo methodInfo)
{
return methodInfo.ReturnType.IsControllerActionReturnType() &&
!methodInfo.IsSpecialName && !methodInfo.IsDeclaredBy<Controller>();
}

/// <summary>
/// Returns true if the passed method is declared by the type T.
/// </summary>
/// <param name="methodInfo"></param>
/// <returns></returns>
//(Chandu) The method below is to simulate the way System.Web.Mvc.ActionMethodSelector.IsValidActionMethod identifies methods of a controller as valid actions
internal static bool IsDeclaredBy<T>(this MethodInfo methodInfo)
{
var passedType = typeof (T);
var declaringType = methodInfo.GetBaseDefinition().DeclaringType;
return declaringType != null && declaringType.IsAssignableFrom(passedType);
}

/// <summary>
/// Returns true if the type matches a controller action return type.
/// </summary>
/// <param name="returnType"></param>
/// <returns></returns>
internal static bool IsControllerActionReturnType(this Type returnType)
{
return typeof (ActionResult).IsAssignableFrom(returnType) || typeof (Task<ActionResult>).IsAssignableFromGenericType(returnType);
return
(
typeof (ActionResult).IsAssignableFrom(returnType) ||
typeof (Task<ActionResult>).IsAssignableFromGenericType(returnType) ||
typeof(void).IsAssignableFrom(returnType)
);
}

/// <summary>
Expand Down

0 comments on commit f3ecd17

Please sign in to comment.