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.model.GroupConstants;
019    import com.liferay.portal.security.auth.PrincipalException;
020    import com.liferay.portal.security.permission.PermissionChecker;
021    import com.liferay.portlet.softwarecatalog.model.SCLicense;
022    import com.liferay.portlet.softwarecatalog.service.SCLicenseLocalServiceUtil;
023    
024    /**
025     * @author Jorge Ferrer
026     * @author Brian Wing Shun Chan
027     */
028    public class SCLicensePermission {
029    
030            public static void check(
031                            PermissionChecker permissionChecker, long productEntryId,
032                            String actionId)
033                    throws PortalException {
034    
035                    if (!contains(permissionChecker, productEntryId, actionId)) {
036                            throw new PrincipalException.MustHavePermission(
037                                    permissionChecker, SCLicense.class.getName(), productEntryId,
038                                    actionId);
039                    }
040            }
041    
042            public static void check(
043                            PermissionChecker permissionChecker, SCLicense license,
044                            String actionId)
045                    throws PortalException {
046    
047                    if (!contains(permissionChecker, license, actionId)) {
048                            throw new PrincipalException.MustHavePermission(
049                                    permissionChecker, SCLicense.class.getName(),
050                                    license.getLicenseId(), actionId);
051                    }
052            }
053    
054            public static boolean contains(
055                            PermissionChecker permissionChecker, long licenseId,
056                            String actionId)
057                    throws PortalException {
058    
059                    SCLicense license = SCLicenseLocalServiceUtil.getLicense(licenseId);
060    
061                    return contains(permissionChecker, license, actionId);
062            }
063    
064            public static boolean contains(
065                    PermissionChecker permissionChecker, SCLicense license,
066                    String actionId) {
067    
068                    return permissionChecker.hasPermission(
069                            GroupConstants.DEFAULT_PARENT_GROUP_ID, SCLicense.class.getName(),
070                            license.getLicenseId(), actionId);
071            }
072    
073    }