Permalink
Browse files

Fix for #71: Context not available in UserAware action

  • Loading branch information...
1 parent 2b9d949 commit 5a9a4e98d0e090a80991310ecaa7de29f57b2bde @jaliss committed Sep 25, 2012
@@ -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
*/
@@ -135,10 +144,7 @@ private static ObjectNode forbiddenJson() {
@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 ) {
@@ -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);
}
}
@@ -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);
}
}
}
@@ -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");
+ }
}
@@ -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)

0 comments on commit 5a9a4e9

Please sign in to comment.