将一个角色权限复制到另一个角色

时间:2015-06-24 09:23:09

标签: liferay liferay-6

我遇到过一种情况,我需要为多个角色分配相同的权限集(注意:角色的类型与常规 - 常规等相同)。

例如:我创建了三个常规角色“test_role_A”“test_role_B”“test_role_C”。这三个角色具有相同的权限。 所以我想知道我是否可以一次性为test_role_B和test_role_C分配test_role_A角色权限

Liferay中是否有任何配置可以这样做?

1 个答案:

答案 0 :(得分:1)

经过一些研究后,我发现 RoleLocalServiceUtil 的方法有reassignPermissionsmergePermissions,这些方法并不能完全满足我的要求。 但是,我发现了

  

ResourcePermissionLocalServiceUtil.addResourcePermissions(resourceName, roleName, scope, resourceActionBitwiseValue)

将有助于我的事业。 所以这里是 beanshell脚本,它将帮助您将一个角色权限复制到另一个相同类型的角色

import com.liferay.portal.util.PortalUtil;
import com.liferay.portal.service.ResourcePermissionLocalServiceUtil;
import com.liferay.portal.model.ResourcePermission;
import java.util.List;
import com.liferay.portal.service.RoleLocalServiceUtil;
import com.liferay.portal.model.Role;

long companyId = PortalUtil.getCompanyId(actionRequest);
Role fromRole = RoleLocalServiceUtil.getRole(companyId, "<fromRoleName>");
List resourcePermissionList = ResourcePermissionLocalServiceUtil.getRoleResourcePermissions(fromRole.getRoleId());
//array of role names to which permissions needs to be copied
String [] copyToRoles = new String [] {"<ToRoleName1>", "<ToRoleName2>"};
for(String copyToRoleStr: copyToRoles){
    try{
        Role copyToRole = RoleLocalServiceUtil.getRole(companyId, copyToRoleStr);
        try{
            for(int i=0;i< resourcePermissionList.size();i++){
                ResourcePermission rp = resourcePermissionList.get(i);
                ResourcePermissionLocalServiceUtil.addResourcePermissions(rp.getName(), copyToRole.getName(), rp.getScope(), rp.getActionIds());
            }
            out.println("Successfully Assigned permissions of "+fromRole.getName()+" to "+copyToRole.getName());
        }catch(Exception e){
            out.println("Error occured while adding resource permission against role - "+copyToRoleStr+" : "+e.getMessage());
        }
    }catch(Exception e){
        out.println("Error occured while fetching role - "+copyToRoleStr+" : "+e.getMessage());
    }
}