001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.InstanceFactory;
020    import com.liferay.portal.model.Portlet;
021    import com.liferay.portal.model.PortletApp;
022    import com.liferay.portal.model.User;
023    import com.liferay.portal.service.UserLocalServiceUtil;
024    import com.liferay.portal.util.PortalUtil;
025    
026    import java.util.Collections;
027    import java.util.HashMap;
028    import java.util.LinkedHashMap;
029    import java.util.Map;
030    
031    import javax.servlet.http.HttpServletRequest;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class UserInfoFactory {
037    
038            public static LinkedHashMap<String, String> getUserInfo(
039                    HttpServletRequest request, Portlet portlet) {
040    
041                    if (request.getRemoteUser() == null) {
042                            return null;
043                    }
044    
045                    LinkedHashMap<String, String> userInfo = new LinkedHashMap<>();
046    
047                    try {
048                            User user = PortalUtil.getUser(request);
049    
050                            userInfo = getUserInfo(user, userInfo, portlet);
051                    }
052                    catch (Exception e) {
053                            _log.error(e, e);
054                    }
055    
056                    return userInfo;
057            }
058    
059            public static LinkedHashMap<String, String> getUserInfo(
060                    long userId, Portlet portlet) {
061    
062                    if (userId <= 0) {
063                            return null;
064                    }
065    
066                    LinkedHashMap<String, String> userInfo = new LinkedHashMap<>();
067    
068                    try {
069                            User user = UserLocalServiceUtil.getUserById(userId);
070    
071                            userInfo = getUserInfo(user, userInfo, portlet);
072                    }
073                    catch (Exception e) {
074                            _log.error(e, e);
075                    }
076    
077                    return userInfo;
078            }
079    
080            public static LinkedHashMap<String, String> getUserInfo(
081                    User user, LinkedHashMap<String, String> userInfo, Portlet portlet) {
082    
083                    PortletApp portletApp = portlet.getPortletApp();
084    
085                    // Liferay user attributes
086    
087                    try {
088                            UserAttributes userAttributes = new UserAttributes(user);
089    
090                            // Mandatory user attributes
091    
092                            userInfo.put(
093                                    UserAttributes.LIFERAY_COMPANY_ID,
094                                    userAttributes.getValue(UserAttributes.LIFERAY_COMPANY_ID));
095    
096                            userInfo.put(
097                                    UserAttributes.LIFERAY_USER_ID,
098                                    userAttributes.getValue(UserAttributes.LIFERAY_USER_ID));
099    
100                            // Portlet user attributes
101    
102                            for (String attrName : portletApp.getUserAttributes()) {
103                                    String attrValue = userAttributes.getValue(attrName);
104    
105                                    if (attrValue != null) {
106                                            userInfo.put(attrName, attrValue);
107                                    }
108                            }
109                    }
110                    catch (Exception e) {
111                            _log.error(e, e);
112                    }
113    
114                    Map<String, String> unmodifiableUserInfo = Collections.unmodifiableMap(
115                            (Map<String, String>)userInfo.clone());
116    
117                    // Custom user attributes
118    
119                    Map<String, CustomUserAttributes> cuaInstances = new HashMap<>();
120    
121                    for (Map.Entry<String, String> entry :
122                                    portletApp.getCustomUserAttributes().entrySet()) {
123    
124                            String attrName = entry.getKey();
125                            String attrCustomClass = entry.getValue();
126    
127                            CustomUserAttributes cua = cuaInstances.get(attrCustomClass);
128    
129                            if (cua == null) {
130                                    if (portletApp.isWARFile()) {
131                                            PortletContextBag portletContextBag =
132                                                    PortletContextBagPool.get(
133                                                            portletApp.getServletContextName());
134    
135                                            Map<String, CustomUserAttributes> customUserAttributes =
136                                                    portletContextBag.getCustomUserAttributes();
137    
138                                            cua = customUserAttributes.get(attrCustomClass);
139    
140                                            cua = (CustomUserAttributes)cua.clone();
141                                    }
142                                    else {
143                                            try {
144                                                    cua = (CustomUserAttributes)InstanceFactory.newInstance(
145                                                            attrCustomClass);
146                                            }
147                                            catch (Exception e) {
148                                                    _log.error(e, e);
149                                            }
150                                    }
151    
152                                    cuaInstances.put(attrCustomClass, cua);
153                            }
154    
155                            if (cua != null) {
156                                    String attrValue = cua.getValue(attrName, unmodifiableUserInfo);
157    
158                                    if (attrValue != null) {
159                                            userInfo.put(attrName, attrValue);
160                                    }
161                            }
162                    }
163    
164                    return userInfo;
165            }
166    
167            private static final Log _log = LogFactoryUtil.getLog(
168                    UserInfoFactory.class);
169    
170    }