1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portal.service.permission;
16  
17  import com.liferay.portal.NoSuchResourceException;
18  import com.liferay.portal.PortalException;
19  import com.liferay.portal.SystemException;
20  import com.liferay.portal.model.Group;
21  import com.liferay.portal.model.Layout;
22  import com.liferay.portal.model.LayoutConstants;
23  import com.liferay.portal.model.ResourceConstants;
24  import com.liferay.portal.security.auth.PrincipalException;
25  import com.liferay.portal.security.permission.ActionKeys;
26  import com.liferay.portal.security.permission.PermissionChecker;
27  import com.liferay.portal.service.GroupLocalServiceUtil;
28  import com.liferay.portal.service.LayoutLocalServiceUtil;
29  import com.liferay.portal.service.ResourceLocalServiceUtil;
30  import com.liferay.portal.service.ResourcePermissionLocalServiceUtil;
31  import com.liferay.portal.util.PropsValues;
32  
33  /**
34   * <a href="LayoutPermissionImpl.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Charles May
37   * @author Brian Wing Shun Chan
38   */
39  public class LayoutPermissionImpl implements LayoutPermission {
40  
41      public void check(
42              PermissionChecker permissionChecker, Layout layout, String actionId)
43          throws PortalException, SystemException {
44  
45          if (!contains(permissionChecker, layout, actionId)) {
46              throw new PrincipalException();
47          }
48      }
49  
50      public void check(
51              PermissionChecker permissionChecker, long groupId,
52              boolean privateLayout, long layoutId, String actionId)
53          throws PortalException, SystemException {
54  
55          if (!contains(
56                  permissionChecker, groupId, privateLayout, layoutId,
57                  actionId)) {
58  
59              throw new PrincipalException();
60          }
61      }
62  
63      public void check(
64              PermissionChecker permissionChecker, long plid, String actionId)
65          throws PortalException, SystemException {
66  
67          if (!contains(permissionChecker, plid, actionId)) {
68              throw new PrincipalException();
69          }
70      }
71  
72      public boolean contains(
73              PermissionChecker permissionChecker, Layout layout, String actionId)
74          throws PortalException, SystemException {
75  
76          if ((layout.isPrivateLayout() &&
77               !PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_MODIFIABLE) ||
78              (layout.isPublicLayout() &&
79               !PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_MODIFIABLE)) {
80  
81              if (actionId.equals(ActionKeys.UPDATE)) {
82                  Group group = GroupLocalServiceUtil.getGroup(
83                      layout.getGroupId());
84  
85                  if (group.isUser()) {
86                      return false;
87                  }
88              }
89          }
90  
91          if (GroupPermissionUtil.contains(
92                  permissionChecker, layout.getGroupId(),
93                  ActionKeys.MANAGE_LAYOUTS)) {
94  
95              return true;
96          }
97  
98          try {
99              if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
100                 if (ResourcePermissionLocalServiceUtil.
101                         getResourcePermissionsCount(
102                             layout.getCompanyId(), Layout.class.getName(),
103                             ResourceConstants.SCOPE_INDIVIDUAL,
104                             String.valueOf(layout.getPlid())) == 0) {
105 
106                     throw new NoSuchResourceException();
107                 }
108             }
109             else {
110                 ResourceLocalServiceUtil.getResource(
111                     layout.getCompanyId(), Layout.class.getName(),
112                     ResourceConstants.SCOPE_INDIVIDUAL,
113                     String.valueOf(layout.getPlid()));
114             }
115         }
116         catch (NoSuchResourceException nsre) {
117             boolean addCommunityPermission = true;
118             boolean addGuestPermission = true;
119 
120             if (layout.isPrivateLayout()) {
121                 addGuestPermission = false;
122             }
123 
124             ResourceLocalServiceUtil.addResources(
125                 layout.getCompanyId(), layout.getGroupId(), 0,
126                 Layout.class.getName(), layout.getPlid(), false,
127                 addCommunityPermission, addGuestPermission);
128         }
129 
130         return permissionChecker.hasPermission(
131             layout.getGroupId(), Layout.class.getName(), layout.getPlid(),
132             actionId);
133     }
134 
135     public boolean contains(
136             PermissionChecker permissionChecker, long groupId,
137             boolean privateLayout, long layoutId, String actionId)
138         throws PortalException, SystemException {
139 
140         if (layoutId == LayoutConstants.DEFAULT_PARENT_LAYOUT_ID) {
141             if (GroupPermissionUtil.contains(
142                     permissionChecker, groupId, ActionKeys.MANAGE_LAYOUTS)) {
143 
144                 return true;
145             }
146             else {
147                 return false;
148             }
149         }
150         else {
151             Layout layout = LayoutLocalServiceUtil.getLayout(
152                 groupId, privateLayout, layoutId);
153 
154             return contains(permissionChecker, layout, actionId);
155         }
156     }
157 
158     public boolean contains(
159             PermissionChecker permissionChecker, long plid, String actionId)
160         throws PortalException, SystemException {
161 
162         Layout layout = LayoutLocalServiceUtil.getLayout(plid);
163 
164         return contains(permissionChecker, layout, actionId);
165     }
166 
167 }