How to get RoleId using role name in liferay?

Is there any method in which I can get RoleId using the role name? I created some of my own roles on my portal, such as Project Manager, Client, and Delivery Manager. Now I need to get the appropriate role of these custom roles programmatically using the Role Name.

Any suggestions?

+1
source share
2 answers

You can use the RoleLocalServiceUtil.getRole(companyId, name) method to get the role object (instance of RoleModel ). If you need an identifier, call role.getRoleId() .

The company identifier can be obtained by calling ThemeDisplay.getCompanyId() .

+4
source
 public long getRoleIdByName(String roleName) throws Exception { if (roleName != null && !roleName.isEmpty()) { for (Role role : RoleLocalServiceUtil.getRoles(0, RoleLocalServiceUtil.getRolesCount())) { if (role.getName().equals(roleName)) { return role.getRoleId(); } } } return -1; } 
+1
source

Source: https://habr.com/ru/post/1238863/


All Articles