16
16
*/
17
17
package securesocial .core .java ;
18
18
19
+ import play .api .mvc .RequestHeader ;
20
+ import play .libs .F ;
21
+ import play .libs .HttpExecution ;
19
22
import play .mvc .Http ;
23
+ import scala .Option ;
24
+ import scala .concurrent .ExecutionContext ;
20
25
import securesocial .core .RuntimeEnvironment ;
26
+ import securesocial .core .SecureSocial$ ;
21
27
22
28
/**
23
29
*
@@ -39,4 +45,42 @@ public class SecureSocial {
39
45
public static RuntimeEnvironment env () {
40
46
return (RuntimeEnvironment ) Http .Context .current ().args .get ("securesocial-env" );
41
47
}
48
+
49
+ /**
50
+ * Returns the current user. Invoke this only if you are executing code
51
+ * without a SecuredAction or UserAware action available (eg: using WebSockets). For other cases this
52
+ * should not be needed.
53
+ *
54
+ * @param env the environment
55
+ * @return the current user object or null if there isn't one available
56
+ */
57
+ public static F .Promise <Object > currentUser (RuntimeEnvironment env ) {
58
+ return currentUser (env , HttpExecution .defaultContext ());
59
+ }
60
+
61
+ /**
62
+ * Returns the current user. Invoke this only if you are executing code
63
+ * without a SecuredAction or UserAware action available (eg: using WebSockets). For other cases this
64
+ * should not be needed.
65
+ *
66
+ * @param env the environment
67
+ * @param executor an ExecutionContext
68
+ * @return the current user object or null if there isn't one available
69
+ */
70
+ public static F .Promise <Object > currentUser (RuntimeEnvironment env , ExecutionContext executor ) {
71
+ RequestHeader requestHeader = Http .Context .current ()._requestHeader ();
72
+ if (requestHeader == null || env == null ) {
73
+ return F .Promise .promise (null );
74
+ } else {
75
+ scala .concurrent .Future scalaFuture = SecureSocial$ .MODULE$ .currentUser (requestHeader , env , executor );
76
+ F .Function <Option <Object >, Object > mapFunction = new F .Function <Option <Object >, Object >() {
77
+
78
+ @ Override
79
+ public Object apply (Option <Object > objectOption ) throws Throwable {
80
+ return objectOption .isDefined () ? objectOption .get () : null ;
81
+ }
82
+ };
83
+ return F .Promise .wrap (scalaFuture ).map (mapFunction );
84
+ }
85
+ }
42
86
}
0 commit comments