Reputation: 1304
I have come across a scenario where I need to assign same set of permissions to multiple roles (Note: Roles are of same type like Regular-Regular etc).
For example: I have created three regular roles "test_role_A", "test_role_B" and "test_role_C". These three roles would have same permissions.
So I am wondering If I can assign test_role_A role permissions to test_role_B and test_role_C at one go.
Is there any configuration in Liferay to do so?
Upvotes: 1
Views: 652
Reputation: 1304
After some research I observed that RoleLocalServiceUtil has methods like reassignPermissions
, mergePermissions
which are not fully satisfying my requirement.
However, I found that
ResourcePermissionLocalServiceUtil.addResourcePermissions(resourceName, roleName, scope, resourceActionBitwiseValue)
would help my cause.
So here is the beanshell script which will help you copy one role permission to another role of same type.
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());
}
}
Upvotes: 1