1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.taglib.security;
16  
17  import com.liferay.portal.kernel.util.HttpUtil;
18  import com.liferay.portal.kernel.util.PropsKeys;
19  import com.liferay.portal.kernel.util.PropsUtil;
20  import com.liferay.portal.kernel.util.Validator;
21  import com.liferay.portal.kernel.util.WebKeys;
22  import com.liferay.portal.model.Company;
23  import com.liferay.portal.theme.ThemeDisplay;
24  import com.liferay.util.Encryptor;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.jsp.JspException;
28  import javax.servlet.jsp.PageContext;
29  import javax.servlet.jsp.tagext.TagSupport;
30  
31  /**
32   * <a href="DoAsURLTag.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class DoAsURLTag extends TagSupport {
37  
38      public static String doTag(
39              long doAsUserId, String var, boolean writeOutput,
40              PageContext pageContext)
41          throws Exception {
42  
43          HttpServletRequest request =
44              (HttpServletRequest)pageContext.getRequest();
45  
46          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
47              WebKeys.THEME_DISPLAY);
48  
49          Company company = themeDisplay.getCompany();
50  
51          String doAsURL = company.getHomeURL();
52  
53          if (Validator.isNull(doAsURL)) {
54              doAsURL = _COMPANY_DEFAULT_HOME_URL;
55          }
56  
57          doAsURL = themeDisplay.getPathContext() + doAsURL;
58  
59          if (doAsUserId <= 0) {
60              doAsUserId = company.getDefaultUser().getUserId();
61          }
62  
63          String encDoAsUserId = Encryptor.encrypt(
64              company.getKeyObj(), String.valueOf(doAsUserId));
65  
66          doAsURL = HttpUtil.addParameter(
67              doAsURL, "doAsUserId", encDoAsUserId);
68  
69          if (Validator.isNotNull(var)) {
70              pageContext.setAttribute(var, doAsURL);
71          }
72          else if (writeOutput) {
73              pageContext.getOut().print(doAsURL);
74          }
75  
76          return doAsURL;
77      }
78  
79      public int doEndTag() throws JspException {
80          try {
81              doTag(_doAsUserId, _var, true, pageContext);
82          }
83          catch (Exception e) {
84              throw new JspException(e);
85          }
86  
87          return EVAL_PAGE;
88      }
89  
90      public void setDoAsUserId(long doAsUserId) {
91          _doAsUserId = doAsUserId;
92      }
93  
94      public void setVar(String var) {
95          _var = var;
96      }
97  
98      private static final String _COMPANY_DEFAULT_HOME_URL =
99          PropsUtil.get(PropsKeys.COMPANY_DEFAULT_HOME_URL);
100 
101     private long _doAsUserId;
102     private String _var;
103 
104 }