1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.service.permission;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.model.Group;
20  import com.liferay.portal.model.Organization;
21  import com.liferay.portal.model.OrganizationConstants;
22  import com.liferay.portal.security.auth.PrincipalException;
23  import com.liferay.portal.security.permission.ActionKeys;
24  import com.liferay.portal.security.permission.PermissionChecker;
25  import com.liferay.portal.service.OrganizationLocalServiceUtil;
26  
27  /**
28   * <a href="OrganizationPermissionImpl.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Charles May
31   * @author Jorge Ferrer
32   */
33  public class OrganizationPermissionImpl implements OrganizationPermission {
34  
35      public void check(
36              PermissionChecker permissionChecker, long organizationId,
37              String actionId)
38          throws PortalException, SystemException {
39  
40          if (!contains(permissionChecker, organizationId, actionId)) {
41              throw new PrincipalException();
42          }
43      }
44  
45      public void check(
46              PermissionChecker permissionChecker, Organization organization,
47              String actionId)
48          throws PortalException, SystemException {
49  
50          if (!contains(permissionChecker, organization, actionId)) {
51              throw new PrincipalException();
52          }
53      }
54  
55      public boolean contains(
56              PermissionChecker permissionChecker, long organizationId,
57              String actionId)
58          throws PortalException, SystemException {
59  
60          if (organizationId > 0) {
61              Organization organization =
62                  OrganizationLocalServiceUtil.getOrganization(organizationId);
63  
64              return contains(permissionChecker, organization, actionId);
65          }
66          else {
67              return false;
68          }
69      }
70  
71      public boolean contains(
72              PermissionChecker permissionChecker, Organization organization,
73              String actionId)
74          throws PortalException, SystemException {
75  
76          Group group = organization.getGroup();
77  
78          long groupId = group.getGroupId();
79  
80          if (contains(permissionChecker, groupId, organization, actionId)) {
81              return true;
82          }
83  
84          while (!organization.isRoot()) {
85              Organization parentOrganization =
86                  organization.getParentOrganization();
87  
88              Group parentGroup = parentOrganization.getGroup();
89  
90              groupId = parentGroup.getGroupId();
91  
92              if (contains(
93                      permissionChecker, groupId, parentOrganization,
94                      ActionKeys.MANAGE_SUBORGANIZATIONS)) {
95  
96                  return true;
97              }
98  
99              organization = parentOrganization;
100         }
101 
102         return false;
103     }
104 
105     protected boolean contains(
106             PermissionChecker permissionChecker, long groupId,
107             Organization organization, String actionId)
108         throws PortalException, SystemException {
109 
110         while ((organization != null) &&
111                (organization.getOrganizationId() !=
112                     OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID)) {
113 
114             if (permissionChecker.hasPermission(
115                     groupId, Organization.class.getName(),
116                     organization.getOrganizationId(), actionId)) {
117 
118                 return true;
119             }
120 
121             organization = organization.getParentOrganization();
122         }
123 
124         return false;
125     }
126 
127 }