Implemented a simple UserIdSource, that stores the user in a cookie
[examples/facebook-app] / src / main / java / de / juplo / yourshouter / SecurityContextUserIdSource.java
diff --git a/src/main/java/de/juplo/yourshouter/SecurityContextUserIdSource.java b/src/main/java/de/juplo/yourshouter/SecurityContextUserIdSource.java
new file mode 100644 (file)
index 0000000..9fae323
--- /dev/null
@@ -0,0 +1,42 @@
+package de.juplo.yourshouter;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.social.UserIdSource;
+
+
+/**
+ * Implementation of {@link UserIdSource}, that retrieves the ID of the current
+ * user from the {@link SecurityContext}.
+ *
+ * @author Kai Moritz
+ */
+public class SecurityContextUserIdSource implements UserIdSource
+{
+  private final static Logger LOG =
+      LoggerFactory.getLogger(SecurityContextUserIdSource.class);
+
+
+  /**
+   * Retrieves the ID of the current user from the {@link SecurityContext}.
+   *
+   * @return
+   *     The ID of the current user, or the special ID <code>anonymous</code>,
+   *     if no current user is present.
+   */
+  @Override
+  public String getUserId()
+  {
+    String user = SecurityContext.getCurrentUser();
+    if (user != null)
+    {
+      LOG.debug("found user \"{}\" in the security-context", user);
+    }
+    else
+    {
+      LOG.info("found no user in the security-context, using \"anonymous\"");
+      user = "anonymous";
+    }
+    return user;
+  }
+}