问题
Is there any method where I can get the RoleId using Role Name? I have created some custom roles on my portal, like "Project Manager", "Client" and "Delivery Head". Now I need to get the respective role of these custom roles programmatically using Role Name.
Any suggestions?
回答1:
You can use RoleLocalServiceUtil.getRole(companyId, name)
method to get the role object (instance of RoleModel
). If you need the id, call role.getRoleId()
.
Company id can be obtained by calling ThemeDisplay.getCompanyId()
.
回答2:
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;
}
来源:https://stackoverflow.com/questions/32472693/how-to-get-roleid-using-role-name-in-liferay