001    /**
002     * Copyright (c) 2000-2010 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(
083                            PortletRequest portletRequest)
084                    throws PortalException, SystemException {
085    
086                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
087                            WebKeys.THEME_DISPLAY);
088    
089                    Layout layout = themeDisplay.getLayout();
090    
091                    String roleName = PropsValues.IFRAME_PASSWORD_PASSWORD_TOKEN_ROLE;
092    
093                    if (Validator.isNull(roleName)) {
094                            return true;
095                    }
096    
097                    if (layout.isPrivateLayout() && layout.getGroup().isUser()) {
098                            return true;
099                    }
100    
101                    try {
102                            Role role = RoleLocalServiceUtil.getRole(
103                                    themeDisplay.getCompanyId(), roleName);
104    
105                            if (UserLocalServiceUtil.hasRoleUser(
106                                            role.getRoleId(), themeDisplay.getUserId())) {
107    
108                                    return true;
109                            }
110                    }
111                    catch (Exception e) {
112                            if (_log.isWarnEnabled()) {
113                                    _log.warn(
114                                            "Error getting role " + roleName + ". The password token " +
115                                                    "will be disabled.");
116                            }
117                    }
118    
119                    return false;
120            }
121    
122            private static Log _log = LogFactoryUtil.getLog(IFrameUtil.class);
123    
124    }