如何在 Liferay 中为用户分配角色
How can I assign a role to a user in Liferay
有没有什么API方法可以使用角色名找到roleId?
我从 excel sheet 中获取角色名称,我需要检查角色名称是否存在。
如果角色存在
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();
}
}
以下代码可能对您有所帮助:
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);
有没有什么API方法可以使用角色名找到roleId? 我从 excel sheet 中获取角色名称,我需要检查角色名称是否存在。
如果角色存在
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();
}
}
以下代码可能对您有所帮助:
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);