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.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.JspWriter;
029    import javax.servlet.jsp.PageContext;
030    import javax.servlet.jsp.tagext.TagSupport;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class DoAsURLTag extends TagSupport {
036    
037            public static void doTag(
038                            long doAsUserId, String var, PageContext pageContext)
039                    throws Exception {
040    
041                    HttpServletRequest request =
042                            (HttpServletRequest)pageContext.getRequest();
043    
044                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
045                            WebKeys.THEME_DISPLAY);
046    
047                    Company company = themeDisplay.getCompany();
048    
049                    String doAsURL = company.getHomeURL();
050    
051                    if (Validator.isNull(doAsURL)) {
052                            doAsURL = _PORTAL_IMPERSONATION_DEFAULT_URL;
053                    }
054    
055                    doAsURL = themeDisplay.getPathContext() + doAsURL;
056    
057                    if (doAsUserId <= 0) {
058                            doAsUserId = company.getDefaultUser().getUserId();
059                    }
060    
061                    String encDoAsUserId = Encryptor.encrypt(
062                            company.getKeyObj(), String.valueOf(doAsUserId));
063    
064                    doAsURL = HttpUtil.addParameter(doAsURL, "doAsUserId", encDoAsUserId);
065    
066                    if (Validator.isNotNull(var)) {
067                            pageContext.setAttribute(var, doAsURL);
068                    }
069                    else {
070                            JspWriter jspWriter = pageContext.getOut();
071    
072                            jspWriter.write(doAsURL);
073                    }
074            }
075    
076            @Override
077            public int doEndTag() throws JspException {
078                    try {
079                            doTag(_doAsUserId, _var, pageContext);
080                    }
081                    catch (Exception e) {
082                            throw new JspException(e);
083                    }
084    
085                    return EVAL_PAGE;
086            }
087    
088            public void setDoAsUserId(long doAsUserId) {
089                    _doAsUserId = doAsUserId;
090            }
091    
092            public void setVar(String var) {
093                    _var = var;
094            }
095    
096            private static final String _PORTAL_IMPERSONATION_DEFAULT_URL =
097                    PropsUtil.get(PropsKeys.PORTAL_IMPERSONATION_DEFAULT_URL);
098    
099            private long _doAsUserId;
100            private String _var;
101    
102    }