【发布时间】:2020-12-05 16:55:39
【问题描述】:
我一直在研究 keycloak admin rest api 方法,并且我得到了大部分方法。我能够使用在 nodejs 中读取的 excel 表创建用户和创建角色,并且效果很好。我能够列出所有用户并列出所有创建的角色。现在我要做的是检查角色是否存在于使用逗号分隔的 excel 文件中,然后只为该用户分配 excel 文件中的角色方法
我将分享我的代码
函数读取用户和角色
let userRoleTobCreated = ReadFromExcel('./uploads/employees-roles.xlsx');
function ReadFromExcel() {
let userRoleTobCreated = []
xlsxFile(filename).then((rows) => {
rows.forEach(row => {
let userObject = {
username: row[0],
lastName: row[1],
firstName: row[2],
email: row[3],
roles: row[4].split(",")
}
userRoleTobCreated.push(userObject);
});
})
return userRoleTobCreated;
}
输出
在这里,我想检查员工的 excel 文件,并根据列中的角色,我想为每个用户分配每个角色,但它不起作用,它只为前四个用户分配四个角色
函数调用
GetUser(userid, kc_accessToken).then((resp) => {
userid = resp.data.map(userRoleTobCreated => userRoleTobCreated.id);
GetRole(roleId, roleName, kc_accessToken).then((resp) => {
roleId = resp.data.map(userRoleTobCreated => userRoleTobCreated.id);
roleName = resp.data.map(userRoleTobCreated => userRoleTobCreated.name);
// Call Api to loop through each customer and assign each role to each customer
// this method just loops through the four role and assign it to the first four users only
for (var i = 0; i < userid.length; i++) {
AssignRole(userid[i], roleId[i], roleName[i], kc_clientUUID, kc_accessToken).then((resp) => {
})
}
})
})
输出 - 每个用户的角色不是指定 excel 文件的方式
【问题讨论】:
标签: javascript node.js arrays loops keycloak