001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.userinfo.impl;
018:
019: import java.security.Principal;
020: import java.util.Collection;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.Map;
024: import java.util.prefs.Preferences;
025:
026: import javax.security.auth.Subject;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.apache.jetspeed.om.common.UserAttributeRef;
031: import org.apache.jetspeed.request.RequestContext;
032: import org.apache.jetspeed.security.SecurityException;
033: import org.apache.jetspeed.security.SecurityHelper;
034: import org.apache.jetspeed.security.User;
035: import org.apache.jetspeed.security.UserManager;
036: import org.apache.jetspeed.security.UserPrincipal;
037: import org.apache.jetspeed.userinfo.UserAttributeRetrievalException;
038: import org.apache.jetspeed.userinfo.UserAttributeSource;
039:
040: /**
041: * Default implementation of a UserAttribute source Provides users attributes from standard prefs implementation
042: *
043: * @author <a href="mailto:KeithGarry.Boyce@bcbsma.com">Keith Garry Boyce</a>
044: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
045: * @version $Id: $
046: */
047: public class UserManagerUserAttributeSourceImpl implements
048: UserAttributeSource {
049:
050: /** Logger */
051: private static final Log log = LogFactory
052: .getLog(UserManagerUserAttributeSourceImpl.class);
053:
054: /** The user manager */
055: private UserManager userManager;
056:
057: /**
058: * @param userManager
059: * The userManager to set.
060: */
061: public void setUserManager(UserManager userManager) {
062: this .userManager = userManager;
063: }
064:
065: /*
066: * (non-Javadoc)
067: *
068: * @see org.jetspeed.userinfo.UserAttributeSource#getUserAttributeMap(javax.security.auth.Subject, java.util.Set)
069: */
070: public Map getUserAttributeMap(Subject subject,
071: Collection userAttributeRefs, RequestContext context)
072: throws UserAttributeRetrievalException {
073:
074: Map userAttributeMap = new HashMap();
075: Principal userPrincipal = SecurityHelper.getPrincipal(subject,
076: UserPrincipal.class);
077: if (null != userPrincipal) {
078: log.debug("Got user principal: " + userPrincipal.getName());
079: try {
080: if (userManager.userExists(userPrincipal.getName())) {
081: User user = userManager.getUser(userPrincipal
082: .getName());
083: Preferences userInfoPrefs = user.getPreferences();
084: for (Iterator iter = userAttributeRefs.iterator(); iter
085: .hasNext();) {
086: UserAttributeRef currentAttributeRef = (UserAttributeRef) iter
087: .next();
088: Object value = userInfoPrefs.get(
089: currentAttributeRef.getName(), null);
090: if (value != null) {
091: userAttributeMap.put(currentAttributeRef
092: .getName(), value);
093: }
094:
095: }
096: }
097: } catch (SecurityException sex) {
098: log
099: .warn(
100: "Unexpected SecurityException in UserInfoManager",
101: sex);
102: }
103: }
104:
105: return userAttributeMap;
106: }
107:
108: }
|