001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.base;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.StringUtil;
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     * @deprecated As of 6.2.0, replaced by {@link
032     *             com.liferay.portal.service.BaseServiceImpl}
033     */
034    @Deprecated
035    public class PrincipalBean {
036    
037            public static final String[] ANONYMOUS_NAMES = {
038                    PrincipalBean.JRUN_ANONYMOUS, PrincipalBean.ORACLE_ANONYMOUS,
039                    PrincipalBean.SUN_ANONYMOUS, PrincipalBean.WEBLOGIC_ANONYMOUS
040            };
041    
042            public static final String JRUN_ANONYMOUS = "anonymous-guest";
043    
044            public static final String ORACLE_ANONYMOUS = "guest";
045    
046            public static final String SUN_ANONYMOUS = "ANONYMOUS";
047    
048            public static final String WEBLOGIC_ANONYMOUS = "<anonymous>";
049    
050            public User getGuestOrUser() throws PortalException {
051                    try {
052                            return getUser();
053                    }
054                    catch (PrincipalException pe) {
055                            try {
056                                    return UserLocalServiceUtil.getDefaultUser(
057                                            CompanyThreadLocal.getCompanyId());
058                            }
059                            catch (Exception e) {
060                                    throw pe;
061                            }
062                    }
063            }
064    
065            public long getGuestOrUserId() throws PrincipalException {
066                    try {
067                            return getUserId();
068                    }
069                    catch (PrincipalException pe) {
070                            try {
071                                    return UserLocalServiceUtil.getDefaultUserId(
072                                            CompanyThreadLocal.getCompanyId());
073                            }
074                            catch (Exception e) {
075                                    throw pe;
076                            }
077                    }
078            }
079    
080            public PermissionChecker getPermissionChecker() throws PrincipalException {
081                    PermissionChecker permissionChecker =
082                            PermissionThreadLocal.getPermissionChecker();
083    
084                    if (permissionChecker == null) {
085                            throw new PrincipalException("PermissionChecker not initialized");
086                    }
087    
088                    return permissionChecker;
089            }
090    
091            public User getUser() throws PortalException {
092                    return UserLocalServiceUtil.getUserById(getUserId());
093            }
094    
095            public long getUserId() throws PrincipalException {
096                    String name = PrincipalThreadLocal.getName();
097    
098                    if (Validator.isNull(name)) {
099                            throw new PrincipalException("Principal is null");
100                    }
101                    else {
102                            for (int i = 0; i < ANONYMOUS_NAMES.length; i++) {
103                                    if (StringUtil.equalsIgnoreCase(name, 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    }