How to get RoleId using Role Name in liferay?

安稳与你 提交于 2019-12-24 04:09:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!