1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.taglib.security;
16  
17  import com.liferay.portal.kernel.util.HttpUtil;
18  import com.liferay.portal.kernel.util.Validator;
19  import com.liferay.portal.kernel.util.WebKeys;
20  import com.liferay.portal.model.Company;
21  import com.liferay.portal.theme.ThemeDisplay;
22  import com.liferay.portal.util.PropsValues;
23  import com.liferay.util.Encryptor;
24  
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.jsp.JspException;
27  import javax.servlet.jsp.PageContext;
28  import javax.servlet.jsp.tagext.TagSupport;
29  
30  /**
31   * <a href="DoAsURLTag.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   */
35  public class DoAsURLTag extends TagSupport {
36  
37      public static String doTag(
38              long doAsUserId, String var, boolean writeOutput,
39              PageContext pageContext)
40          throws Exception {
41  
42          HttpServletRequest request =
43              (HttpServletRequest)pageContext.getRequest();
44  
45          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
46              WebKeys.THEME_DISPLAY);
47  
48          Company company = themeDisplay.getCompany();
49  
50          String doAsURL = company.getHomeURL();
51  
52          if (Validator.isNull(doAsURL)) {
53              doAsURL = PropsValues.COMPANY_DEFAULT_HOME_URL;
54          }
55  
56          if (doAsUserId <= 0) {
57              doAsUserId = company.getDefaultUser().getUserId();
58          }
59  
60          String encDoAsUserId = Encryptor.encrypt(
61              company.getKeyObj(), String.valueOf(doAsUserId));
62  
63          doAsURL = HttpUtil.addParameter(
64              doAsURL, "doAsUserId", encDoAsUserId);
65  
66          if (Validator.isNotNull(var)) {
67              pageContext.setAttribute(var, doAsURL);
68          }
69          else if (writeOutput) {
70              pageContext.getOut().print(doAsURL);
71          }
72  
73          return doAsURL;
74      }
75  
76      public int doEndTag() throws JspException {
77          try {
78              doTag(_doAsUserId, _var, true, pageContext);
79          }
80          catch (Exception e) {
81              throw new JspException(e);
82          }
83  
84          return EVAL_PAGE;
85      }
86  
87      public void setDoAsUserId(long doAsUserId) {
88          _doAsUserId = doAsUserId;
89      }
90  
91      public void setVar(String var) {
92          _var = var;
93      }
94  
95      private long _doAsUserId;
96      private String _var;
97  
98  }