Skip to content

Commit

Permalink
Fix for #71: Context not available in UserAware action
Browse files Browse the repository at this point in the history
  • Loading branch information
jaliss committed Sep 25, 2012
1 parent 2b9d949 commit 5a9a4e9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
30 changes: 21 additions & 9 deletions module-code/app/securesocial/core/java/SecureSocial.java
Expand Up @@ -127,6 +127,15 @@ private static ObjectNode forbiddenJson() {
return result;
}

private static void fixHttpContext(Http.Context ctx) {
// As of Play 2.0.3:
// I don't understand why the ctx is not set in the Http.Context thread local variable.
// I'm setting it by hand so I can retrieve the i18n messages and currentUser() can work.
// will find out later why this is working this way, if you know why this is not set let me know :)
// This is looks like a bug, Play should be setting the context properly.
Http.Context.current.set(ctx);
}

/**
* Protects an action with SecureSocial
*/
Expand All @@ -135,10 +144,7 @@ public static class SecuredAction extends Action<Secured> {
@Override
public Result call(Http.Context ctx) throws Throwable {
try {
// I don't understand why the ctx is not set in the Http.Context thread local variable.
// I'm setting it by hand so I can retrieve the i18n messages and currentUser() can work.
// will find out later why this is working this way, if you know why this is not set let me know :)
Http.Context.current.set(ctx);
fixHttpContext(ctx);
securesocial.core.UserId scalaUserId = getUserIdFromSession(ctx);

if ( scalaUserId == null ) {
Expand Down Expand Up @@ -170,7 +176,7 @@ public Result call(Http.Context ctx) throws Throwable {
}
}
} finally {
// leave it null as it was before, just in case.H
// leave it null as it was before, just in case.
Http.Context.current.set(null);
}
}
Expand All @@ -194,11 +200,17 @@ public Result call(Http.Context ctx) throws Throwable {
public static class UserAwareAction extends Action<UserAware> {
@Override
public Result call(Http.Context ctx) throws Throwable {
SocialUser user = currentUser();
if ( user != null ) {
ctx.args.put(USER_KEY, user);
SecureSocial.fixHttpContext(ctx);
try {
SocialUser user = currentUser();
if ( user != null ) {
ctx.args.put(USER_KEY, user);
}
return delegate.call(ctx);
} finally {
// leave it null as it was before, just in case.
Http.Context.current.set(null);
}
return delegate.call(ctx);
}
}
}
7 changes: 7 additions & 0 deletions samples/java/demo/app/controllers/Application.java
Expand Up @@ -36,4 +36,11 @@ public static Result index() {
SocialUser user = (SocialUser) ctx().args.get(SecureSocial.USER_KEY);
return ok(index.render(user));
}

@SecureSocial.UserAware
public static Result userAware() {
SocialUser user = (SocialUser) ctx().args.get(SecureSocial.USER_KEY);
final String userName = user != null ? user.displayName : "guest";
return ok("Hello " + userName + ", you are seeing a public page");
}
}
1 change: 1 addition & 0 deletions samples/java/demo/conf/routes
Expand Up @@ -4,6 +4,7 @@

# Home page
GET / controllers.Application.index
GET /userAware controllers.Application.userAware

# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.at(path="/public", file)
Expand Down

0 comments on commit 5a9a4e9

Please sign in to comment.