1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.service.base;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.util.GetterUtil;
25  import com.liferay.portal.kernel.util.Validator;
26  import com.liferay.portal.model.User;
27  import com.liferay.portal.security.auth.CompanyThreadLocal;
28  import com.liferay.portal.security.auth.PrincipalException;
29  import com.liferay.portal.security.auth.PrincipalThreadLocal;
30  import com.liferay.portal.security.permission.PermissionChecker;
31  import com.liferay.portal.security.permission.PermissionThreadLocal;
32  import com.liferay.portal.service.UserLocalServiceUtil;
33  
34  /**
35   * <a href="PrincipalBean.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class PrincipalBean {
41  
42      public static final String JRUN_ANONYMOUS = "anonymous-guest";
43  
44      public static final String ORACLE_ANONYMOUS = "guest";
45  
46      public static final String SUN_ANONYMOUS = "ANONYMOUS";
47  
48      public static final String WEBLOGIC_ANONYMOUS = "<anonymous>";
49  
50      public static final String[] ANONYMOUS_NAMES = {
51          JRUN_ANONYMOUS, ORACLE_ANONYMOUS, SUN_ANONYMOUS, WEBLOGIC_ANONYMOUS
52      };
53  
54      public long getGuestOrUserId() throws PrincipalException {
55          try {
56              return getUserId();
57          }
58          catch (PrincipalException pe) {
59              try {
60                  return UserLocalServiceUtil.getDefaultUserId(
61                      CompanyThreadLocal.getCompanyId());
62              }
63              catch (Exception e) {
64                  throw pe;
65              }
66          }
67      }
68  
69      public PermissionChecker getPermissionChecker() throws PrincipalException {
70          PermissionChecker permissionChecker =
71              PermissionThreadLocal.getPermissionChecker();
72  
73          if (permissionChecker == null) {
74              throw new PrincipalException("PermissionChecker not initialized");
75          }
76  
77          return permissionChecker;
78      }
79  
80      public User getUser() throws PortalException, SystemException {
81          return UserLocalServiceUtil.getUserById(getUserId());
82      }
83  
84      public long getUserId() throws PrincipalException {
85          String name = PrincipalThreadLocal.getName();
86  
87          if (name == null) {
88              throw new PrincipalException();
89          }
90  
91          if (Validator.isNull(name)) {
92              throw new PrincipalException("Principal cannot be null");
93          }
94          else {
95              for (int i = 0; i < ANONYMOUS_NAMES.length; i++) {
96                  if (name.equalsIgnoreCase(ANONYMOUS_NAMES[i])) {
97                      throw new PrincipalException(
98                          "Principal cannot be " + ANONYMOUS_NAMES[i]);
99                  }
100             }
101         }
102 
103         return GetterUtil.getLong(name);
104     }
105 
106 }