1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.service.base;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.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 long getGuestOrUserId() throws PrincipalException {
49          try {
50              return getUserId();
51          }
52          catch (PrincipalException pe) {
53              try {
54                  return UserLocalServiceUtil.getDefaultUserId(
55                      CompanyThreadLocal.getCompanyId());
56              }
57              catch (Exception e) {
58                  throw pe;
59              }
60          }
61      }
62  
63      public PermissionChecker getPermissionChecker() throws PrincipalException {
64          PermissionChecker permissionChecker =
65              PermissionThreadLocal.getPermissionChecker();
66  
67          if (permissionChecker == null) {
68              throw new PrincipalException("PermissionChecker not initialized");
69          }
70  
71          return permissionChecker;
72      }
73  
74      public User getUser() throws PortalException, SystemException {
75          return UserLocalServiceUtil.getUserById(getUserId());
76      }
77  
78      public long getUserId() throws PrincipalException {
79          String name = PrincipalThreadLocal.getName();
80  
81          if (name == null) {
82              throw new PrincipalException();
83          }
84  
85          if (Validator.isNull(name)) {
86              throw new PrincipalException("Principal cannot be null");
87          }
88          else {
89              for (int i = 0; i < ANONYMOUS_NAMES.length; i++) {
90                  if (name.equalsIgnoreCase(ANONYMOUS_NAMES[i])) {
91                      throw new PrincipalException(
92                          "Principal cannot be " + ANONYMOUS_NAMES[i]);
93                  }
94              }
95          }
96  
97          return GetterUtil.getLong(name);
98      }
99  
100 }