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