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.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    }