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.kernel.log.Log;
25  import com.liferay.portal.kernel.log.LogFactoryUtil;
26  import com.liferay.portal.model.Group;
27  import com.liferay.portal.model.Layout;
28  import com.liferay.portal.model.Portlet;
29  import com.liferay.portal.model.PortletConstants;
30  import com.liferay.portal.security.auth.PrincipalException;
31  import com.liferay.portal.security.permission.ActionKeys;
32  import com.liferay.portal.security.permission.PermissionChecker;
33  import com.liferay.portal.security.permission.ResourceActionsUtil;
34  import com.liferay.portal.service.GroupLocalServiceUtil;
35  import com.liferay.portal.service.LayoutLocalServiceUtil;
36  import com.liferay.portal.util.PropsValues;
37  
38  import java.util.List;
39  
40  /**
41   * <a href="PortletPermissionImpl.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class PortletPermissionImpl implements PortletPermission {
47  
48      public static final boolean DEFAULT_STRICT = false;
49  
50      public void check(
51              PermissionChecker permissionChecker, String portletId,
52              String actionId)
53          throws PortalException, SystemException {
54  
55          if (!contains(permissionChecker, portletId, actionId)) {
56              throw new PrincipalException();
57          }
58      }
59  
60      public void check(
61              PermissionChecker permissionChecker, long plid, String portletId,
62              String actionId)
63          throws PortalException, SystemException {
64  
65          check(permissionChecker, plid, portletId, actionId, DEFAULT_STRICT);
66      }
67  
68      public void check(
69              PermissionChecker permissionChecker, long plid, String portletId,
70              String actionId, boolean strict)
71          throws PortalException, SystemException {
72  
73          if (!contains(permissionChecker, plid, portletId, actionId, strict)) {
74              throw new PrincipalException();
75          }
76      }
77  
78      public boolean contains(
79              PermissionChecker permissionChecker, String portletId,
80              String actionId)
81          throws PortalException, SystemException {
82  
83          return contains(permissionChecker, 0, portletId, actionId);
84      }
85  
86      public boolean contains(
87              PermissionChecker permissionChecker, long plid, String portletId,
88              String actionId)
89          throws PortalException, SystemException {
90  
91          return contains(
92              permissionChecker, plid, portletId, actionId, DEFAULT_STRICT);
93      }
94  
95      public boolean contains(
96              PermissionChecker permissionChecker, long plid, String portletId,
97              String actionId, boolean strict)
98          throws PortalException, SystemException {
99  
100         long groupId = 0;
101         String name = null;
102         String primKey = null;
103 
104         if (plid > 0) {
105             Layout layout = LayoutLocalServiceUtil.getLayout(plid);
106 
107             groupId = layout.getGroupId();
108             name = PortletConstants.getRootPortletId(portletId);
109             primKey = getPrimaryKey(plid, portletId);
110 
111             if ((layout.isPrivateLayout() &&
112                  !PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_MODIFIABLE) ||
113                 (layout.isPublicLayout() &&
114                  !PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_MODIFIABLE)) {
115 
116                 if (actionId.equals(ActionKeys.CONFIGURATION)) {
117                     Group group = GroupLocalServiceUtil.getGroup(
118                         layout.getGroupId());
119 
120                     if (group.isUser()) {
121                         return false;
122                     }
123                 }
124             }
125 
126             if (!strict) {
127                 if (LayoutPermissionUtil.contains(
128                         permissionChecker, groupId, layout.isPrivateLayout(),
129                         layout.getLayoutId(), ActionKeys.UPDATE) &&
130                     hasLayoutManagerPermission(portletId, actionId)) {
131 
132                     return true;
133                 }
134             }
135         }
136         else {
137             name = portletId;
138             primKey = portletId;
139         }
140 
141         return permissionChecker.hasPermission(
142             groupId, name, primKey, actionId);
143     }
144 
145     public boolean contains(
146             PermissionChecker permissionChecker, long plid, Portlet portlet,
147             String actionId)
148         throws PortalException, SystemException {
149 
150         return contains(
151             permissionChecker, plid, portlet, actionId, DEFAULT_STRICT);
152     }
153 
154     public boolean contains(
155             PermissionChecker permissionChecker, long plid, Portlet portlet,
156             String actionId, boolean strict)
157         throws PortalException, SystemException {
158 
159         boolean value = contains(
160             permissionChecker, plid, portlet.getPortletId(), actionId, strict);
161 
162         if (value) {
163             return true;
164         }
165         else {
166             if (portlet.isSystem() && actionId.equals(ActionKeys.VIEW)) {
167                 return true;
168             }
169             else {
170                 return false;
171             }
172         }
173     }
174 
175     public String getPrimaryKey(long plid, String portletId) {
176         StringBuilder sb = new StringBuilder();
177 
178         sb.append(plid);
179         sb.append(PortletConstants.LAYOUT_SEPARATOR);
180         sb.append(portletId);
181 
182         return sb.toString();
183     }
184 
185     public boolean hasLayoutManagerPermission(
186         String portletId, String actionId) {
187 
188         try {
189             return hasLayoutManagerPermissionImpl(portletId, actionId);
190         }
191         catch (Exception e) {
192             _log.error(e, e);
193 
194             return false;
195         }
196     }
197 
198     protected boolean hasLayoutManagerPermissionImpl(
199         String portletId, String actionId) {
200 
201         portletId = PortletConstants.getRootPortletId(portletId);
202 
203         List<String> layoutManagerActions =
204             ResourceActionsUtil.getPortletResourceLayoutManagerActions(
205                 portletId);
206 
207         return layoutManagerActions.contains(actionId);
208     }
209 
210     private static Log _log =
211          LogFactoryUtil.getLog(PortletPermissionImpl.class);
212 
213 }