1   /**
2    * Copyright (c) 2000-2008 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.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  /**
42   * <a href="UserInfoFactory.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
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          // Liferay user attributes
99  
100         try {
101             UserAttributes userAttributes = new UserAttributes(user);
102 
103             // Mandatory user attributes
104 
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             // Portlet user attributes
114 
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         // Custom user attributes
131 
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 }