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