001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.iframe.util;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.model.Layout;
024    import com.liferay.portal.model.Role;
025    import com.liferay.portal.model.User;
026    import com.liferay.portal.service.RoleLocalServiceUtil;
027    import com.liferay.portal.service.UserLocalServiceUtil;
028    import com.liferay.portal.theme.ThemeDisplay;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.portal.util.PropsValues;
031    import com.liferay.portal.util.WebKeys;
032    
033    import javax.portlet.PortletRequest;
034    
035    /**
036     * @author Amos Fong
037     */
038    public class IFrameUtil {
039    
040            public static String getPassword(
041                            PortletRequest portletRequest, String password)
042                    throws PortalException, SystemException {
043    
044                    if (!isPasswordTokenEnabled(portletRequest)) {
045                            return StringPool.BLANK;
046                    }
047    
048                    if (Validator.isNull(password) || password.equals("@password@")) {
049                            password = PortalUtil.getUserPassword(portletRequest);
050    
051                            if (password == null) {
052                                    password = StringPool.BLANK;
053                            }
054                    }
055    
056                    return password;
057            }
058    
059            public static String getUserName(
060                            PortletRequest portletRequest, String userName)
061                    throws PortalException, SystemException {
062    
063                    User user = PortalUtil.getUser(portletRequest);
064    
065                    if (user == null) {
066                            return userName;
067                    }
068    
069                    if (Validator.isNull(userName) || userName.equals("@user_id@")) {
070                            userName = portletRequest.getRemoteUser();
071                    }
072                    else if (userName.equals("@email_address@")) {
073                            userName = user.getEmailAddress();
074                    }
075                    else if (userName.equals("@screen_name@")) {
076                            userName = user.getScreenName();
077                    }
078    
079                    return userName;
080            }
081    
082            public static boolean isPasswordTokenEnabled(PortletRequest portletRequest)
083                    throws PortalException, SystemException {
084    
085                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
086                            WebKeys.THEME_DISPLAY);
087    
088                    Layout layout = themeDisplay.getLayout();
089    
090                    String roleName = PropsValues.IFRAME_PASSWORD_PASSWORD_TOKEN_ROLE;
091    
092                    if (Validator.isNull(roleName)) {
093                            return true;
094                    }
095    
096                    if (layout.isPrivateLayout() && layout.getGroup().isUser()) {
097                            return true;
098                    }
099    
100                    try {
101                            Role role = RoleLocalServiceUtil.getRole(
102                                    themeDisplay.getCompanyId(), roleName);
103    
104                            if (UserLocalServiceUtil.hasRoleUser(
105                                            role.getRoleId(), themeDisplay.getUserId())) {
106    
107                                    return true;
108                            }
109                    }
110                    catch (Exception e) {
111                            if (_log.isWarnEnabled()) {
112                                    _log.warn(
113                                            "Error getting role " + roleName + ". The password token " +
114                                                    "will be disabled.");
115                            }
116                    }
117    
118                    return false;
119            }
120    
121            private static Log _log = LogFactoryUtil.getLog(IFrameUtil.class);
122    
123    }