001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.service.permission;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.model.Group;
020    import com.liferay.portal.model.Organization;
021    import com.liferay.portal.model.OrganizationConstants;
022    import com.liferay.portal.security.auth.PrincipalException;
023    import com.liferay.portal.security.permission.ActionKeys;
024    import com.liferay.portal.security.permission.PermissionChecker;
025    import com.liferay.portal.service.OrganizationLocalServiceUtil;
026    
027    /**
028     * @author Charles May
029     * @author Jorge Ferrer
030     */
031    public class OrganizationPermissionImpl implements OrganizationPermission {
032    
033            public void check(
034                            PermissionChecker permissionChecker, long organizationId,
035                            String actionId)
036                    throws PortalException, SystemException {
037    
038                    if (!contains(permissionChecker, organizationId, actionId)) {
039                            throw new PrincipalException();
040                    }
041            }
042    
043            public void check(
044                            PermissionChecker permissionChecker, Organization organization,
045                            String actionId)
046                    throws PortalException, SystemException {
047    
048                    if (!contains(permissionChecker, organization, actionId)) {
049                            throw new PrincipalException();
050                    }
051            }
052    
053            public boolean contains(
054                            PermissionChecker permissionChecker, long organizationId,
055                            String actionId)
056                    throws PortalException, SystemException {
057    
058                    if (organizationId > 0) {
059                            Organization organization =
060                                    OrganizationLocalServiceUtil.getOrganization(organizationId);
061    
062                            return contains(permissionChecker, organization, actionId);
063                    }
064                    else {
065                            return false;
066                    }
067            }
068    
069            public boolean contains(
070                            PermissionChecker permissionChecker, Organization organization,
071                            String actionId)
072                    throws PortalException, SystemException {
073    
074                    Group group = organization.getGroup();
075    
076                    long groupId = group.getGroupId();
077    
078                    if (contains(permissionChecker, groupId, organization, actionId)) {
079                            return true;
080                    }
081    
082                    while (!organization.isRoot()) {
083                            Organization parentOrganization =
084                                    organization.getParentOrganization();
085    
086                            Group parentGroup = parentOrganization.getGroup();
087    
088                            groupId = parentGroup.getGroupId();
089    
090                            if (contains(
091                                            permissionChecker, groupId, parentOrganization,
092                                            ActionKeys.MANAGE_SUBORGANIZATIONS)) {
093    
094                                    return true;
095                            }
096    
097                            organization = parentOrganization;
098                    }
099    
100                    return false;
101            }
102    
103            protected boolean contains(
104                            PermissionChecker permissionChecker, long groupId,
105                            Organization organization, String actionId)
106                    throws PortalException, SystemException {
107    
108                    while ((organization != null) &&
109                               (organization.getOrganizationId() !=
110                                            OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID)) {
111    
112                            if (permissionChecker.hasPermission(
113                                            groupId, Organization.class.getName(),
114                                            organization.getOrganizationId(), actionId)) {
115    
116                                    return true;
117                            }
118    
119                            organization = organization.getParentOrganization();
120                    }
121    
122                    return false;
123            }
124    
125    }