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.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.security.permission.ActionKeys;
28  import com.liferay.portal.kernel.security.permission.PermissionChecker;
29  import com.liferay.portal.kernel.util.StringMaker;
30  import com.liferay.portal.model.Layout;
31  import com.liferay.portal.model.Portlet;
32  import com.liferay.portal.model.impl.PortletImpl;
33  import com.liferay.portal.security.auth.PrincipalException;
34  import com.liferay.portal.service.LayoutLocalServiceUtil;
35  
36  /**
37   * <a href="PortletPermissionImpl.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class PortletPermissionImpl implements PortletPermission {
43  
44      public void check(
45              PermissionChecker permissionChecker, String portletId,
46              String actionId)
47          throws PortalException, SystemException {
48  
49          if (!contains(permissionChecker, portletId, actionId)) {
50              throw new PrincipalException();
51          }
52      }
53  
54      public void check(
55              PermissionChecker permissionChecker, long plid, String portletId,
56              String actionId)
57          throws PortalException, SystemException {
58  
59          if (!contains(permissionChecker, plid, portletId, actionId)) {
60              throw new PrincipalException();
61          }
62      }
63  
64      public boolean contains(
65              PermissionChecker permissionChecker, String portletId,
66              String actionId)
67          throws PortalException, SystemException {
68  
69          return contains(permissionChecker, 0, portletId, actionId);
70      }
71  
72      public boolean contains(
73              PermissionChecker permissionChecker, long plid, String portletId,
74              String actionId)
75          throws PortalException, SystemException {
76  
77          long groupId = 0;
78          String name = null;
79          String primKey = null;
80  
81          if (plid > 0) {
82              Layout layout = LayoutLocalServiceUtil.getLayout(plid);
83  
84              groupId = layout.getGroupId();
85              name = PortletImpl.getRootPortletId(portletId);
86              primKey = getPrimaryKey(plid, portletId);
87  
88              if (LayoutPermissionUtil.contains(
89                      permissionChecker, groupId, layout.isPrivateLayout(),
90                      layout.getLayoutId(), ActionKeys.UPDATE)) {
91  
92                  return true;
93              }
94          }
95          else {
96              name = portletId;
97              primKey = portletId;
98          }
99  
100         return permissionChecker.hasPermission(
101             groupId, name, primKey, actionId);
102     }
103 
104     public boolean contains(
105             PermissionChecker permissionChecker, long plid, Portlet portlet,
106             String actionId)
107         throws PortalException, SystemException {
108 
109         boolean value = contains(
110             permissionChecker, plid, portlet.getPortletId(), actionId);
111 
112         if (value) {
113             return true;
114         }
115         else {
116             if (portlet.isSystem() && actionId.equals(ActionKeys.VIEW)) {
117                 return true;
118             }
119             else {
120                 return false;
121             }
122         }
123     }
124 
125     public String getPrimaryKey(long plid, String portletId) {
126         StringMaker sm = new StringMaker();
127 
128         sm.append(plid);
129         sm.append(PortletImpl.LAYOUT_SEPARATOR);
130         sm.append(portletId);
131 
132         return sm.toString();
133     }
134 
135 }