如何在Liferay中为用户分配角色 [英] How can I assign a role to a user in Liferay

查看:96
本文介绍了如何在Liferay中为用户分配角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何API方法使用角色名称来查找roleId? 我从excel工作表中获取角色名称,我需要检查角色名称是否存在.

Is there any API method to find the roleId using role name? I am getting the role name(s) form an excel sheet, I need to check, whether the role name exists or not.

如果角色存在

如何将该角色分配给用户?

how can I assign that role to user?

如果角色不存在,

如何首先创建角色,然后将该角色分配给用户?

how can I create the role first and then assign that role to user?

我的代码,

if(role != null && !role.isEmpty()){
            Role currentRole=RoleLocalServiceUtil.getRole(companyId,role.trim());

            if(currentRole != null)
            {
                roleId = currentRole.getRoleId();
            }
            else{
                Role newRole = RoleServiceUtil.addRole(role.trim(), null, null, 0);
                roleId = newRole.getRoleId();
            }
        }

推荐答案

以下代码可能对您有帮助:

The following code might be helpful in you case:

String roleName = "role name";

// Get role by name
Role role = RoleLocalServiceUtil.getRole(companyId, roleName);

// If role doesn't exist, create new using roleName
if(role == null){
    role = RoleServiceUtil.addRole(roleName, null, null, 0);
}

// Get user by userId and add role to it
User user = UserLocalServiceUtil.getUserById(userId);
UserLocalServiceUtil.addRoleUser(role.getRoleId(), user.getUserId());
UserLocalServiceUtil.updateUser(user);

这篇关于如何在Liferay中为用户分配角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆