1
19
20 package com.liferay.portlet;
21
22 import com.liferay.portal.kernel.log.Log;
23 import com.liferay.portal.kernel.log.LogFactoryUtil;
24 import com.liferay.portal.model.Portlet;
25 import com.liferay.portal.model.PortletApp;
26 import com.liferay.portal.model.User;
27 import com.liferay.portal.service.UserLocalServiceUtil;
28 import com.liferay.portal.util.PortalUtil;
29
30 import java.util.Collections;
31 import java.util.HashMap;
32 import java.util.LinkedHashMap;
33 import java.util.Map;
34
35 import javax.servlet.http.HttpServletRequest;
36
37
43 public class UserInfoFactory {
44
45 public static LinkedHashMap<String, String> getUserInfo(
46 long userId, Portlet portlet) {
47
48 if (userId <= 0) {
49 return null;
50 }
51
52 LinkedHashMap<String, String> userInfo =
53 new LinkedHashMap<String, String>();
54
55 try {
56 User user = UserLocalServiceUtil.getUserById(userId);
57
58 userInfo = getUserInfo(user, userInfo, portlet);
59 }
60 catch (Exception e) {
61 _log.error(e, e);
62 }
63
64 return userInfo;
65 }
66
67 public static LinkedHashMap<String, String> getUserInfo(
68 HttpServletRequest request, Portlet portlet) {
69
70 if (request.getRemoteUser() == null) {
71 return null;
72 }
73
74 LinkedHashMap<String, String> userInfo =
75 new LinkedHashMap<String, String>();
76
77 try {
78 User user = PortalUtil.getUser(request);
79
80 userInfo = getUserInfo(user, userInfo, portlet);
81 }
82 catch (Exception e) {
83 _log.error(e, e);
84 }
85
86 return userInfo;
87 }
88
89 public static LinkedHashMap<String, String> getUserInfo(
90 User user, LinkedHashMap<String, String> userInfo, Portlet portlet) {
91
92 PortletApp portletApp = portlet.getPortletApp();
93
94
96 try {
97 UserAttributes userAttributes = new UserAttributes(user);
98
99
101 userInfo.put(
102 UserAttributes.LIFERAY_COMPANY_ID,
103 userAttributes.getValue(UserAttributes.LIFERAY_COMPANY_ID));
104
105 userInfo.put(
106 UserAttributes.LIFERAY_USER_ID,
107 userAttributes.getValue(UserAttributes.LIFERAY_USER_ID));
108
109
111 for (String attrName : portletApp.getUserAttributes()) {
112 String attrValue = userAttributes.getValue(attrName);
113
114 if (attrValue != null) {
115 userInfo.put(attrName, attrValue);
116 }
117 }
118 }
119 catch (Exception e) {
120 _log.error(e, e);
121 }
122
123 Map<String, String> unmodifiableUserInfo =
124 Collections.unmodifiableMap((Map<String, String>)userInfo.clone());
125
126
128 Map<String, CustomUserAttributes> cuaInstances =
129 new HashMap<String, CustomUserAttributes>();
130
131 for (Map.Entry<String, String> entry :
132 portletApp.getCustomUserAttributes().entrySet()) {
133
134 String attrName = entry.getKey();
135 String attrCustomClass = entry.getValue();
136
137 CustomUserAttributes cua = cuaInstances.get(attrCustomClass);
138
139 if (cua == null) {
140 if (portletApp.isWARFile()) {
141 PortletContextBag portletContextBag =
142 PortletContextBagPool.get(
143 portletApp.getServletContextName());
144
145 cua = portletContextBag.getCustomUserAttributes().get(
146 attrCustomClass);
147
148 cua = (CustomUserAttributes)cua.clone();
149 }
150 else {
151 try {
152 cua = (CustomUserAttributes)Class.forName(
153 attrCustomClass).newInstance();
154 }
155 catch (Exception e) {
156 _log.error(e, e);
157 }
158 }
159
160 cuaInstances.put(attrCustomClass, cua);
161 }
162
163 if (cua != null) {
164 String attrValue = cua.getValue(attrName, unmodifiableUserInfo);
165
166 if (attrValue != null) {
167 userInfo.put(attrName, attrValue);
168 }
169 }
170 }
171
172 return userInfo;
173 }
174
175 private static Log _log = LogFactoryUtil.getLog(UserInfoFactory.class);
176
177 }