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