001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.service.impl;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.security.permission.PermissionChecker;
026: import com.liferay.portal.kernel.util.GetterUtil;
027: import com.liferay.portal.kernel.util.Validator;
028: import com.liferay.portal.model.User;
029: import com.liferay.portal.security.auth.CompanyThreadLocal;
030: import com.liferay.portal.security.auth.PrincipalException;
031: import com.liferay.portal.security.auth.PrincipalThreadLocal;
032: import com.liferay.portal.security.permission.PermissionThreadLocal;
033: import com.liferay.portal.service.UserLocalServiceUtil;
034:
035: /**
036: * <a href="PrincipalBean.java.html"><b><i>View Source</i></b></a>
037: *
038: * @author Brian Wing Shun Chan
039: *
040: */
041: public class PrincipalBean {
042:
043: public static final String JRUN_ANONYMOUS = "anonymus-guest";
044:
045: public static final String ORACLE_ANONYMOUS = "guest";
046:
047: public static final String SUN_ANONYMOUS = "ANONYMOUS";
048:
049: public static final String WEBLOGIC_ANONYMOUS = "<anonymous>";
050:
051: public static final String[] ANONYMOUS_NAMES = { JRUN_ANONYMOUS,
052: ORACLE_ANONYMOUS, SUN_ANONYMOUS, WEBLOGIC_ANONYMOUS };
053:
054: public long getGuestOrUserId() throws PrincipalException {
055: try {
056: return getUserId();
057: } catch (PrincipalException pe) {
058: try {
059: return UserLocalServiceUtil
060: .getDefaultUserId(CompanyThreadLocal
061: .getCompanyId());
062: } catch (Exception e) {
063: throw pe;
064: }
065: }
066: }
067:
068: public User getUser() throws PortalException, SystemException {
069: return UserLocalServiceUtil.getUserById(getUserId());
070: }
071:
072: public long getUserId() throws PrincipalException {
073: String name = PrincipalThreadLocal.getName();
074:
075: if (name == null) {
076: throw new PrincipalException();
077: }
078:
079: if (Validator.isNull(name)) {
080: throw new PrincipalException("Principal cannot be null");
081: } else {
082: for (int i = 0; i < ANONYMOUS_NAMES.length; i++) {
083: if (name.equalsIgnoreCase(ANONYMOUS_NAMES[i])) {
084: throw new PrincipalException("Principal cannot be "
085: + ANONYMOUS_NAMES[i]);
086: }
087: }
088: }
089:
090: return GetterUtil.getLong(name);
091: }
092:
093: public PermissionChecker getPermissionChecker()
094: throws PrincipalException {
095: PermissionChecker permissionChecker = PermissionThreadLocal
096: .getPermissionChecker();
097:
098: if (permissionChecker == null) {
099: throw new PrincipalException(
100: "PermissionChecker not initialized");
101: }
102:
103: return permissionChecker;
104: }
105:
106: }
|