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.Validator;
19  import com.liferay.portal.kernel.util.WebKeys;
20  import com.liferay.portal.model.Company;
21  import com.liferay.portal.model.Group;
22  import com.liferay.portal.model.GroupConstants;
23  import com.liferay.portal.model.Layout;
24  import com.liferay.portal.service.GroupLocalServiceUtil;
25  import com.liferay.portal.theme.ThemeDisplay;
26  import com.liferay.util.Encryptor;
27  
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.jsp.JspException;
30  import javax.servlet.jsp.PageContext;
31  import javax.servlet.jsp.tagext.TagSupport;
32  
33  /**
34   * <a href="DoAsURLTag.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public class DoAsURLTag extends TagSupport {
39  
40      public static String doTag(
41              long doAsUserId, String var, boolean writeOutput,
42              PageContext pageContext)
43          throws Exception {
44  
45          HttpServletRequest request =
46              (HttpServletRequest)pageContext.getRequest();
47  
48          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
49              WebKeys.THEME_DISPLAY);
50  
51          Company company = themeDisplay.getCompany();
52          Layout layout = themeDisplay.getLayout();
53  
54          Group guestGroup = GroupLocalServiceUtil.getGroup(
55              company.getCompanyId(), GroupConstants.GUEST);
56  
57          String doAsURL =
58              guestGroup.getPathFriendlyURL(false, themeDisplay) +
59                  guestGroup.getFriendlyURL();
60  
61          if (doAsUserId <= 0) {
62              doAsUserId = company.getDefaultUser().getUserId();
63          }
64  
65          String encDoAsUserId = Encryptor.encrypt(
66              company.getKeyObj(), String.valueOf(doAsUserId));
67  
68          doAsURL = HttpUtil.addParameter(doAsURL, "doAsUserId", encDoAsUserId);
69  
70          if (Validator.isNotNull(var)) {
71              pageContext.setAttribute(var, doAsURL);
72          }
73          else if (writeOutput) {
74              pageContext.getOut().print(doAsURL);
75          }
76  
77          return doAsURL;
78      }
79  
80      public int doEndTag() throws JspException {
81          try {
82              doTag(_doAsUserId, _var, true, pageContext);
83          }
84          catch (Exception e) {
85              throw new JspException(e);
86          }
87  
88          return EVAL_PAGE;
89      }
90  
91      public void setDoAsUserId(long doAsUserId) {
92          _doAsUserId = doAsUserId;
93      }
94  
95      public void setVar(String var) {
96          _var = var;
97      }
98  
99      private long _doAsUserId;
100     private String _var;
101 
102 }