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