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 userAttributeName : portletApp.getUserAttributes()) {
103                                    String userAttributeValue = userAttributes.getValue(
104                                            userAttributeName);
105    
106                                    if (userAttributeValue != null) {
107                                            userInfo.put(userAttributeName, userAttributeValue);
108                                    }
109                            }
110                    }
111                    catch (Exception e) {
112                            _log.error(e, e);
113                    }
114    
115                    Map<String, String> unmodifiableUserInfo = Collections.unmodifiableMap(
116                            (Map<String, String>)userInfo.clone());
117    
118                    // Custom user attributes
119    
120                    Map<String, CustomUserAttributes> customUserAttributesMap =
121                            new HashMap<>();
122    
123                    Map<String, String> customUserAttributesClassNames =
124                            portletApp.getCustomUserAttributes();
125    
126                    for (Map.Entry<String, String> entry :
127                                    customUserAttributesClassNames.entrySet()) {
128    
129                            String userAttributeName = entry.getKey();
130                            String customUserAttributesClassName = entry.getValue();
131    
132                            CustomUserAttributes customUserAttributes =
133                                    customUserAttributesMap.get(customUserAttributesClassName);
134    
135                            if (customUserAttributes == null) {
136                                    if (portletApp.isWARFile()) {
137                                            PortletContextBag portletContextBag =
138                                                    PortletContextBagPool.get(
139                                                            portletApp.getServletContextName());
140    
141                                            Map<String, CustomUserAttributes>
142                                                    portletContextBagCustomUserAttributes =
143                                                            portletContextBag.getCustomUserAttributes();
144    
145                                            customUserAttributes =
146                                                    portletContextBagCustomUserAttributes.get(
147                                                            customUserAttributesClassName);
148    
149                                            if (customUserAttributes != null) {
150                                                    customUserAttributes =
151                                                            (CustomUserAttributes)customUserAttributes.clone();
152                                            }
153                                    }
154                                    else {
155                                            customUserAttributes = newInstance(
156                                                    customUserAttributesClassName);
157                                    }
158    
159                                    if (customUserAttributes != null) {
160                                            customUserAttributesMap.put(
161                                                    customUserAttributesClassName, customUserAttributes);
162                                    }
163                            }
164    
165                            if (customUserAttributes != null) {
166                                    String attrValue = customUserAttributes.getValue(
167                                            userAttributeName, unmodifiableUserInfo);
168    
169                                    if (attrValue != null) {
170                                            userInfo.put(userAttributeName, attrValue);
171                                    }
172                            }
173                    }
174    
175                    return userInfo;
176            }
177    
178            private static CustomUserAttributes newInstance(String className) {
179                    try {
180                            return (CustomUserAttributes)InstanceFactory.newInstance(className);
181                    }
182                    catch (Exception e) {
183                            _log.error(e, e);
184                    }
185    
186                    return null;
187            }
188    
189            private static final Log _log = LogFactoryUtil.getLog(
190                    UserInfoFactory.class);
191    
192    }