How to get RoleId using Role Name in liferay?
Asked Answered
I

2

3

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?

Inconsonant answered 9/9, 2015 at 6:50 Comment(0)
G
6

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().

Glorygloryofthesnow answered 9/9, 2015 at 7:15 Comment(0)
W
1
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;
}
Warlock answered 9/9, 2015 at 8:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.