001    /**
002     * Copyright (c) 2000-2013 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.exception.SystemException;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.model.User;
023    import com.liferay.portal.security.auth.CompanyThreadLocal;
024    import com.liferay.portal.security.auth.PrincipalException;
025    import com.liferay.portal.security.auth.PrincipalThreadLocal;
026    import com.liferay.portal.security.permission.PermissionChecker;
027    import com.liferay.portal.security.permission.PermissionThreadLocal;
028    import com.liferay.portal.service.UserLocalServiceUtil;
029    
030    /**
031     * @author     Brian Wing Shun Chan
032     * @deprecated As of 6.2.0, replaced by {@link
033     *             com.liferay.portal.service.BaseServiceImpl}
034     */
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, SystemException {
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, SystemException {
092                    return UserLocalServiceUtil.getUserById(getUserId());
093            }
094    
095            public long getUserId() throws PrincipalException {
096                    String name = PrincipalThreadLocal.getName();
097    
098                    if (name == null) {
099                            throw new PrincipalException();
100                    }
101    
102                    if (Validator.isNull(name)) {
103                            throw new PrincipalException("Principal is null");
104                    }
105                    else {
106                            for (int i = 0; i < ANONYMOUS_NAMES.length; i++) {
107                                    if (StringUtil.equalsIgnoreCase(name, ANONYMOUS_NAMES[i])) {
108                                            throw new PrincipalException(
109                                                    "Principal cannot be " + ANONYMOUS_NAMES[i]);
110                                    }
111                            }
112                    }
113    
114                    return GetterUtil.getLong(name);
115            }
116    
117    }