37ca54c9bb90b9ff60d179f896cd564f1cd3f1e1
[examples/facebook-app] / src / main / java / de / juplo / yourshouter / SecurityContext.java
1 package de.juplo.yourshouter;
2
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
5
6
7 /**
8  * Simple SecurityContext that stores the currently signed-in connection in a
9  * thread local.
10  *
11  * @author Kai Moritz
12  */
13 public final class SecurityContext
14 {
15   private final static Logger LOG = LoggerFactory.getLogger(SecurityContext.class);
16   private final static ThreadLocal<String> CURRENT_USER = new ThreadLocal<>();
17
18
19   /**
20    * Fetches the ID of the current user from the thread-local.
21    *
22    * @return
23    *     The ID of the current user, or <code>null</code> if no user is known.
24    */
25   public static String getCurrentUser()
26   {
27     String user = CURRENT_USER.get();
28     LOG.debug("current user: {}", user);
29     return user;
30   }
31
32   /**
33    * Stores the given ID as the ID of the current user in the thread-local.
34    *
35    * @param user
36    *     The ID to store as the ID of the current user.
37    */
38   public static void setCurrentUser(String user)
39   {
40     LOG.debug("setting current user: {}", user);
41     CURRENT_USER.set(user);
42   }
43
44   /**
45    * Checks, if a user is signed in. That is, if the ID of a user is stored in
46    * the thread-local.
47    *
48    * @return
49    *   <code>true</code>, if a user is signed in, <code>false</code> otherwise.
50    */
51   public static boolean userSignedIn()
52   {
53     boolean signedIn = CURRENT_USER.get() != null;
54     LOG.debug("user signed in: {}", signedIn);
55     return signedIn;
56   }
57
58   /**
59    * Removes the ID of the current user from the thread-local.
60    */
61   public static void remove()
62   {
63     LOG.debug("removing current user");
64     CURRENT_USER.remove();
65   }
66 }