001
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.model.Portlet;
020 import com.liferay.portal.kernel.model.PortletApp;
021 import com.liferay.portal.kernel.model.User;
022 import com.liferay.portal.kernel.portlet.CustomUserAttributes;
023 import com.liferay.portal.kernel.portlet.UserAttributes;
024 import com.liferay.portal.kernel.service.UserLocalServiceUtil;
025 import com.liferay.portal.kernel.util.InstanceFactory;
026 import com.liferay.portal.kernel.util.PortalUtil;
027
028 import java.util.Collections;
029 import java.util.HashMap;
030 import java.util.LinkedHashMap;
031 import java.util.Map;
032
033 import javax.servlet.http.HttpServletRequest;
034
035
038 public class UserInfoFactory {
039
040 public static LinkedHashMap<String, String> getUserInfo(
041 HttpServletRequest request, Portlet portlet) {
042
043 if (request.getRemoteUser() == null) {
044 return null;
045 }
046
047 LinkedHashMap<String, String> userInfo = new LinkedHashMap<>();
048
049 try {
050 User user = PortalUtil.getUser(request);
051
052 userInfo = getUserInfo(user, userInfo, portlet);
053 }
054 catch (Exception e) {
055 _log.error(e, e);
056 }
057
058 return userInfo;
059 }
060
061 public static LinkedHashMap<String, String> getUserInfo(
062 long userId, Portlet portlet) {
063
064 if (userId <= 0) {
065 return null;
066 }
067
068 LinkedHashMap<String, String> userInfo = new LinkedHashMap<>();
069
070 try {
071 User user = UserLocalServiceUtil.getUserById(userId);
072
073 userInfo = getUserInfo(user, userInfo, portlet);
074 }
075 catch (Exception e) {
076 _log.error(e, e);
077 }
078
079 return userInfo;
080 }
081
082 public static LinkedHashMap<String, String> getUserInfo(
083 User user, LinkedHashMap<String, String> userInfo, Portlet portlet) {
084
085 PortletApp portletApp = portlet.getPortletApp();
086
087
088
089 try {
090 UserAttributes userAttributes = new UserAttributes(user);
091
092
093
094 userInfo.put(
095 UserAttributes.LIFERAY_COMPANY_ID,
096 userAttributes.getValue(UserAttributes.LIFERAY_COMPANY_ID));
097
098 userInfo.put(
099 UserAttributes.LIFERAY_USER_ID,
100 userAttributes.getValue(UserAttributes.LIFERAY_USER_ID));
101
102 userInfo.put(
103 UserAttributes.USER_NAME_FULL,
104 userAttributes.getValue(UserAttributes.USER_NAME_FULL));
105
106
107
108 for (String userAttributeName : portletApp.getUserAttributes()) {
109 String userAttributeValue = userAttributes.getValue(
110 userAttributeName);
111
112 if (userAttributeValue != null) {
113 userInfo.put(userAttributeName, userAttributeValue);
114 }
115 }
116 }
117 catch (Exception e) {
118 _log.error(e, e);
119 }
120
121 Map<String, String> unmodifiableUserInfo = Collections.unmodifiableMap(
122 (Map<String, String>)userInfo.clone());
123
124
125
126 Map<String, CustomUserAttributes> customUserAttributesMap =
127 new HashMap<>();
128
129 Map<String, String> customUserAttributesClassNames =
130 portletApp.getCustomUserAttributes();
131
132 for (Map.Entry<String, String> entry :
133 customUserAttributesClassNames.entrySet()) {
134
135 String userAttributeName = entry.getKey();
136 String customUserAttributesClassName = entry.getValue();
137
138 CustomUserAttributes customUserAttributes =
139 customUserAttributesMap.get(customUserAttributesClassName);
140
141 if (customUserAttributes == null) {
142 if (portletApp.isWARFile()) {
143 PortletContextBag portletContextBag =
144 PortletContextBagPool.get(
145 portletApp.getServletContextName());
146
147 Map<String, CustomUserAttributes>
148 portletContextBagCustomUserAttributes =
149 portletContextBag.getCustomUserAttributes();
150
151 customUserAttributes =
152 portletContextBagCustomUserAttributes.get(
153 customUserAttributesClassName);
154
155 if (customUserAttributes != null) {
156 customUserAttributes =
157 (CustomUserAttributes)customUserAttributes.clone();
158 }
159 }
160 else {
161 customUserAttributes = _newInstance(
162 customUserAttributesClassName);
163 }
164
165 if (customUserAttributes != null) {
166 customUserAttributesMap.put(
167 customUserAttributesClassName, customUserAttributes);
168 }
169 }
170
171 if (customUserAttributes != null) {
172 String attrValue = customUserAttributes.getValue(
173 userAttributeName, unmodifiableUserInfo);
174
175 if (attrValue != null) {
176 userInfo.put(userAttributeName, attrValue);
177 }
178 }
179 }
180
181 return userInfo;
182 }
183
184 private static CustomUserAttributes _newInstance(String className) {
185 try {
186 return (CustomUserAttributes)InstanceFactory.newInstance(className);
187 }
188 catch (Exception e) {
189 _log.error(e, e);
190 }
191
192 return null;
193 }
194
195 private static final Log _log = LogFactoryUtil.getLog(
196 UserInfoFactory.class);
197
198 }