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.portlet.shopping.service.permission;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.security.auth.PrincipalException;
25  import com.liferay.portal.security.permission.ActionKeys;
26  import com.liferay.portal.security.permission.PermissionChecker;
27  import com.liferay.portal.service.permission.PortletPermissionUtil;
28  import com.liferay.portal.util.PortletKeys;
29  import com.liferay.portlet.shopping.model.ShoppingCategory;
30  import com.liferay.portlet.shopping.model.impl.ShoppingCategoryImpl;
31  import com.liferay.portlet.shopping.service.ShoppingCategoryLocalServiceUtil;
32  
33  /**
34   * <a href="ShoppingCategoryPermission.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   *
38   */
39  public class ShoppingCategoryPermission {
40  
41      public static void check(
42              PermissionChecker permissionChecker, long plid, long categoryId,
43              String actionId)
44          throws PortalException, SystemException {
45  
46          if (!contains(permissionChecker, plid, categoryId, actionId)) {
47              throw new PrincipalException();
48          }
49      }
50  
51      public static void check(
52              PermissionChecker permissionChecker, long categoryId,
53              String actionId)
54          throws PortalException, SystemException {
55  
56          if (!contains(permissionChecker, categoryId, actionId)) {
57              throw new PrincipalException();
58          }
59      }
60  
61      public static void check(
62              PermissionChecker permissionChecker, ShoppingCategory category,
63              String actionId)
64          throws PortalException, SystemException {
65  
66          if (!contains(permissionChecker, category, actionId)) {
67              throw new PrincipalException();
68          }
69      }
70  
71      public static boolean contains(
72              PermissionChecker permissionChecker, long plid, long categoryId,
73              String actionId)
74          throws PortalException, SystemException {
75  
76          if (categoryId == ShoppingCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
77              return PortletPermissionUtil.contains(
78                  permissionChecker, plid, PortletKeys.SHOPPING, actionId);
79          }
80          else {
81              return contains(permissionChecker, categoryId, actionId);
82          }
83      }
84  
85      public static boolean contains(
86              PermissionChecker permissionChecker, long categoryId,
87              String actionId)
88          throws PortalException, SystemException {
89  
90          ShoppingCategory category =
91              ShoppingCategoryLocalServiceUtil.getCategory(categoryId);
92  
93          return contains(permissionChecker, category, actionId);
94      }
95  
96      public static boolean contains(
97              PermissionChecker permissionChecker, ShoppingCategory category,
98              String actionId)
99          throws PortalException, SystemException {
100 
101         if (actionId.equals(ActionKeys.ADD_CATEGORY)) {
102             actionId = ActionKeys.ADD_SUBCATEGORY;
103         }
104 
105         long categoryId = category.getCategoryId();
106 
107         while (categoryId != ShoppingCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
108             if (permissionChecker.hasOwnerPermission(
109                     category.getCompanyId(), ShoppingCategory.class.getName(),
110                     category.getCategoryId(), category.getUserId(), actionId)) {
111 
112                 return true;
113             }
114 
115             if (permissionChecker.hasPermission(
116                     category.getGroupId(), ShoppingCategory.class.getName(),
117                     category.getCategoryId(), actionId)) {
118 
119                 return true;
120             }
121 
122             if (actionId.equals(ActionKeys.VIEW)) {
123                 break;
124             }
125 
126             category = ShoppingCategoryLocalServiceUtil.getCategory(categoryId);
127 
128             categoryId = category.getParentCategoryId();
129         }
130 
131         return false;
132     }
133 
134 }