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