001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.service.base;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.model.User;
022    import com.liferay.portal.security.auth.CompanyThreadLocal;
023    import com.liferay.portal.security.auth.PrincipalException;
024    import com.liferay.portal.security.auth.PrincipalThreadLocal;
025    import com.liferay.portal.security.permission.PermissionChecker;
026    import com.liferay.portal.security.permission.PermissionThreadLocal;
027    import com.liferay.portal.service.UserLocalServiceUtil;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    public class PrincipalBean {
033    
034            public static final String JRUN_ANONYMOUS = "anonymous-guest";
035    
036            public static final String ORACLE_ANONYMOUS = "guest";
037    
038            public static final String SUN_ANONYMOUS = "ANONYMOUS";
039    
040            public static final String WEBLOGIC_ANONYMOUS = "<anonymous>";
041    
042            public static final String[] ANONYMOUS_NAMES = {
043                    JRUN_ANONYMOUS, ORACLE_ANONYMOUS, SUN_ANONYMOUS, WEBLOGIC_ANONYMOUS
044            };
045    
046            public User getGuestOrUser() throws PortalException, SystemException {
047                    try {
048                            return getUser();
049                    }
050                    catch (PrincipalException pe) {
051                            try {
052                                    return UserLocalServiceUtil.getDefaultUser(
053                                            CompanyThreadLocal.getCompanyId());
054                            }
055                            catch (Exception e) {
056                                    throw pe;
057                            }
058                    }
059            }
060    
061            public long getGuestOrUserId() throws PrincipalException {
062                    try {
063                            return getUserId();
064                    }
065                    catch (PrincipalException pe) {
066                            try {
067                                    return UserLocalServiceUtil.getDefaultUserId(
068                                            CompanyThreadLocal.getCompanyId());
069                            }
070                            catch (Exception e) {
071                                    throw pe;
072                            }
073                    }
074            }
075    
076            public PermissionChecker getPermissionChecker() throws PrincipalException {
077                    PermissionChecker permissionChecker =
078                            PermissionThreadLocal.getPermissionChecker();
079    
080                    if (permissionChecker == null) {
081                            throw new PrincipalException("PermissionChecker not initialized");
082                    }
083    
084                    return permissionChecker;
085            }
086    
087            public User getUser() throws PortalException, SystemException {
088                    return UserLocalServiceUtil.getUserById(getUserId());
089            }
090    
091            public long getUserId() throws PrincipalException {
092                    String name = PrincipalThreadLocal.getName();
093    
094                    if (name == null) {
095                            throw new PrincipalException();
096                    }
097    
098                    if (Validator.isNull(name)) {
099                            throw new PrincipalException("Principal cannot be null");
100                    }
101                    else {
102                            for (int i = 0; i < ANONYMOUS_NAMES.length; i++) {
103                                    if (name.equalsIgnoreCase(ANONYMOUS_NAMES[i])) {
104                                            throw new PrincipalException(
105                                                    "Principal cannot be " + ANONYMOUS_NAMES[i]);
106                                    }
107                            }
108                    }
109    
110                    return GetterUtil.getLong(name);
111            }
112    
113    }