Roles and Permissions

Necessary classes:

com.liferay.portal.service.UserGroupRoleLocalServiceUtil
com.liferay.portal.service.RoleLocalServiceUtil
Add Site-Role to user:
UserGroupRoleLocalServiceUtil.
addUserGroupRoles(userId, groupId, roleIds);
Get all users with a specific site-role:
List userIds = new ArrayList<>();
Role role = 
RoleLocalServiceUtil.getRole(long companyId, String roleName);

List userGroupRoles = 
UserGroupRoleLocalServiceUtil.getUserGroupRolesByGroupAndRole(long groupId, long roleId);

for(UserGroupRole u:userGroupRoles){
userIds.add(u.getUserId());
}
Check if user has regular role:
RoleLocalServiceUtil.hasUserRole(long userId, long roleId);
Check if user has site role:
UserGroupRoleLocalServiceUtil.hasUserGroupRole(long userId, long groupId, String roleName);

Leave a Reply