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