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