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