X-Git-Url: https://juplo.de/gitweb/?p=examples%2Ffacebook-app;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fyourshouter%2FSecurityContext.java;fp=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fyourshouter%2FSecurityContext.java;h=37ca54c9bb90b9ff60d179f896cd564f1cd3f1e1;hp=0000000000000000000000000000000000000000;hb=02f599692669d48f9865764fda994ad61d203ffb;hpb=931f5c9b9a43acb81775e995a613bd0c5b8aab52 diff --git a/src/main/java/de/juplo/yourshouter/SecurityContext.java b/src/main/java/de/juplo/yourshouter/SecurityContext.java new file mode 100644 index 0000000..37ca54c --- /dev/null +++ b/src/main/java/de/juplo/yourshouter/SecurityContext.java @@ -0,0 +1,66 @@ +package de.juplo.yourshouter; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * Simple SecurityContext that stores the currently signed-in connection in a + * thread local. + * + * @author Kai Moritz + */ +public final class SecurityContext +{ + private final static Logger LOG = LoggerFactory.getLogger(SecurityContext.class); + private final static ThreadLocal CURRENT_USER = new ThreadLocal<>(); + + + /** + * Fetches the ID of the current user from the thread-local. + * + * @return + * The ID of the current user, or null if no user is known. + */ + public static String getCurrentUser() + { + String user = CURRENT_USER.get(); + LOG.debug("current user: {}", user); + return user; + } + + /** + * Stores the given ID as the ID of the current user in the thread-local. + * + * @param user + * The ID to store as the ID of the current user. + */ + public static void setCurrentUser(String user) + { + LOG.debug("setting current user: {}", user); + CURRENT_USER.set(user); + } + + /** + * Checks, if a user is signed in. That is, if the ID of a user is stored in + * the thread-local. + * + * @return + * true, if a user is signed in, false otherwise. + */ + public static boolean userSignedIn() + { + boolean signedIn = CURRENT_USER.get() != null; + LOG.debug("user signed in: {}", signedIn); + return signedIn; + } + + /** + * Removes the ID of the current user from the thread-local. + */ + public static void remove() + { + LOG.debug("removing current user"); + CURRENT_USER.remove(); + } +}