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.portal.service.base;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.Validator;
21  import com.liferay.portal.model.User;
22  import com.liferay.portal.security.auth.CompanyThreadLocal;
23  import com.liferay.portal.security.auth.PrincipalException;
24  import com.liferay.portal.security.auth.PrincipalThreadLocal;
25  import com.liferay.portal.security.permission.PermissionChecker;
26  import com.liferay.portal.security.permission.PermissionThreadLocal;
27  import com.liferay.portal.service.UserLocalServiceUtil;
28  
29  /**
30   * <a href="PrincipalBean.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Brian Wing Shun Chan
33   */
34  public class PrincipalBean {
35  
36      public static final String JRUN_ANONYMOUS = "anonymous-guest";
37  
38      public static final String ORACLE_ANONYMOUS = "guest";
39  
40      public static final String SUN_ANONYMOUS = "ANONYMOUS";
41  
42      public static final String WEBLOGIC_ANONYMOUS = "<anonymous>";
43  
44      public static final String[] ANONYMOUS_NAMES = {
45          JRUN_ANONYMOUS, ORACLE_ANONYMOUS, SUN_ANONYMOUS, WEBLOGIC_ANONYMOUS
46      };
47  
48      public User getGuestOrUser() throws PortalException, SystemException {
49          try {
50              return getUser();
51          }
52          catch (PrincipalException pe) {
53              try {
54                  return UserLocalServiceUtil.getDefaultUser(
55                      CompanyThreadLocal.getCompanyId());
56              }
57              catch (Exception e) {
58                  throw pe;
59              }
60          }
61      }
62  
63      public long getGuestOrUserId() throws PrincipalException {
64          try {
65              return getUserId();
66          }
67          catch (PrincipalException pe) {
68              try {
69                  return UserLocalServiceUtil.getDefaultUserId(
70                      CompanyThreadLocal.getCompanyId());
71              }
72              catch (Exception e) {
73                  throw pe;
74              }
75          }
76      }
77  
78      public PermissionChecker getPermissionChecker() throws PrincipalException {
79          PermissionChecker permissionChecker =
80              PermissionThreadLocal.getPermissionChecker();
81  
82          if (permissionChecker == null) {
83              throw new PrincipalException("PermissionChecker not initialized");
84          }
85  
86          return permissionChecker;
87      }
88  
89      public User getUser() throws PortalException, SystemException {
90          return UserLocalServiceUtil.getUserById(getUserId());
91      }
92  
93      public long getUserId() throws PrincipalException {
94          String name = PrincipalThreadLocal.getName();
95  
96          if (name == null) {
97              throw new PrincipalException();
98          }
99  
100         if (Validator.isNull(name)) {
101             throw new PrincipalException("Principal cannot be null");
102         }
103         else {
104             for (int i = 0; i < ANONYMOUS_NAMES.length; i++) {
105                 if (name.equalsIgnoreCase(ANONYMOUS_NAMES[i])) {
106                     throw new PrincipalException(
107                         "Principal cannot be " + ANONYMOUS_NAMES[i]);
108                 }
109             }
110         }
111 
112         return GetterUtil.getLong(name);
113     }
114 
115 }