Skip to content

Commit

Permalink
Http.Context passed into various methods due to annoyances
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Chaloner authored and Steve Chaloner committed Mar 27, 2012
1 parent fbb0688 commit e19bca8
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 24 deletions.
6 changes: 3 additions & 3 deletions project-code/app/be/objectify/deadbolt/Deadbolt.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static boolean viewRestrict(List<String[]> roles)
public static boolean viewDynamic(String name,
String meta)
{
DynamicResourceHandler resourceHandler = DEADBOLT_HANDLER.getDynamicResourceHandler();
DynamicResourceHandler resourceHandler = DEADBOLT_HANDLER.getDynamicResourceHandler(Http.Context.current());
boolean allowed = false;
if (resourceHandler == null)
{
Expand All @@ -131,7 +131,7 @@ public static boolean viewDynamic(String name,
*/
public static boolean viewRoleHolderPresent()
{
RoleHolder roleHolder = DEADBOLT_HANDLER.getRoleHolder();
RoleHolder roleHolder = DEADBOLT_HANDLER.getRoleHolder(Http.Context.current());
boolean allowed = false;

if (roleHolder != null)
Expand All @@ -150,7 +150,7 @@ public static boolean viewPattern(String value,
switch (patternType)
{
case REGEX:
allowed = DeadboltAnalyzer.checkRegexPattern(DEADBOLT_HANDLER.getRoleHolder(),
allowed = DeadboltAnalyzer.checkRegexPattern(DEADBOLT_HANDLER.getRoleHolder(Http.Context.current()),
getPattern(value));
break;
case TREE:
Expand Down
14 changes: 10 additions & 4 deletions project-code/app/be/objectify/deadbolt/DeadboltHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package be.objectify.deadbolt;

import be.objectify.deadbolt.models.RoleHolder;
import play.mvc.Http;
import play.mvc.Result;

/**
Expand All @@ -31,32 +32,37 @@ public interface DeadboltHandler
* Invoked immediately before controller or view restrictions are checked. This forms the integration with any
* authentication actions that may need to occur.
*
* @param context the HTTP context
* @return the action result if an action other than the delegate must be taken, otherwise null. For a case where
* the user is authenticated (or whatever your test condition is), this will be null otherwise the restriction
* won't be applied.
*/
Result beforeRoleCheck();
Result beforeRoleCheck(Http.Context context);

/**
* Gets the current {@link RoleHolder}, e.g. the current user.
*
* @param context the HTTP context
* @return the current role holder
*/
RoleHolder getRoleHolder();
RoleHolder getRoleHolder(Http.Context context);

/**
* Invoked when an access failure is detected on <i>controllerClassName</i>.
*
* @param context the HTTP context
* @param content the content type hint. This can be used to return a response in the appropriate content
* type, e.g. JSON
* @return the action result
*/
Result onAccessFailure(String content);
Result onAccessFailure(Http.Context context,
String content);

/**
* Gets the handler used for dealing with resources restricted to specific users/groups.
*
* @param context the HTTP context
* @return the handler for restricted resources. May be null.
*/
DynamicResourceHandler getDynamicResourceHandler();
DynamicResourceHandler getDynamicResourceHandler(Http.Context context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ protected Result onAccessFailure(DeadboltHandler deadboltHandler,
{
Logger.warn(String.format("Deadbolt: Access failure on [%s]",
ctx.request().uri()));
return deadboltHandler.onAccessFailure(content);
return deadboltHandler.onAccessFailure(ctx,
content);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Result call(Http.Context ctx) throws Throwable
else
{
DeadboltHandler deadboltHandler = getDeadboltHandler(getDeadboltHandlerClass());
result = deadboltHandler.beforeRoleCheck();
result = deadboltHandler.beforeRoleCheck(ctx);

if (result == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Result call(Http.Context ctx) throws Throwable
else
{
DeadboltHandler deadboltHandler = getDeadboltHandler(configuration.value());
result = deadboltHandler.beforeRoleCheck();
result = deadboltHandler.beforeRoleCheck(ctx);

if (result == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class DynamicAction extends AbstractRestrictiveAction<Dynamic>
public Result applyRestriction(Http.Context ctx,
DeadboltHandler deadboltHandler) throws Throwable
{
DynamicResourceHandler resourceHandler = deadboltHandler.getDynamicResourceHandler();
DynamicResourceHandler resourceHandler = deadboltHandler.getDynamicResourceHandler(ctx);
Result result;

if (resourceHandler == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ public static final RoleHolder getRoleHolder(DeadboltHandler deadboltHandler,
}
else
{
roleHolder = deadboltHandler.getRoleHolder();
roleHolder = deadboltHandler.getRoleHolder(ctx);
ctx.args.put(Deadbolt.CACHE_USER,
roleHolder);
}
}
else
{
roleHolder = deadboltHandler.getRoleHolder();
roleHolder = deadboltHandler.getRoleHolder(ctx);
}

return roleHolder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import be.objectify.deadbolt.DynamicResourceHandler;
import be.objectify.deadbolt.models.RoleHolder;
import models.User;
import play.mvc.Http;
import play.mvc.Result;
import views.html.accessFailed;

Expand All @@ -27,26 +28,27 @@
*/
public class MyAlternativeDeadboltHandler extends AbstractDeadboltHandler
{
public Result beforeRoleCheck()
public Result beforeRoleCheck(Http.Context context)
{
// returning null means that everything is OK. Return a real result if you want a redirect to a login page or
// somewhere else
return null;
}

public RoleHolder getRoleHolder()
public RoleHolder getRoleHolder(Http.Context context)
{
// in a real application, the user name would probably be in the session following a login process
return User.findByUserName("steve");
}

public DynamicResourceHandler getDynamicResourceHandler()
public DynamicResourceHandler getDynamicResourceHandler(Http.Context context)
{
return new MyAlternativeDynamicResourceHandler();
}

@Override
public Result onAccessFailure(String content)
public Result onAccessFailure(Http.Context context,
String content)
{
// you can return any result from here - forbidden, etc
return ok(accessFailed.render());
Expand Down
10 changes: 6 additions & 4 deletions samples/java/deadbolt-usage/app/security/MyDeadboltHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import be.objectify.deadbolt.DynamicResourceHandler;
import be.objectify.deadbolt.models.RoleHolder;
import models.User;
import play.mvc.Http;
import play.mvc.Result;

import views.html.accessFailed;
Expand All @@ -28,26 +29,27 @@
*/
public class MyDeadboltHandler extends AbstractDeadboltHandler
{
public Result beforeRoleCheck()
public Result beforeRoleCheck(Http.Context context)
{
// returning null means that everything is OK. Return a real result if you want a redirect to a login page or
// somewhere else
return null;
}

public RoleHolder getRoleHolder()
public RoleHolder getRoleHolder(Http.Context context)
{
// in a real application, the user name would probably be in the session following a login process
return User.findByUserName("steve");
}

public DynamicResourceHandler getDynamicResourceHandler()
public DynamicResourceHandler getDynamicResourceHandler(Http.Context context)
{
return new MyDynamicResourceHandler();
}

@Override
public Result onAccessFailure(String content)
public Result onAccessFailure(Http.Context context,
String content)
{
// you can return any result from here - forbidden, etc
return ok(accessFailed.render());
Expand Down
4 changes: 2 additions & 2 deletions samples/java/deadbolt-usage/project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import PlayProject._
object ApplicationBuild extends Build {

val appName = "deadbolt-usage"
val appVersion = "1.1"
val appVersion = "1.1-SNAPSHOT"

val appDependencies = Seq(
"be.objectify" % "deadbolt-2_2.9.1" % "1.1"
"be.objectify" % "deadbolt-2_2.9.1" % "1.1-SNAPSHOT"
)

val main = PlayProject(appName, appVersion, appDependencies, mainLang = JAVA).settings(
Expand Down
2 changes: 1 addition & 1 deletion samples/scala/deadbolt-usage-scala/project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ object ApplicationBuild extends Build {
val appVersion = "1.1"

val appDependencies = Seq(
"be.objectify" % "deadbolt-2_2.9.1" % "1.1"
"be.objectify" % "deadbolt-2_2.9.1" % "1.1-SNAPSHOT"
)

val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings(
Expand Down

0 comments on commit e19bca8

Please sign in to comment.