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.softwarecatalog.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.BaseModelPermissionChecker;
021    import com.liferay.portal.security.permission.PermissionChecker;
022    import com.liferay.portlet.softwarecatalog.model.SCProductEntry;
023    import com.liferay.portlet.softwarecatalog.service.SCProductEntryLocalServiceUtil;
024    
025    /**
026     * @author Jorge Ferrer
027     * @author Brian Wing Shun Chan
028     */
029    @OSGiBeanProperties(
030            property = {
031                    "model.class.name=" +
032                            "com.liferay.portlet.softwarecatalog.model.SCProductEntry"
033            }
034    )
035    public class SCProductEntryPermission implements BaseModelPermissionChecker {
036    
037            public static void check(
038                            PermissionChecker permissionChecker, long productEntryId,
039                            String actionId)
040                    throws PortalException {
041    
042                    if (!contains(permissionChecker, productEntryId, actionId)) {
043                            throw new PrincipalException.MustHavePermission(
044                                    permissionChecker, SCProductEntry.class.getName(),
045                                    productEntryId, actionId);
046                    }
047            }
048    
049            public static void check(
050                            PermissionChecker permissionChecker, SCProductEntry productEntry,
051                            String actionId)
052                    throws PortalException {
053    
054                    if (!contains(permissionChecker, productEntry, actionId)) {
055                            throw new PrincipalException.MustHavePermission(
056                                    permissionChecker, SCProductEntry.class.getName(),
057                                    productEntry.getProductEntryId(), actionId);
058                    }
059            }
060    
061            public static boolean contains(
062                            PermissionChecker permissionChecker, long productEntryId,
063                            String actionId)
064                    throws PortalException {
065    
066                    SCProductEntry productEntry =
067                            SCProductEntryLocalServiceUtil.getProductEntry(productEntryId);
068    
069                    return contains(permissionChecker, productEntry, actionId);
070            }
071    
072            public static boolean contains(
073                    PermissionChecker permissionChecker, SCProductEntry productEntry,
074                    String actionId) {
075    
076                    if (permissionChecker.hasOwnerPermission(
077                                    productEntry.getCompanyId(), SCProductEntry.class.getName(),
078                                    productEntry.getProductEntryId(), productEntry.getUserId(),
079                                    actionId)) {
080    
081                            return true;
082                    }
083    
084                    return permissionChecker.hasPermission(
085                            productEntry.getGroupId(), SCProductEntry.class.getName(),
086                            productEntry.getProductEntryId(), actionId);
087            }
088    
089            @Override
090            public void checkBaseModel(
091                            PermissionChecker permissionChecker, long groupId, long primaryKey,
092                            String actionId)
093                    throws PortalException {
094    
095                    check(permissionChecker, primaryKey, actionId);
096            }
097    
098    }