001    /**
002     * Copyright (c) 2000-present 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.util.ArrayUtil;
019    import com.liferay.portal.model.Organization;
020    import com.liferay.portal.model.OrganizationConstants;
021    import com.liferay.portal.security.auth.PrincipalException;
022    import com.liferay.portal.security.permission.ActionKeys;
023    import com.liferay.portal.security.permission.PermissionChecker;
024    import com.liferay.portal.service.OrganizationLocalServiceUtil;
025    
026    /**
027     * @author Charles May
028     * @author Jorge Ferrer
029     * @author Sergio Gonz??lez
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 {
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 {
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 {
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 {
077    
078                    if (ArrayUtil.isEmpty(organizationIds)) {
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 {
094    
095                    long groupId = organization.getGroupId();
096    
097                    if (contains(permissionChecker, groupId, organization, actionId)) {
098                            return true;
099                    }
100    
101                    while (!organization.isRoot()) {
102                            Organization parentOrganization =
103                                    organization.getParentOrganization();
104    
105                            groupId = parentOrganization.getGroupId();
106    
107                            if (contains(
108                                            permissionChecker, groupId, parentOrganization,
109                                            ActionKeys.MANAGE_SUBORGANIZATIONS)) {
110    
111                                    return true;
112                            }
113    
114                            organization = parentOrganization;
115                    }
116    
117                    return false;
118            }
119    
120            protected boolean contains(
121                            PermissionChecker permissionChecker, long groupId,
122                            Organization organization, String actionId)
123                    throws PortalException {
124    
125                    while ((organization != null) &&
126                               (organization.getOrganizationId() !=
127                                       OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID)) {
128    
129                            if (actionId.equals(ActionKeys.ADD_ORGANIZATION) &&
130                                    permissionChecker.hasPermission(
131                                            groupId, Organization.class.getName(),
132                                            organization.getOrganizationId(),
133                                            ActionKeys.MANAGE_SUBORGANIZATIONS) ||
134                                    PortalPermissionUtil.contains(
135                                            permissionChecker, ActionKeys.ADD_ORGANIZATION)) {
136    
137                                    return true;
138                            }
139                            else if (permissionChecker.hasPermission(
140                                                    groupId, Organization.class.getName(),
141                                                    organization.getOrganizationId(), actionId)) {
142    
143                                    return true;
144                            }
145    
146                            organization = organization.getParentOrganization();
147                    }
148    
149                    return false;
150            }
151    
152    }