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.permission;
022:
023: import com.liferay.portal.kernel.security.permission.ActionKeys;
024: import com.liferay.portal.kernel.security.permission.PermissionChecker;
025: import com.liferay.portal.model.User;
026: import com.liferay.portal.security.auth.PrincipalException;
027: import com.liferay.portal.security.permission.PermissionCheckerImpl;
028: import com.liferay.portal.service.UserLocalServiceUtil;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032:
033: /**
034: * <a href="UserPermissionImpl.java.html"><b><i>View Source</i></b></a>
035: *
036: * @author Charles May
037: * @author Jorge Ferrer
038: *
039: */
040: public class UserPermissionImpl implements UserPermission {
041:
042: public void check(PermissionChecker permissionChecker, long userId,
043: String actionId) throws PrincipalException {
044:
045: if (!contains(permissionChecker, userId, actionId)) {
046: throw new PrincipalException();
047: }
048: }
049:
050: /**
051: * @deprecated
052: */
053: public void check(PermissionChecker permissionChecker, long userId,
054: long organizationId, long locationId, String actionId)
055: throws PrincipalException {
056:
057: check(permissionChecker, userId, new long[] { organizationId,
058: locationId }, actionId);
059: }
060:
061: public void check(PermissionChecker permissionChecker, long userId,
062: long[] organizationIds, String actionId)
063: throws PrincipalException {
064:
065: if (!contains(permissionChecker, userId, organizationIds,
066: actionId)) {
067:
068: throw new PrincipalException();
069: }
070: }
071:
072: public boolean contains(PermissionChecker permissionChecker,
073: long userId, String actionId) {
074:
075: return contains(permissionChecker, userId, null, actionId);
076: }
077:
078: /**
079: * @deprecated
080: */
081: public boolean contains(PermissionChecker permissionChecker,
082: long userId, long organizationId, long locationId,
083: String actionId) {
084:
085: return contains(permissionChecker, userId, new long[] {
086: organizationId, locationId }, actionId);
087: }
088:
089: public boolean contains(PermissionChecker permissionChecker,
090: long userId, long[] organizationIds, String actionId) {
091:
092: PermissionCheckerImpl permissionCheckerImpl = (PermissionCheckerImpl) permissionChecker;
093:
094: if (permissionCheckerImpl.getUserId() == userId) {
095: return true;
096: } else if (permissionChecker.hasPermission(0, User.class
097: .getName(), userId, actionId)) {
098:
099: return true;
100: } else {
101: try {
102: if (organizationIds == null) {
103: User user = UserLocalServiceUtil
104: .getUserById(userId);
105:
106: organizationIds = user.getOrganizationIds();
107: }
108:
109: for (int i = 0; i < organizationIds.length; i++) {
110: long organizationId = organizationIds[i];
111:
112: if (OrganizationPermissionUtil.contains(
113: permissionChecker, organizationId,
114: ActionKeys.MANAGE_USERS)) {
115:
116: return true;
117: }
118: }
119: } catch (Exception e) {
120: _log.error(e, e);
121: }
122: }
123:
124: return false;
125: }
126:
127: private static Log _log = LogFactory
128: .getLog(UserPermissionImpl.class);
129:
130: }
|