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.rolesadmin.lar;
016    
017    import com.liferay.portal.model.Group;
018    import com.liferay.portal.model.ResourceConstants;
019    import com.liferay.portal.model.ResourcePermission;
020    import com.liferay.portal.model.ResourceTypePermission;
021    import com.liferay.portal.model.Role;
022    import com.liferay.portal.model.RoleConstants;
023    import com.liferay.portal.security.permission.PermissionConversionFilter;
024    import com.liferay.portal.service.GroupLocalServiceUtil;
025    
026    /**
027     * @author Michael C. Han
028     */
029    public class ImportExportPermissionConversionFilter
030            implements PermissionConversionFilter {
031    
032            @Override
033            public boolean accept(Role role, ResourcePermission resourcePermission) {
034                    int scope = resourcePermission.getScope();
035    
036                    if ((scope == ResourceConstants.SCOPE_COMPANY) ||
037                            (scope == ResourceConstants.SCOPE_GROUP_TEMPLATE)) {
038    
039                            return true;
040                    }
041                    else if (resourcePermission.getScope() ==
042                                            ResourceConstants.SCOPE_GROUP) {
043    
044                            Group group = GroupLocalServiceUtil.fetchGroup(
045                                    Long.valueOf(resourcePermission.getPrimKey()));
046    
047                            if (group.isCompany() || group.isUserPersonalSite()) {
048                                    return true;
049                            }
050                    }
051    
052                    return false;
053            }
054    
055            @Override
056            public boolean accept(
057                    Role role, ResourceTypePermission resourceTypePermission) {
058    
059                    if (role.getType() != RoleConstants.TYPE_REGULAR) {
060                            return true;
061                    }
062                    else if (resourceTypePermission.isCompanyScope()) {
063                            return true;
064                    }
065    
066                    Group group = GroupLocalServiceUtil.fetchGroup(
067                            resourceTypePermission.getGroupId());
068    
069                    if ((group != null) &&
070                            (group.isCompany() || group.isUserPersonalSite())) {
071    
072                            return true;
073                    }
074    
075                    return false;
076            }
077    
078    }