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