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 boolean contains(
52              PermissionChecker permissionChecker, long organizationId,
53              String actionId)
54          throws PortalException, SystemException {
55  
56          Organization organization = null;
57  
58          long groupId = 0;
59  
60          if (organizationId > 0) {
61              organization = OrganizationLocalServiceUtil.getOrganization(
62                  organizationId);
63  
64              Group group = organization.getGroup();
65  
66              groupId = group.getGroupId();
67          }
68  
69          if (contains(permissionChecker, groupId, organizationId, actionId)) {
70              return true;
71          }
72  
73          if ((!actionId.equals(ActionKeys.MANAGE_SUBORGANIZATIONS)) &&
74              (organization != null)) {
75  
76              if (contains(
77                      permissionChecker, groupId,
78                      organization.getParentOrganizationId(),
79                      ActionKeys.MANAGE_SUBORGANIZATIONS)) {
80  
81                  return true;
82              }
83          }
84  
85          return false;
86      }
87  
88      protected boolean contains(
89              PermissionChecker permissionChecker, long groupId,
90              long organizationId, String actionId)
91          throws PortalException, SystemException {
92  
93          while (organizationId !=
94                      OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
95  
96              if (permissionChecker.hasPermission(
97                      groupId, Organization.class.getName(), organizationId,
98                      actionId)) {
99  
100                 return true;
101             }
102 
103             Organization organization =
104                 OrganizationLocalServiceUtil.getOrganization(organizationId);
105 
106             organizationId = organization.getParentOrganizationId();
107         }
108 
109         return false;
110     }
111 
112 }