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.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 void doTag(
39              long doAsUserId, String var, 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 = _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 {
70              pageContext.getOut().print(doAsURL);
71          }
72      }
73  
74      public int doEndTag() throws JspException {
75          try {
76              doTag(_doAsUserId, _var, pageContext);
77          }
78          catch (Exception e) {
79              throw new JspException(e);
80          }
81  
82          return EVAL_PAGE;
83      }
84  
85      public void setDoAsUserId(long doAsUserId) {
86          _doAsUserId = doAsUserId;
87      }
88  
89      public void setVar(String var) {
90          _var = var;
91      }
92  
93      private static final String _COMPANY_DEFAULT_HOME_URL =
94          PropsUtil.get(PropsKeys.COMPANY_DEFAULT_HOME_URL);
95  
96      private long _doAsUserId;
97      private String _var;
98  
99  }