1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portlet.iframe.util;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.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  
45          if (!isPasswordTokenEnabled(portletRequest)) {
46              return StringPool.BLANK;
47          }
48  
49          if (Validator.isNull(password) || password.equals("@password@")) {
50              password = PortalUtil.getUserPassword(portletRequest);
51          }
52  
53          return password;
54      }
55  
56      public static String getUserName(
57              PortletRequest portletRequest, String userName)
58          throws PortalException, SystemException {
59  
60          User user = PortalUtil.getUser(portletRequest);
61  
62          if (user == null) {
63              return userName;
64          }
65  
66          if (Validator.isNull(userName) || userName.equals("@user_id@")) {
67              userName = portletRequest.getRemoteUser();
68          }
69          else if (userName.equals("@email_address@")) {
70              userName = user.getEmailAddress();
71          }
72          else if (userName.equals("@screen_name@")) {
73              userName = user.getScreenName();
74          }
75  
76          return userName;
77      }
78  
79      public static boolean isPasswordTokenEnabled(
80          PortletRequest portletRequest) {
81  
82          ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
83              WebKeys.THEME_DISPLAY);
84  
85          Layout layout = themeDisplay.getLayout();
86  
87          String roleName = PropsValues.IFRAME_PASSWORD_PASSWORD_TOKEN_ROLE;
88  
89          if (Validator.isNull(roleName)) {
90              return true;
91          }
92  
93          if (layout.isPrivateLayout() && layout.getGroup().isUser()) {
94              return true;
95          }
96  
97          try {
98              Role role = RoleLocalServiceUtil.getRole(
99                  themeDisplay.getCompanyId(), roleName);
100 
101             if (UserLocalServiceUtil.hasRoleUser(
102                     role.getRoleId(), themeDisplay.getUserId())) {
103 
104                 return true;
105             }
106         }
107         catch (Exception e) {
108             if (_log.isWarnEnabled()) {
109                 _log.warn(
110                     "Error getting role " + roleName + ". The password token " +
111                         "will be disabled.");
112             }
113         }
114 
115         return false;
116     }
117 
118     private static Log _log = LogFactoryUtil.getLog(IFrameUtil.class);
119 
120 }