1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.model.Group;
30  import com.liferay.portal.model.Layout;
31  import com.liferay.portal.model.Portlet;
32  import com.liferay.portal.model.PortletConstants;
33  import com.liferay.portal.security.auth.PrincipalException;
34  import com.liferay.portal.security.permission.ActionKeys;
35  import com.liferay.portal.security.permission.PermissionChecker;
36  import com.liferay.portal.security.permission.ResourceActionsUtil;
37  import com.liferay.portal.service.GroupLocalServiceUtil;
38  import com.liferay.portal.service.LayoutLocalServiceUtil;
39  import com.liferay.portal.util.PropsValues;
40  
41  import java.util.List;
42  
43  /**
44   * <a href="PortletPermissionImpl.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
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 = PortletConstants.getRootPortletId(portletId);
111             primKey = getPrimaryKey(plid, portletId);
112 
113             if ((layout.isPrivateLayout() &&
114                  !PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_MODIFIABLE) ||
115                 (layout.isPublicLayout() &&
116                  !PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_MODIFIABLE)) {
117 
118                 if (actionId.equals(ActionKeys.CONFIGURATION)) {
119                     Group group = GroupLocalServiceUtil.getGroup(
120                         layout.getGroupId());
121 
122                     if (group.isUser()) {
123                         return false;
124                     }
125                 }
126             }
127 
128             if (!strict) {
129                 if (LayoutPermissionUtil.contains(
130                         permissionChecker, groupId, layout.isPrivateLayout(),
131                         layout.getLayoutId(), ActionKeys.UPDATE) &&
132                     hasLayoutManagerPermission(portletId, actionId)) {
133 
134                     return true;
135                 }
136             }
137         }
138         else {
139             name = portletId;
140             primKey = portletId;
141         }
142 
143         return permissionChecker.hasPermission(
144             groupId, name, primKey, actionId);
145     }
146 
147     public boolean contains(
148             PermissionChecker permissionChecker, long plid, Portlet portlet,
149             String actionId)
150         throws PortalException, SystemException {
151 
152         return contains(
153             permissionChecker, plid, portlet, actionId, DEFAULT_STRICT);
154     }
155 
156     public boolean contains(
157             PermissionChecker permissionChecker, long plid, Portlet portlet,
158             String actionId, boolean strict)
159         throws PortalException, SystemException {
160 
161         boolean value = contains(
162             permissionChecker, plid, portlet.getPortletId(), actionId, strict);
163 
164         if (value) {
165             return true;
166         }
167         else {
168             if (portlet.isSystem() && actionId.equals(ActionKeys.VIEW)) {
169                 return true;
170             }
171             else {
172                 return false;
173             }
174         }
175     }
176 
177     public String getPrimaryKey(long plid, String portletId) {
178         StringBuilder sb = new StringBuilder();
179 
180         sb.append(plid);
181         sb.append(PortletConstants.LAYOUT_SEPARATOR);
182         sb.append(portletId);
183 
184         return sb.toString();
185     }
186 
187     public boolean hasLayoutManagerPermission(
188         String portletId, String actionId) {
189 
190         try {
191             return hasLayoutManagerPermissionImpl(portletId, actionId);
192         }
193         catch (Exception e) {
194             _log.error(e, e);
195 
196             return false;
197         }
198     }
199 
200     protected boolean hasLayoutManagerPermissionImpl(
201         String portletId, String actionId) {
202 
203         portletId = PortletConstants.getRootPortletId(portletId);
204 
205         List<String> layoutManagerActions =
206             ResourceActionsUtil.getPortletResourceLayoutManagerActions(
207                 portletId);
208 
209         return layoutManagerActions.contains(actionId);
210     }
211 
212     private static Log _log =
213         LogFactoryUtil.getLog(PortletPermissionImpl.class);
214 
215 }