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