1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
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  /**
33   * <a href="UserInfoFactory.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   */
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          // Liferay user attributes
89  
90          try {
91              UserAttributes userAttributes = new UserAttributes(user);
92  
93              // Mandatory user attributes
94  
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             // Portlet user attributes
104 
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         // Custom user attributes
121 
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 }