1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.userinfo.impl;
18
19 import java.security.Principal;
20 import java.util.Collection;
21 import java.util.HashMap;
22 import java.util.Iterator;
23 import java.util.Map;
24 import java.util.prefs.Preferences;
25
26 import javax.security.auth.Subject;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.jetspeed.om.common.UserAttributeRef;
31 import org.apache.jetspeed.request.RequestContext;
32 import org.apache.jetspeed.security.SecurityException;
33 import org.apache.jetspeed.security.SecurityHelper;
34 import org.apache.jetspeed.security.User;
35 import org.apache.jetspeed.security.UserManager;
36 import org.apache.jetspeed.security.UserPrincipal;
37 import org.apache.jetspeed.userinfo.UserAttributeRetrievalException;
38 import org.apache.jetspeed.userinfo.UserAttributeSource;
39
40 /***
41 * Default implementation of a UserAttribute source Provides users attributes from standard prefs implementation
42 *
43 * @author <a href="mailto:KeithGarry.Boyce@bcbsma.com">Keith Garry Boyce</a>
44 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
45 * @version $Id: $
46 */
47 public class UserManagerUserAttributeSourceImpl implements UserAttributeSource
48 {
49
50 /*** Logger */
51 private static final Log log = LogFactory.getLog(UserManagerUserAttributeSourceImpl.class);
52
53 /*** The user manager */
54 private UserManager userManager;
55
56 /***
57 * @param userManager
58 * The userManager to set.
59 */
60 public void setUserManager(UserManager userManager)
61 {
62 this.userManager = userManager;
63 }
64
65
66
67
68
69
70 public Map getUserAttributeMap(Subject subject, Collection userAttributeRefs, RequestContext context)
71 throws UserAttributeRetrievalException
72 {
73
74 Map userAttributeMap = new HashMap();
75 Principal userPrincipal = SecurityHelper.getPrincipal(subject, UserPrincipal.class);
76 if (null != userPrincipal)
77 {
78 log.debug("Got user principal: " + userPrincipal.getName());
79 try
80 {
81 if (userManager.userExists(userPrincipal.getName()))
82 {
83 User user = userManager.getUser(userPrincipal.getName());
84 Preferences userInfoPrefs = user.getPreferences();
85 for (Iterator iter = userAttributeRefs.iterator(); iter.hasNext();)
86 {
87 UserAttributeRef currentAttributeRef = (UserAttributeRef) iter.next();
88 Object value = userInfoPrefs.get(currentAttributeRef.getName(), null);
89 if (value != null)
90 {
91 userAttributeMap.put(currentAttributeRef.getName(), value);
92 }
93
94 }
95 }
96 }
97 catch (SecurityException sex)
98 {
99 log.warn("Unexpected SecurityException in UserInfoManager", sex);
100 }
101 }
102
103 return userAttributeMap;
104 }
105
106 }