1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class PrincipalBean {
43  
44      public static final String JRUN_ANONYMOUS = "anonymous-guest";
45  
46      public static final String ORACLE_ANONYMOUS = "guest";
47  
48      public static final String SUN_ANONYMOUS = "ANONYMOUS";
49  
50      public static final String WEBLOGIC_ANONYMOUS = "<anonymous>";
51  
52      public static final String[] ANONYMOUS_NAMES = {
53          JRUN_ANONYMOUS, ORACLE_ANONYMOUS, SUN_ANONYMOUS, WEBLOGIC_ANONYMOUS
54      };
55  
56      public long getGuestOrUserId() throws PrincipalException {
57          try {
58              return getUserId();
59          }
60          catch (PrincipalException pe) {
61              try {
62                  return UserLocalServiceUtil.getDefaultUserId(
63                      CompanyThreadLocal.getCompanyId());
64              }
65              catch (Exception e) {
66                  throw pe;
67              }
68          }
69      }
70  
71      public PermissionChecker getPermissionChecker() throws PrincipalException {
72          PermissionChecker permissionChecker =
73              PermissionThreadLocal.getPermissionChecker();
74  
75          if (permissionChecker == null) {
76              throw new PrincipalException("PermissionChecker not initialized");
77          }
78  
79          return permissionChecker;
80      }
81  
82      public User getUser() throws PortalException, SystemException {
83          return UserLocalServiceUtil.getUserById(getUserId());
84      }
85  
86      public long getUserId() throws PrincipalException {
87          String name = PrincipalThreadLocal.getName();
88  
89          if (name == null) {
90              throw new PrincipalException();
91          }
92  
93          if (Validator.isNull(name)) {
94              throw new PrincipalException("Principal cannot be null");
95          }
96          else {
97              for (int i = 0; i < ANONYMOUS_NAMES.length; i++) {
98                  if (name.equalsIgnoreCase(ANONYMOUS_NAMES[i])) {
99                      throw new PrincipalException(
100                         "Principal cannot be " + ANONYMOUS_NAMES[i]);
101                 }
102             }
103         }
104 
105         return GetterUtil.getLong(name);
106     }
107 
108 }