1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service.permission;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.model.Group;
28  import com.liferay.portal.model.Organization;
29  import com.liferay.portal.model.OrganizationConstants;
30  import com.liferay.portal.security.auth.PrincipalException;
31  import com.liferay.portal.security.permission.ActionKeys;
32  import com.liferay.portal.security.permission.PermissionChecker;
33  import com.liferay.portal.service.OrganizationLocalServiceUtil;
34  
35  /**
36   * <a href="OrganizationPermissionImpl.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Charles May
39   * @author Jorge Ferrer
40   */
41  public class OrganizationPermissionImpl implements OrganizationPermission {
42  
43      public void check(
44              PermissionChecker permissionChecker, long organizationId,
45              String actionId)
46          throws PortalException, SystemException {
47  
48          if (!contains(permissionChecker, organizationId, actionId)) {
49              throw new PrincipalException();
50          }
51      }
52  
53      public void check(
54              PermissionChecker permissionChecker, Organization organization,
55              String actionId)
56          throws PortalException, SystemException {
57  
58          if (!contains(permissionChecker, organization, actionId)) {
59              throw new PrincipalException();
60          }
61      }
62  
63      public boolean contains(
64              PermissionChecker permissionChecker, long organizationId,
65              String actionId)
66          throws PortalException, SystemException {
67  
68          if (organizationId > 0) {
69              Organization organization =
70                  OrganizationLocalServiceUtil.getOrganization(organizationId);
71  
72              return contains(permissionChecker, organization, actionId);
73          }
74          else {
75              return false;
76          }
77      }
78  
79      public boolean contains(
80              PermissionChecker permissionChecker, Organization organization,
81              String actionId)
82          throws PortalException, SystemException {
83  
84          Group group = organization.getGroup();
85  
86          long groupId = group.getGroupId();
87  
88          if (contains(permissionChecker, groupId, organization, actionId)) {
89              return true;
90          }
91  
92          while (!organization.isRoot()) {
93              Organization parentOrganization =
94                  organization.getParentOrganization();
95  
96              Group parentGroup = parentOrganization.getGroup();
97  
98              groupId = parentGroup.getGroupId();
99  
100             if (contains(
101                     permissionChecker, groupId, parentOrganization,
102                     ActionKeys.MANAGE_SUBORGANIZATIONS)) {
103 
104                 return true;
105             }
106 
107             organization = parentOrganization;
108         }
109 
110         return false;
111     }
112 
113     protected boolean contains(
114             PermissionChecker permissionChecker, long groupId,
115             Organization organization, String actionId)
116         throws PortalException, SystemException {
117 
118         while ((organization != null) &&
119                (organization.getOrganizationId() !=
120                     OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID)) {
121 
122             if (permissionChecker.hasPermission(
123                     groupId, Organization.class.getName(),
124                     organization.getOrganizationId(), actionId)) {
125 
126                 return true;
127             }
128 
129             organization = organization.getParentOrganization();
130         }
131 
132         return false;
133     }
134 
135 }