1/*2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. See the NOTICE file distributed with4 * this work for additional information regarding copyright ownership.5 * The ASF licenses this file to You under the Apache License, Version 2.06 * (the "License"); you may not use this file except in compliance with7 * the License. You may obtain a copy of the License at8 *9 * http://www.apache.org/licenses/LICENSE-2.010 *11 * Unless required by applicable law or agreed to in writing, software12 * distributed under the License is distributed on an "AS IS" BASIS,13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14 * See the License for the specific language governing permissions and15 * limitations under the License.16 */17packageorg.apache.jetspeed.userinfo.impl;
1819import java.security.Principal;
20import java.util.Collection;
21import java.util.HashMap;
22import java.util.Iterator;
23import java.util.Map;
24import java.util.prefs.Preferences;
2526import javax.security.auth.Subject;
2728import org.apache.commons.logging.Log;
29import org.apache.commons.logging.LogFactory;
30import org.apache.jetspeed.om.common.UserAttributeRef;
31import org.apache.jetspeed.request.RequestContext;
32import org.apache.jetspeed.security.SecurityException;
33import org.apache.jetspeed.security.SecurityHelper;
34import org.apache.jetspeed.security.User;
35import org.apache.jetspeed.security.UserManager;
36import org.apache.jetspeed.security.UserPrincipal;
37import org.apache.jetspeed.userinfo.UserAttributeRetrievalException;
38import org.apache.jetspeed.userinfo.UserAttributeSource;
3940/***41 * Default implementation of a UserAttribute source Provides users attributes from standard prefs implementation42 * 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 */47publicclassUserManagerUserAttributeSourceImpl implements UserAttributeSource
48 {
4950/*** Logger */51privatestaticfinal Log log = LogFactory.getLog(UserManagerUserAttributeSourceImpl.class);
5253/*** The user manager */54private UserManager userManager;
5556/***57 * @param userManager58 * The userManager to set.59 */60publicvoid setUserManager(UserManager userManager)
61 {
62this.userManager = userManager;
63 }
6465/*66 * (non-Javadoc)67 * 68 * @see org.jetspeed.userinfo.UserAttributeSource#getUserAttributeMap(javax.security.auth.Subject, java.util.Set)69 */70public Map getUserAttributeMap(Subject subject, Collection userAttributeRefs, RequestContext context)
71 throws UserAttributeRetrievalException
72 {
7374 Map userAttributeMap = new HashMap();
75 Principal userPrincipal = SecurityHelper.getPrincipal(subject, UserPrincipal.class);
76if (null != userPrincipal)
77 {
78 log.debug("Got user principal: " + userPrincipal.getName());
79try80 {
81if (userManager.userExists(userPrincipal.getName()))
82 {
83 User user = userManager.getUser(userPrincipal.getName());
84 Preferences userInfoPrefs = user.getPreferences();
85for (Iterator iter = userAttributeRefs.iterator(); iter.hasNext();)
86 {
87 UserAttributeRef currentAttributeRef = (UserAttributeRef) iter.next();
88 Object value = userInfoPrefs.get(currentAttributeRef.getName(), null);
89if (value != null)
90 {
91 userAttributeMap.put(currentAttributeRef.getName(), value);
92 }
9394 }
95 }
96 }
97catch (SecurityException sex)
98 {
99 log.warn("Unexpected SecurityException in UserInfoManager", sex);
100 }
101 }
102103return userAttributeMap;
104 }
105106 }