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