001    /**
002     * Copyright (c) 2000-present 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.taglib.security;
016    
017    import com.liferay.portal.kernel.util.HttpUtil;
018    import com.liferay.portal.kernel.util.PropsKeys;
019    import com.liferay.portal.kernel.util.PropsUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.kernel.util.WebKeys;
022    import com.liferay.portal.model.Company;
023    import com.liferay.portal.model.Layout;
024    import com.liferay.portal.model.LayoutConstants;
025    import com.liferay.portal.service.LayoutLocalServiceUtil;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.util.Encryptor;
029    
030    import java.util.List;
031    
032    import javax.servlet.http.HttpServletRequest;
033    import javax.servlet.jsp.JspException;
034    import javax.servlet.jsp.JspWriter;
035    import javax.servlet.jsp.tagext.TagSupport;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class DoAsURLTag extends TagSupport {
041    
042            public static String doTag(long doAsUserId, HttpServletRequest request)
043                    throws Exception {
044    
045                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
046                            WebKeys.THEME_DISPLAY);
047    
048                    Company company = themeDisplay.getCompany();
049    
050                    String doAsURL = company.getHomeURL();
051    
052                    if (Validator.isNull(doAsURL)) {
053                            Layout layout = null;
054    
055                            List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
056                                    themeDisplay.getScopeGroupId(), false,
057                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
058    
059                            if (!layouts.isEmpty()) {
060                                    layout = layouts.get(0);
061    
062                                    doAsURL = PortalUtil.getLayoutFriendlyURL(layout, themeDisplay);
063                            }
064    
065                            if (Validator.isNull(doAsURL)) {
066                                    doAsURL =
067                                            themeDisplay.getPathContext() + _COMPANY_DEFAULT_HOME_URL;
068                            }
069                    }
070                    else {
071                            doAsURL = themeDisplay.getPathContext() + doAsURL;
072                    }
073    
074                    if (doAsUserId <= 0) {
075                            doAsUserId = company.getDefaultUser().getUserId();
076                    }
077    
078                    String encDoAsUserId = Encryptor.encrypt(
079                            company.getKeyObj(), String.valueOf(doAsUserId));
080    
081                    return HttpUtil.addParameter(doAsURL, "doAsUserId", encDoAsUserId);
082            }
083    
084            @Override
085            public int doEndTag() throws JspException {
086                    try {
087                            String doAsURL = doTag(
088                                    _doAsUserId, (HttpServletRequest)pageContext.getRequest());
089    
090                            if (Validator.isNotNull(_var)) {
091                                    pageContext.setAttribute(_var, doAsURL);
092                            }
093                            else {
094                                    JspWriter jspWriter = pageContext.getOut();
095    
096                                    jspWriter.write(doAsURL);
097                            }
098                    }
099                    catch (Exception e) {
100                            throw new JspException(e);
101                    }
102    
103                    return EVAL_PAGE;
104            }
105    
106            public void setDoAsUserId(long doAsUserId) {
107                    _doAsUserId = doAsUserId;
108            }
109    
110            public void setVar(String var) {
111                    _var = var;
112            }
113    
114            private static final String _COMPANY_DEFAULT_HOME_URL = PropsUtil.get(
115                    PropsKeys.COMPANY_DEFAULT_HOME_URL);
116    
117            private long _doAsUserId;
118            private String _var;
119    
120    }