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