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          if (doAsUserId <= 0) {
58              doAsUserId = company.getDefaultUser().getUserId();
59          }
60  
61          String encDoAsUserId = Encryptor.encrypt(
62              company.getKeyObj(), String.valueOf(doAsUserId));
63  
64          doAsURL = HttpUtil.addParameter(
65              doAsURL, "doAsUserId", encDoAsUserId);
66  
67          if (Validator.isNotNull(var)) {
68              pageContext.setAttribute(var, doAsURL);
69          }
70          else if (writeOutput) {
71              pageContext.getOut().print(doAsURL);
72          }
73  
74          return doAsURL;
75      }
76  
77      public int doEndTag() throws JspException {
78          try {
79              doTag(_doAsUserId, _var, true, pageContext);
80          }
81          catch (Exception e) {
82              throw new JspException(e);
83          }
84  
85          return EVAL_PAGE;
86      }
87  
88      public void setDoAsUserId(long doAsUserId) {
89          _doAsUserId = doAsUserId;
90      }
91  
92      public void setVar(String var) {
93          _var = var;
94      }
95  
96      private static final String _COMPANY_DEFAULT_HOME_URL =
97          PropsUtil.get(PropsKeys.COMPANY_DEFAULT_HOME_URL);
98  
99      private long _doAsUserId;
100     private String _var;
101 
102 }