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.iframe.util;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portal.model.Layout;
24  import com.liferay.portal.model.Role;
25  import com.liferay.portal.model.User;
26  import com.liferay.portal.service.RoleLocalServiceUtil;
27  import com.liferay.portal.service.UserLocalServiceUtil;
28  import com.liferay.portal.theme.ThemeDisplay;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portal.util.PropsValues;
31  import com.liferay.portal.util.WebKeys;
32  
33  import javax.portlet.PortletRequest;
34  
35  /**
36   * <a href="IFrameUtil.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Amos Fong
39   */
40  public class IFrameUtil {
41  
42      public static String getPassword(
43              PortletRequest portletRequest, String password)
44          throws PortalException, SystemException {
45  
46          if (!isPasswordTokenEnabled(portletRequest)) {
47              return StringPool.BLANK;
48          }
49  
50          if (Validator.isNull(password) || password.equals("@password@")) {
51              password = PortalUtil.getUserPassword(portletRequest);
52          }
53  
54          return password;
55      }
56  
57      public static String getUserName(
58              PortletRequest portletRequest, String userName)
59          throws PortalException, SystemException {
60  
61          User user = PortalUtil.getUser(portletRequest);
62  
63          if (user == null) {
64              return userName;
65          }
66  
67          if (Validator.isNull(userName) || userName.equals("@user_id@")) {
68              userName = portletRequest.getRemoteUser();
69          }
70          else if (userName.equals("@email_address@")) {
71              userName = user.getEmailAddress();
72          }
73          else if (userName.equals("@screen_name@")) {
74              userName = user.getScreenName();
75          }
76  
77          return userName;
78      }
79  
80      public static boolean isPasswordTokenEnabled(
81              PortletRequest portletRequest)
82          throws PortalException, SystemException {
83  
84          ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
85              WebKeys.THEME_DISPLAY);
86  
87          Layout layout = themeDisplay.getLayout();
88  
89          String roleName = PropsValues.IFRAME_PASSWORD_PASSWORD_TOKEN_ROLE;
90  
91          if (Validator.isNull(roleName)) {
92              return true;
93          }
94  
95          if (layout.isPrivateLayout() && layout.getGroup().isUser()) {
96              return true;
97          }
98  
99          try {
100             Role role = RoleLocalServiceUtil.getRole(
101                 themeDisplay.getCompanyId(), roleName);
102 
103             if (UserLocalServiceUtil.hasRoleUser(
104                     role.getRoleId(), themeDisplay.getUserId())) {
105 
106                 return true;
107             }
108         }
109         catch (Exception e) {
110             if (_log.isWarnEnabled()) {
111                 _log.warn(
112                     "Error getting role " + roleName + ". The password token " +
113                         "will be disabled.");
114             }
115         }
116 
117         return false;
118     }
119 
120     private static Log _log = LogFactoryUtil.getLog(IFrameUtil.class);
121 
122 }