001    /**
002     * Copyright (c) 2000-present 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.spring.osgi.OSGiBeanProperties;
019    import com.liferay.portal.security.auth.PrincipalException;
020    import com.liferay.portal.security.permission.ActionKeys;
021    import com.liferay.portal.security.permission.BaseModelPermissionChecker;
022    import com.liferay.portal.security.permission.PermissionChecker;
023    import com.liferay.portal.util.PropsValues;
024    import com.liferay.portlet.shopping.model.ShoppingCategory;
025    import com.liferay.portlet.shopping.model.ShoppingCategoryConstants;
026    import com.liferay.portlet.shopping.model.ShoppingItem;
027    import com.liferay.portlet.shopping.service.ShoppingItemLocalServiceUtil;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    @OSGiBeanProperties(
033            property = {
034                    "model.class.name=com.liferay.portlet.shopping.model.ShoppingItem"
035            }
036    )
037    public class ShoppingItemPermission implements BaseModelPermissionChecker {
038    
039            public static void check(
040                            PermissionChecker permissionChecker, long itemId, String actionId)
041                    throws PortalException {
042    
043                    if (!contains(permissionChecker, itemId, actionId)) {
044                            throw new PrincipalException();
045                    }
046            }
047    
048            public static void check(
049                            PermissionChecker permissionChecker, ShoppingItem item,
050                            String actionId)
051                    throws PortalException {
052    
053                    if (!contains(permissionChecker, item, actionId)) {
054                            throw new PrincipalException();
055                    }
056            }
057    
058            public static boolean contains(
059                            PermissionChecker permissionChecker, long itemId, String actionId)
060                    throws PortalException {
061    
062                    ShoppingItem item = ShoppingItemLocalServiceUtil.getItem(itemId);
063    
064                    return contains(permissionChecker, item, actionId);
065            }
066    
067            public static boolean contains(
068                            PermissionChecker permissionChecker, ShoppingItem item,
069                            String actionId)
070                    throws PortalException {
071    
072                    if (actionId.equals(ActionKeys.VIEW) &&
073                            PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
074    
075                            if (item.getCategoryId() ==
076                                            ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
077    
078                                    if (!ShoppingPermission.contains(
079                                                    permissionChecker, item.getGroupId(), actionId)) {
080    
081                                            return false;
082                                    }
083                            }
084                            else {
085                                    ShoppingCategory category = item.getCategory();
086    
087                                    if (!ShoppingCategoryPermission.contains(
088                                                    permissionChecker, category, actionId)) {
089    
090                                            return false;
091                                    }
092                            }
093                    }
094    
095                    if (permissionChecker.hasOwnerPermission(
096                                    item.getCompanyId(), ShoppingItem.class.getName(),
097                                    item.getItemId(), item.getUserId(), actionId)) {
098    
099                            return true;
100                    }
101    
102                    return permissionChecker.hasPermission(
103                            item.getGroupId(), ShoppingItem.class.getName(), item.getItemId(),
104                            actionId);
105            }
106    
107            @Override
108            public void checkBaseModel(
109                            PermissionChecker permissionChecker, long groupId, long primaryKey,
110                            String actionId)
111                    throws PortalException {
112    
113                    check(permissionChecker, primaryKey, actionId);
114            }
115    
116    }