1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class UserInfoFactory {
46  
47      public static LinkedHashMap<String, String> getUserInfo(
48          long userId, Portlet portlet) {
49  
50          if (userId <= 0) {
51              return null;
52          }
53  
54          LinkedHashMap<String, String> userInfo =
55              new LinkedHashMap<String, String>();
56  
57          try {
58              User user = UserLocalServiceUtil.getUserById(userId);
59  
60              userInfo = getUserInfo(user, userInfo, portlet);
61          }
62          catch (Exception e) {
63              _log.error(e, e);
64          }
65  
66          return userInfo;
67      }
68  
69      public static LinkedHashMap<String, String> getUserInfo(
70          HttpServletRequest request, Portlet portlet) {
71  
72          if (request.getRemoteUser() == null) {
73              return null;
74          }
75  
76          LinkedHashMap<String, String> userInfo =
77              new LinkedHashMap<String, String>();
78  
79          try {
80              User user = PortalUtil.getUser(request);
81  
82              userInfo = getUserInfo(user, userInfo, portlet);
83          }
84          catch (Exception e) {
85              _log.error(e, e);
86          }
87  
88          return userInfo;
89      }
90  
91      public static LinkedHashMap<String, String> getUserInfo(
92          User user, LinkedHashMap<String, String> userInfo, Portlet portlet) {
93  
94          PortletApp portletApp = portlet.getPortletApp();
95  
96          // Liferay user attributes
97  
98          try {
99              UserAttributes userAttributes = new UserAttributes(user);
100 
101             // Mandatory user attributes
102 
103             userInfo.put(
104                 UserAttributes.LIFERAY_COMPANY_ID,
105                 userAttributes.getValue(UserAttributes.LIFERAY_COMPANY_ID));
106 
107             userInfo.put(
108                 UserAttributes.LIFERAY_USER_ID,
109                 userAttributes.getValue(UserAttributes.LIFERAY_USER_ID));
110 
111             // Portlet user attributes
112 
113             for (String attrName : portletApp.getUserAttributes()) {
114                 String attrValue = userAttributes.getValue(attrName);
115 
116                 if (attrValue != null) {
117                     userInfo.put(attrName, attrValue);
118                 }
119             }
120         }
121         catch (Exception e) {
122             _log.error(e, e);
123         }
124 
125         Map<String, String> unmodifiableUserInfo =
126             Collections.unmodifiableMap((Map<String, String>)userInfo.clone());
127 
128         // Custom user attributes
129 
130         Map<String, CustomUserAttributes> cuaInstances =
131             new HashMap<String, CustomUserAttributes>();
132 
133         for (Map.Entry<String, String> entry :
134                 portletApp.getCustomUserAttributes().entrySet()) {
135 
136             String attrName = entry.getKey();
137             String attrCustomClass = entry.getValue();
138 
139             CustomUserAttributes cua = cuaInstances.get(attrCustomClass);
140 
141             if (cua == null) {
142                 if (portletApp.isWARFile()) {
143                     PortletContextBag portletContextBag =
144                         PortletContextBagPool.get(
145                             portletApp.getServletContextName());
146 
147                     cua = portletContextBag.getCustomUserAttributes().get(
148                         attrCustomClass);
149 
150                     cua = (CustomUserAttributes)cua.clone();
151                 }
152                 else {
153                     try {
154                         cua = (CustomUserAttributes)Class.forName(
155                             attrCustomClass).newInstance();
156                     }
157                     catch (Exception e) {
158                         _log.error(e, e);
159                     }
160                 }
161 
162                 cuaInstances.put(attrCustomClass, cua);
163             }
164 
165             if (cua != null) {
166                 String attrValue = cua.getValue(attrName, unmodifiableUserInfo);
167 
168                 if (attrValue != null) {
169                     userInfo.put(attrName, attrValue);
170                 }
171             }
172         }
173 
174         return userInfo;
175     }
176 
177     private static Log _log = LogFactoryUtil.getLog(UserInfoFactory.class);
178 
179 }