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.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.security.permission.ActionKeys;
026: import com.liferay.portal.kernel.security.permission.PermissionChecker;
027: import com.liferay.portal.model.Group;
028: import com.liferay.portal.model.Organization;
029: import com.liferay.portal.model.impl.OrganizationImpl;
030: import com.liferay.portal.security.auth.PrincipalException;
031: import com.liferay.portal.service.OrganizationLocalServiceUtil;
032:
033: /**
034: * <a href="OrganizationPermissionImpl.java.html"><b><i>View Source</i></b></a>
035: *
036: * @author Charles May
037: * @author Jorge Ferrer
038: *
039: */
040: public class OrganizationPermissionImpl implements
041: OrganizationPermission {
042:
043: public void check(PermissionChecker permissionChecker,
044: long organizationId, String actionId)
045: throws PortalException, SystemException {
046:
047: if (!contains(permissionChecker, organizationId, actionId)) {
048: throw new PrincipalException();
049: }
050: }
051:
052: public boolean contains(PermissionChecker permissionChecker,
053: long organizationId, String actionId)
054: throws PortalException, SystemException {
055:
056: Organization organization = null;
057:
058: long groupId = 0;
059:
060: if (organizationId > 0) {
061: organization = OrganizationLocalServiceUtil
062: .getOrganization(organizationId);
063:
064: Group group = organization.getGroup();
065:
066: groupId = group.getGroupId();
067: }
068:
069: if (contains(permissionChecker, groupId, organizationId,
070: actionId)) {
071: return true;
072: }
073:
074: if ((!actionId.equals(ActionKeys.MANAGE_SUBORGANIZATIONS))
075: && (organization != null)) {
076:
077: if (contains(permissionChecker, groupId, organization
078: .getParentOrganizationId(),
079: ActionKeys.MANAGE_SUBORGANIZATIONS)) {
080:
081: return true;
082: }
083: }
084:
085: return false;
086: }
087:
088: protected boolean contains(PermissionChecker permissionChecker,
089: long groupId, long organizationId, String actionId)
090: throws PortalException, SystemException {
091:
092: while (organizationId != OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID) {
093:
094: if (permissionChecker.hasPermission(groupId,
095: Organization.class.getName(), organizationId,
096: actionId)) {
097:
098: return true;
099: }
100:
101: Organization organization = OrganizationLocalServiceUtil
102: .getOrganization(organizationId);
103:
104: organizationId = organization.getParentOrganizationId();
105: }
106:
107: return false;
108: }
109:
110: }
|