1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
41   * <a href="UserInfoFactory.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class UserInfoFactory {
47  
48      public static LinkedHashMap<String, String> getUserInfo(
49          long userId, Portlet portlet) {
50  
51          if (userId <= 0) {
52              return null;
53          }
54  
55          LinkedHashMap<String, String> userInfo =
56              new LinkedHashMap<String, String>();
57  
58          try {
59              User user = UserLocalServiceUtil.getUserById(userId);
60  
61              userInfo = getUserInfo(user, userInfo, portlet);
62          }
63          catch (Exception e) {
64              _log.error(e, e);
65          }
66  
67          return userInfo;
68      }
69  
70      public static LinkedHashMap<String, String> getUserInfo(
71          HttpServletRequest request, Portlet portlet) {
72  
73          if (request.getRemoteUser() == null) {
74              return null;
75          }
76  
77          LinkedHashMap<String, String> userInfo =
78              new LinkedHashMap<String, String>();
79  
80          try {
81              User user = PortalUtil.getUser(request);
82  
83              userInfo = getUserInfo(user, userInfo, portlet);
84          }
85          catch (Exception e) {
86              _log.error(e, e);
87          }
88  
89          return userInfo;
90      }
91  
92      public static LinkedHashMap<String, String> getUserInfo(
93          User user, LinkedHashMap<String, String> userInfo, Portlet portlet) {
94  
95          PortletApp portletApp = portlet.getPortletApp();
96  
97          // Liferay user attributes
98  
99          try {
100             UserAttributes userAttributes = new UserAttributes(user);
101 
102             // Mandatory user attributes
103 
104             userInfo.put(
105                 UserAttributes.LIFERAY_COMPANY_ID,
106                 userAttributes.getValue(UserAttributes.LIFERAY_COMPANY_ID));
107 
108             userInfo.put(
109                 UserAttributes.LIFERAY_USER_ID,
110                 userAttributes.getValue(UserAttributes.LIFERAY_USER_ID));
111 
112             // Portlet user attributes
113 
114             for (String attrName : portletApp.getUserAttributes()) {
115                 String attrValue = userAttributes.getValue(attrName);
116 
117                 if (attrValue != null) {
118                     userInfo.put(attrName, attrValue);
119                 }
120             }
121         }
122         catch (Exception e) {
123             _log.error(e, e);
124         }
125 
126         Map<String, String> unmodifiableUserInfo =
127             Collections.unmodifiableMap((Map<String, String>)userInfo.clone());
128 
129         // Custom user attributes
130 
131         Map<String, CustomUserAttributes> cuaInstances =
132             new HashMap<String, CustomUserAttributes>();
133 
134         for (Map.Entry<String, String> entry :
135                 portletApp.getCustomUserAttributes().entrySet()) {
136 
137             String attrName = entry.getKey();
138             String attrCustomClass = entry.getValue();
139 
140             CustomUserAttributes cua = cuaInstances.get(attrCustomClass);
141 
142             if (cua == null) {
143                 if (portletApp.isWARFile()) {
144                     PortletContextBag portletContextBag =
145                         PortletContextBagPool.get(
146                             portletApp.getServletContextName());
147 
148                     cua = portletContextBag.getCustomUserAttributes().get(
149                         attrCustomClass);
150 
151                     cua = (CustomUserAttributes)cua.clone();
152                 }
153                 else {
154                     try {
155                         cua = (CustomUserAttributes)Class.forName(
156                             attrCustomClass).newInstance();
157                     }
158                     catch (Exception e) {
159                         _log.error(e, e);
160                     }
161                 }
162 
163                 cuaInstances.put(attrCustomClass, cua);
164             }
165 
166             if (cua != null) {
167                 String attrValue = cua.getValue(attrName, unmodifiableUserInfo);
168 
169                 if (attrValue != null) {
170                     userInfo.put(attrName, attrValue);
171                 }
172             }
173         }
174 
175         return userInfo;
176     }
177 
178     private static Log _log = LogFactoryUtil.getLog(UserInfoFactory.class);
179 
180 }