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.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            @Override
034            public void check(
035                            PermissionChecker permissionChecker, long organizationId,
036                            String actionId)
037                    throws PortalException, SystemException {
038    
039                    if (!contains(permissionChecker, organizationId, actionId)) {
040                            throw new PrincipalException();
041                    }
042            }
043    
044            @Override
045            public void check(
046                            PermissionChecker permissionChecker, Organization organization,
047                            String actionId)
048                    throws PortalException, SystemException {
049    
050                    if (!contains(permissionChecker, organization, actionId)) {
051                            throw new PrincipalException();
052                    }
053            }
054    
055            @Override
056            public boolean contains(
057                            PermissionChecker permissionChecker, long organizationId,
058                            String actionId)
059                    throws PortalException, SystemException {
060    
061                    if (organizationId > 0) {
062                            Organization organization =
063                                    OrganizationLocalServiceUtil.getOrganization(organizationId);
064    
065                            return contains(permissionChecker, organization, actionId);
066                    }
067                    else {
068                            return false;
069                    }
070            }
071    
072            @Override
073            public boolean contains(
074                            PermissionChecker permissionChecker, long[] organizationIds,
075                            String actionId)
076                    throws PortalException, SystemException {
077    
078                    if ((organizationIds == null) || (organizationIds.length == 0)) {
079                            return true;
080                    }
081    
082                    for (long organizationId : organizationIds) {
083                            check(permissionChecker, organizationId, actionId);
084                    }
085    
086                    return true;
087            }
088    
089            @Override
090            public boolean contains(
091                            PermissionChecker permissionChecker, Organization organization,
092                            String actionId)
093                    throws PortalException, SystemException {
094    
095                    Group group = organization.getGroup();
096    
097                    long groupId = group.getGroupId();
098    
099                    if (contains(permissionChecker, groupId, organization, actionId)) {
100                            return true;
101                    }
102    
103                    while (!organization.isRoot()) {
104                            Organization parentOrganization =
105                                    organization.getParentOrganization();
106    
107                            Group parentGroup = parentOrganization.getGroup();
108    
109                            groupId = parentGroup.getGroupId();
110    
111                            if (contains(
112                                            permissionChecker, groupId, parentOrganization,
113                                            ActionKeys.MANAGE_SUBORGANIZATIONS)) {
114    
115                                    return true;
116                            }
117    
118                            organization = parentOrganization;
119                    }
120    
121                    return false;
122            }
123    
124            protected boolean contains(
125                            PermissionChecker permissionChecker, long groupId,
126                            Organization organization, String actionId)
127                    throws PortalException, SystemException {
128    
129                    while ((organization != null) &&
130                               (organization.getOrganizationId() !=
131                                            OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID)) {
132    
133                            if (permissionChecker.hasPermission(
134                                            groupId, Organization.class.getName(),
135                                            organization.getOrganizationId(), actionId)) {
136    
137                                    return true;
138                            }
139    
140                            organization = organization.getParentOrganization();
141                    }
142    
143                    return false;
144            }
145    
146    }