1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.NoSuchResourceException;
26  import com.liferay.portal.PortalException;
27  import com.liferay.portal.SystemException;
28  import com.liferay.portal.model.Group;
29  import com.liferay.portal.model.Layout;
30  import com.liferay.portal.model.LayoutConstants;
31  import com.liferay.portal.model.ResourceConstants;
32  import com.liferay.portal.security.auth.PrincipalException;
33  import com.liferay.portal.security.permission.ActionKeys;
34  import com.liferay.portal.security.permission.PermissionChecker;
35  import com.liferay.portal.service.GroupLocalServiceUtil;
36  import com.liferay.portal.service.LayoutLocalServiceUtil;
37  import com.liferay.portal.service.ResourceLocalServiceUtil;
38  import com.liferay.portal.util.PropsValues;
39  
40  /**
41   * <a href="LayoutPermissionImpl.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Charles May
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class LayoutPermissionImpl implements LayoutPermission {
48  
49      public void check(
50              PermissionChecker permissionChecker, long plid, String actionId)
51          throws PortalException, SystemException {
52  
53          if (!contains(permissionChecker, plid, actionId)) {
54              throw new PrincipalException();
55          }
56      }
57  
58      public void check(
59              PermissionChecker permissionChecker, long groupId,
60              boolean privateLayout, long layoutId, String actionId)
61          throws PortalException, SystemException {
62  
63          if (!contains(
64                  permissionChecker, groupId, privateLayout, layoutId,
65                  actionId)) {
66  
67              throw new PrincipalException();
68          }
69      }
70  
71      public void check(
72              PermissionChecker permissionChecker, Layout layout, String actionId)
73          throws PortalException, SystemException {
74  
75          if (!contains(permissionChecker, layout, actionId)) {
76              throw new PrincipalException();
77          }
78      }
79  
80      public boolean contains(
81              PermissionChecker permissionChecker, long plid, String actionId)
82          throws PortalException, SystemException {
83  
84          Layout layout = LayoutLocalServiceUtil.getLayout(plid);
85  
86          return contains(permissionChecker, layout, actionId);
87      }
88  
89      public boolean contains(
90              PermissionChecker permissionChecker, long groupId,
91              boolean privateLayout, long layoutId, String actionId)
92          throws PortalException, SystemException {
93  
94          if (layoutId == LayoutConstants.DEFAULT_PARENT_LAYOUT_ID) {
95              if (GroupPermissionUtil.contains(
96                      permissionChecker, groupId, ActionKeys.MANAGE_LAYOUTS)) {
97  
98                  return true;
99              }
100             else {
101                 return false;
102             }
103         }
104         else {
105             Layout layout = LayoutLocalServiceUtil.getLayout(
106                 groupId, privateLayout, layoutId);
107 
108             return contains(permissionChecker, layout, actionId);
109         }
110     }
111 
112     public boolean contains(
113             PermissionChecker permissionChecker, Layout layout, String actionId)
114         throws PortalException, SystemException {
115 
116         if ((layout.isPrivateLayout() &&
117              !PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_MODIFIABLE) ||
118             (layout.isPublicLayout() &&
119              !PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_MODIFIABLE)) {
120 
121             if (actionId.equals(ActionKeys.UPDATE)) {
122                 Group group = GroupLocalServiceUtil.getGroup(
123                     layout.getGroupId());
124 
125                 if (group.isUser()) {
126                     return false;
127                 }
128             }
129         }
130 
131         if (GroupPermissionUtil.contains(
132                 permissionChecker, layout.getGroupId(),
133                 ActionKeys.MANAGE_LAYOUTS)) {
134 
135             return true;
136         }
137 
138         try {
139             ResourceLocalServiceUtil.getResource(
140                 layout.getCompanyId(), Layout.class.getName(),
141                 ResourceConstants.SCOPE_INDIVIDUAL,
142                 String.valueOf(layout.getPlid()));
143         }
144         catch (NoSuchResourceException nsre) {
145             boolean addCommunityPermission = true;
146             boolean addGuestPermission = true;
147 
148             if (layout.isPrivateLayout()) {
149                 addGuestPermission = false;
150             }
151 
152             ResourceLocalServiceUtil.addResources(
153                 layout.getCompanyId(), layout.getGroupId(), 0,
154                 Layout.class.getName(), layout.getPlid(), false,
155                 addCommunityPermission, addGuestPermission);
156         }
157 
158         return permissionChecker.hasPermission(
159             layout.getGroupId(), Layout.class.getName(), layout.getPlid(),
160             actionId);
161     }
162 
163 }