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