001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.theme.ThemeDisplay;
024    import com.liferay.util.Encryptor;
025    
026    import javax.servlet.http.HttpServletRequest;
027    import javax.servlet.jsp.JspException;
028    import javax.servlet.jsp.PageContext;
029    import javax.servlet.jsp.tagext.TagSupport;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class DoAsURLTag extends TagSupport {
035    
036            public static void doTag(
037                            long doAsUserId, String var, PageContext pageContext)
038                    throws Exception {
039    
040                    HttpServletRequest request =
041                            (HttpServletRequest)pageContext.getRequest();
042    
043                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
044                            WebKeys.THEME_DISPLAY);
045    
046                    Company company = themeDisplay.getCompany();
047    
048                    String doAsURL = company.getHomeURL();
049    
050                    if (Validator.isNull(doAsURL)) {
051                            doAsURL = _COMPANY_DEFAULT_HOME_URL;
052                    }
053    
054                    if (doAsUserId <= 0) {
055                            doAsUserId = company.getDefaultUser().getUserId();
056                    }
057    
058                    String encDoAsUserId = Encryptor.encrypt(
059                            company.getKeyObj(), String.valueOf(doAsUserId));
060    
061                    doAsURL = HttpUtil.addParameter(
062                            doAsURL, "doAsUserId", encDoAsUserId);
063    
064                    if (Validator.isNotNull(var)) {
065                            pageContext.setAttribute(var, doAsURL);
066                    }
067                    else {
068                            pageContext.getOut().print(doAsURL);
069                    }
070            }
071    
072            public int doEndTag() throws JspException {
073                    try {
074                            doTag(_doAsUserId, _var, pageContext);
075                    }
076                    catch (Exception e) {
077                            throw new JspException(e);
078                    }
079    
080                    return EVAL_PAGE;
081            }
082    
083            public void setDoAsUserId(long doAsUserId) {
084                    _doAsUserId = doAsUserId;
085            }
086    
087            public void setVar(String var) {
088                    _var = var;
089            }
090    
091            private static final String _COMPANY_DEFAULT_HOME_URL =
092                    PropsUtil.get(PropsKeys.COMPANY_DEFAULT_HOME_URL);
093    
094            private long _doAsUserId;
095            private String _var;
096    
097    }