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.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
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 =
046 new LinkedHashMap<String, String>();
047
048 try {
049 User user = PortalUtil.getUser(request);
050
051 userInfo = getUserInfo(user, userInfo, portlet);
052 }
053 catch (Exception e) {
054 _log.error(e, e);
055 }
056
057 return userInfo;
058 }
059
060 public static LinkedHashMap<String, String> getUserInfo(
061 long userId, Portlet portlet) {
062
063 if (userId <= 0) {
064 return null;
065 }
066
067 LinkedHashMap<String, String> userInfo =
068 new LinkedHashMap<String, String>();
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 attrName : portletApp.getUserAttributes()) {
105 String attrValue = userAttributes.getValue(attrName);
106
107 if (attrValue != null) {
108 userInfo.put(attrName, attrValue);
109 }
110 }
111 }
112 catch (Exception e) {
113 _log.error(e, e);
114 }
115
116 Map<String, String> unmodifiableUserInfo = Collections.unmodifiableMap(
117 (Map<String, String>)userInfo.clone());
118
119
120
121 Map<String, CustomUserAttributes> cuaInstances =
122 new HashMap<String, CustomUserAttributes>();
123
124 for (Map.Entry<String, String> entry :
125 portletApp.getCustomUserAttributes().entrySet()) {
126
127 String attrName = entry.getKey();
128 String attrCustomClass = entry.getValue();
129
130 CustomUserAttributes cua = cuaInstances.get(attrCustomClass);
131
132 if (cua == null) {
133 if (portletApp.isWARFile()) {
134 PortletContextBag portletContextBag =
135 PortletContextBagPool.get(
136 portletApp.getServletContextName());
137
138 Map<String, CustomUserAttributes> customUserAttributes =
139 portletContextBag.getCustomUserAttributes();
140
141 cua = customUserAttributes.get(attrCustomClass);
142
143 cua = (CustomUserAttributes)cua.clone();
144 }
145 else {
146 try {
147 cua = (CustomUserAttributes)InstanceFactory.newInstance(
148 attrCustomClass);
149 }
150 catch (Exception e) {
151 _log.error(e, e);
152 }
153 }
154
155 cuaInstances.put(attrCustomClass, cua);
156 }
157
158 if (cua != null) {
159 String attrValue = cua.getValue(attrName, unmodifiableUserInfo);
160
161 if (attrValue != null) {
162 userInfo.put(attrName, attrValue);
163 }
164 }
165 }
166
167 return userInfo;
168 }
169
170 private static Log _log = LogFactoryUtil.getLog(UserInfoFactory.class);
171
172 }