1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.model.Portlet;
25  import com.liferay.portal.model.PortletApp;
26  import com.liferay.portal.model.User;
27  import com.liferay.portal.service.UserLocalServiceUtil;
28  import com.liferay.portal.util.PortalUtil;
29  
30  import java.util.Collections;
31  import java.util.HashMap;
32  import java.util.LinkedHashMap;
33  import java.util.Map;
34  
35  import javax.servlet.http.HttpServletRequest;
36  
37  /**
38   * <a href="UserInfoFactory.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class UserInfoFactory {
44  
45      public static LinkedHashMap<String, String> getUserInfo(
46          long userId, Portlet portlet) {
47  
48          if (userId <= 0) {
49              return null;
50          }
51  
52          LinkedHashMap<String, String> userInfo =
53              new LinkedHashMap<String, String>();
54  
55          try {
56              User user = UserLocalServiceUtil.getUserById(userId);
57  
58              userInfo = getUserInfo(user, userInfo, portlet);
59          }
60          catch (Exception e) {
61              _log.error(e, e);
62          }
63  
64          return userInfo;
65      }
66  
67      public static LinkedHashMap<String, String> getUserInfo(
68          HttpServletRequest request, Portlet portlet) {
69  
70          if (request.getRemoteUser() == null) {
71              return null;
72          }
73  
74          LinkedHashMap<String, String> userInfo =
75              new LinkedHashMap<String, String>();
76  
77          try {
78              User user = PortalUtil.getUser(request);
79  
80              userInfo = getUserInfo(user, userInfo, portlet);
81          }
82          catch (Exception e) {
83              _log.error(e, e);
84          }
85  
86          return userInfo;
87      }
88  
89      public static LinkedHashMap<String, String> getUserInfo(
90          User user, LinkedHashMap<String, String> userInfo, Portlet portlet) {
91  
92          PortletApp portletApp = portlet.getPortletApp();
93  
94          // Liferay user attributes
95  
96          try {
97              UserAttributes userAttributes = new UserAttributes(user);
98  
99              // Mandatory user attributes
100 
101             userInfo.put(
102                 UserAttributes.LIFERAY_COMPANY_ID,
103                 userAttributes.getValue(UserAttributes.LIFERAY_COMPANY_ID));
104 
105             userInfo.put(
106                 UserAttributes.LIFERAY_USER_ID,
107                 userAttributes.getValue(UserAttributes.LIFERAY_USER_ID));
108 
109             // Portlet user attributes
110 
111             for (String attrName : portletApp.getUserAttributes()) {
112                 String attrValue = userAttributes.getValue(attrName);
113 
114                 if (attrValue != null) {
115                     userInfo.put(attrName, attrValue);
116                 }
117             }
118         }
119         catch (Exception e) {
120             _log.error(e, e);
121         }
122 
123         Map<String, String> unmodifiableUserInfo =
124             Collections.unmodifiableMap((Map<String, String>)userInfo.clone());
125 
126         // Custom user attributes
127 
128         Map<String, CustomUserAttributes> cuaInstances =
129             new HashMap<String, CustomUserAttributes>();
130 
131         for (Map.Entry<String, String> entry :
132                 portletApp.getCustomUserAttributes().entrySet()) {
133 
134             String attrName = entry.getKey();
135             String attrCustomClass = entry.getValue();
136 
137             CustomUserAttributes cua = cuaInstances.get(attrCustomClass);
138 
139             if (cua == null) {
140                 if (portletApp.isWARFile()) {
141                     PortletContextBag portletContextBag =
142                         PortletContextBagPool.get(
143                             portletApp.getServletContextName());
144 
145                     cua = portletContextBag.getCustomUserAttributes().get(
146                         attrCustomClass);
147 
148                     cua = (CustomUserAttributes)cua.clone();
149                 }
150                 else {
151                     try {
152                         cua = (CustomUserAttributes)Class.forName(
153                             attrCustomClass).newInstance();
154                     }
155                     catch (Exception e) {
156                         _log.error(e, e);
157                     }
158                 }
159 
160                 cuaInstances.put(attrCustomClass, cua);
161             }
162 
163             if (cua != null) {
164                 String attrValue = cua.getValue(attrName, unmodifiableUserInfo);
165 
166                 if (attrValue != null) {
167                     userInfo.put(attrName, attrValue);
168                 }
169             }
170         }
171 
172         return userInfo;
173     }
174 
175     private static Log _log = LogFactoryUtil.getLog(UserInfoFactory.class);
176 
177 }