9fae3235dd5a95dd55e50979bf6a16fd3093b9d5
[examples/facebook-app] / src / main / java / de / juplo / yourshouter / SecurityContextUserIdSource.java
1 package de.juplo.yourshouter;
2
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
5 import org.springframework.social.UserIdSource;
6
7
8 /**
9  * Implementation of {@link UserIdSource}, that retrieves the ID of the current
10  * user from the {@link SecurityContext}.
11  *
12  * @author Kai Moritz
13  */
14 public class SecurityContextUserIdSource implements UserIdSource
15 {
16   private final static Logger LOG =
17       LoggerFactory.getLogger(SecurityContextUserIdSource.class);
18
19
20   /**
21    * Retrieves the ID of the current user from the {@link SecurityContext}.
22    *
23    * @return
24    *     The ID of the current user, or the special ID <code>anonymous</code>,
25    *     if no current user is present.
26    */
27   @Override
28   public String getUserId()
29   {
30     String user = SecurityContext.getCurrentUser();
31     if (user != null)
32     {
33       LOG.debug("found user \"{}\" in the security-context", user);
34     }
35     else
36     {
37       LOG.info("found no user in the security-context, using \"anonymous\"");
38       user = "anonymous";
39     }
40     return user;
41   }
42 }