1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service.base;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.User;
30  import com.liferay.portal.security.auth.CompanyThreadLocal;
31  import com.liferay.portal.security.auth.PrincipalException;
32  import com.liferay.portal.security.auth.PrincipalThreadLocal;
33  import com.liferay.portal.security.permission.PermissionChecker;
34  import com.liferay.portal.security.permission.PermissionThreadLocal;
35  import com.liferay.portal.service.UserLocalServiceUtil;
36  
37  /**
38   * <a href="PrincipalBean.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class PrincipalBean {
44  
45      public static final String JRUN_ANONYMOUS = "anonymous-guest";
46  
47      public static final String ORACLE_ANONYMOUS = "guest";
48  
49      public static final String SUN_ANONYMOUS = "ANONYMOUS";
50  
51      public static final String WEBLOGIC_ANONYMOUS = "<anonymous>";
52  
53      public static final String[] ANONYMOUS_NAMES = {
54          JRUN_ANONYMOUS, ORACLE_ANONYMOUS, SUN_ANONYMOUS, WEBLOGIC_ANONYMOUS
55      };
56  
57      public long getGuestOrUserId() throws PrincipalException {
58          try {
59              return getUserId();
60          }
61          catch (PrincipalException pe) {
62              try {
63                  return UserLocalServiceUtil.getDefaultUserId(
64                      CompanyThreadLocal.getCompanyId());
65              }
66              catch (Exception e) {
67                  throw pe;
68              }
69          }
70      }
71  
72      public PermissionChecker getPermissionChecker() throws PrincipalException {
73          PermissionChecker permissionChecker =
74              PermissionThreadLocal.getPermissionChecker();
75  
76          if (permissionChecker == null) {
77              throw new PrincipalException("PermissionChecker not initialized");
78          }
79  
80          return permissionChecker;
81      }
82  
83      public User getUser() throws PortalException, SystemException {
84          return UserLocalServiceUtil.getUserById(getUserId());
85      }
86  
87      public long getUserId() throws PrincipalException {
88          String name = PrincipalThreadLocal.getName();
89  
90          if (name == null) {
91              throw new PrincipalException();
92          }
93  
94          if (Validator.isNull(name)) {
95              throw new PrincipalException("Principal cannot be null");
96          }
97          else {
98              for (int i = 0; i < ANONYMOUS_NAMES.length; i++) {
99                  if (name.equalsIgnoreCase(ANONYMOUS_NAMES[i])) {
100                     throw new PrincipalException(
101                         "Principal cannot be " + ANONYMOUS_NAMES[i]);
102                 }
103             }
104         }
105 
106         return GetterUtil.getLong(name);
107     }
108 
109 }