1   /**
2    * Copyright (c) 2000-2007 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.kernel.security.permission.ActionKeys;
29  import com.liferay.portal.kernel.security.permission.PermissionChecker;
30  import com.liferay.portal.model.Layout;
31  import com.liferay.portal.model.impl.LayoutImpl;
32  import com.liferay.portal.model.impl.ResourceImpl;
33  import com.liferay.portal.security.auth.PrincipalException;
34  import com.liferay.portal.service.LayoutLocalServiceUtil;
35  import com.liferay.portal.service.ResourceLocalServiceUtil;
36  
37  /**
38   * <a href="LayoutPermissionImpl.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Charles May
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class LayoutPermissionImpl implements LayoutPermission {
45  
46      public void check(
47              PermissionChecker permissionChecker, long plid, String actionId)
48          throws PortalException, SystemException {
49  
50          if (!contains(permissionChecker, plid, actionId)) {
51              throw new PrincipalException();
52          }
53      }
54  
55      public void check(
56              PermissionChecker permissionChecker, long groupId,
57              boolean privateLayout, long layoutId, String actionId)
58          throws PortalException, SystemException {
59  
60          if (!contains(
61                  permissionChecker, groupId, privateLayout, layoutId,
62                  actionId)) {
63  
64              throw new PrincipalException();
65          }
66      }
67  
68      public void check(
69              PermissionChecker permissionChecker, Layout layout, String actionId)
70          throws PortalException, SystemException {
71  
72          if (!contains(permissionChecker, layout, actionId)) {
73              throw new PrincipalException();
74          }
75      }
76  
77      public boolean contains(
78              PermissionChecker permissionChecker, long plid, String actionId)
79          throws PortalException, SystemException {
80  
81          Layout layout = LayoutLocalServiceUtil.getLayout(plid);
82  
83          return contains(permissionChecker, layout, actionId);
84      }
85  
86      public boolean contains(
87              PermissionChecker permissionChecker, long groupId,
88              boolean privateLayout, long layoutId, String actionId)
89          throws PortalException, SystemException {
90  
91          if (layoutId == LayoutImpl.DEFAULT_PARENT_LAYOUT_ID) {
92              if (GroupPermissionUtil.contains(
93                      permissionChecker, groupId, ActionKeys.MANAGE_LAYOUTS)) {
94  
95                  return true;
96              }
97              else {
98                  return false;
99              }
100         }
101         else {
102             Layout layout = LayoutLocalServiceUtil.getLayout(
103                 groupId, privateLayout, layoutId);
104 
105             return contains(permissionChecker, layout, actionId);
106         }
107     }
108 
109     public boolean contains(
110             PermissionChecker permissionChecker, Layout layout, String actionId)
111         throws PortalException, SystemException {
112 
113         if (GroupPermissionUtil.contains(
114                 permissionChecker, layout.getGroupId(),
115                 ActionKeys.MANAGE_LAYOUTS)) {
116 
117             return true;
118         }
119 
120         try {
121             ResourceLocalServiceUtil.getResource(
122                 layout.getCompanyId(), Layout.class.getName(),
123                 ResourceImpl.SCOPE_INDIVIDUAL,
124                 String.valueOf(layout.getPlid()));
125         }
126         catch (NoSuchResourceException nsre) {
127             boolean addCommunityPermission = true;
128             boolean addGuestPermission = true;
129 
130             if (layout.isPrivateLayout()) {
131                 addGuestPermission = false;
132             }
133 
134             ResourceLocalServiceUtil.addResources(
135                 layout.getCompanyId(), layout.getGroupId(), 0,
136                 Layout.class.getName(), layout.getPlid(), false,
137                 addCommunityPermission, addGuestPermission);
138         }
139 
140         return permissionChecker.hasPermission(
141             layout.getGroupId(), Layout.class.getName(), layout.getPlid(),
142             actionId);
143     }
144 
145 }