001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.shopping.service.permission;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.security.auth.PrincipalException;
020    import com.liferay.portal.security.permission.ActionKeys;
021    import com.liferay.portal.security.permission.PermissionChecker;
022    import com.liferay.portal.util.PropsValues;
023    import com.liferay.portlet.shopping.model.ShoppingCategory;
024    import com.liferay.portlet.shopping.model.ShoppingCategoryConstants;
025    import com.liferay.portlet.shopping.model.ShoppingItem;
026    import com.liferay.portlet.shopping.service.ShoppingItemLocalServiceUtil;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class ShoppingItemPermission {
032    
033            public static void check(
034                            PermissionChecker permissionChecker, long itemId, String actionId)
035                    throws PortalException, SystemException {
036    
037                    if (!contains(permissionChecker, itemId, actionId)) {
038                            throw new PrincipalException();
039                    }
040            }
041    
042            public static void check(
043                            PermissionChecker permissionChecker, ShoppingItem item,
044                            String actionId)
045                    throws PortalException, SystemException {
046    
047                    if (!contains(permissionChecker, item, actionId)) {
048                            throw new PrincipalException();
049                    }
050            }
051    
052            public static boolean contains(
053                            PermissionChecker permissionChecker, long itemId, String actionId)
054                    throws PortalException, SystemException {
055    
056                    ShoppingItem item = ShoppingItemLocalServiceUtil.getItem(itemId);
057    
058                    return contains(permissionChecker, item, actionId);
059            }
060    
061            public static boolean contains(
062                            PermissionChecker permissionChecker, ShoppingItem item,
063                            String actionId)
064                    throws PortalException, SystemException {
065    
066                    if (actionId.equals(ActionKeys.VIEW) &&
067                            PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
068    
069                            if (item.getCategoryId() ==
070                                            ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
071    
072                                    if (!ShoppingPermission.contains(
073                                                    permissionChecker, item.getGroupId(), actionId)) {
074    
075                                            return false;
076                                    }
077                            }
078                            else {
079                                    ShoppingCategory category = item.getCategory();
080    
081                                    if (!ShoppingCategoryPermission.contains(
082                                                    permissionChecker, category, actionId)) {
083    
084                                            return false;
085                                    }
086                            }
087                    }
088    
089                    if (permissionChecker.hasOwnerPermission(
090                                    item.getCompanyId(), ShoppingItem.class.getName(),
091                                    item.getItemId(), item.getUserId(), actionId)) {
092    
093                            return true;
094                    }
095    
096                    return permissionChecker.hasPermission(
097                            item.getGroupId(), ShoppingItem.class.getName(), item.getItemId(),
098                            actionId);
099            }
100    
101    }