A more robust logic to parse user roles on setRoles
.
This commit is contained in:
parent
06566f504b
commit
bb0da62ea0
1 changed files with 32 additions and 10 deletions
|
@ -54,22 +54,44 @@ class User {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async setRoles(roles: (Role | RoleName | number | string)[]): Promise<void> {
|
public async setRoles(roles: (Role | RoleName | number | string)[]): Promise<void> {
|
||||||
const rolesToFetch = roles.map((role) => {
|
const inputRoleIds = new Set(
|
||||||
if (role instanceof Role) {
|
roles.filter((role) => {
|
||||||
return role.id || role.name;
|
role instanceof Role || typeof role === 'number'
|
||||||
}
|
}).map((role) => {
|
||||||
|
if (role instanceof Role) {
|
||||||
|
return role.id;
|
||||||
|
}
|
||||||
|
|
||||||
return role;
|
return role;
|
||||||
});
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
const inputRoleNames = new Set(
|
||||||
|
roles.filter((role) => {
|
||||||
|
typeof role === 'string'
|
||||||
|
}).map((role) => {
|
||||||
|
return role;
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!inputRoleIds.size && !inputRoleNames.size) {
|
||||||
|
return; // No roles to set
|
||||||
|
}
|
||||||
|
|
||||||
|
const query: Record<string, any> = {}
|
||||||
|
if (inputRoleIds.size) {
|
||||||
|
query['id'] = [...inputRoleIds];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (inputRoleNames.size) {
|
||||||
|
query['name'] = [...inputRoleNames];
|
||||||
|
}
|
||||||
|
|
||||||
const roleIds = new Set(
|
const roleIds = new Set(
|
||||||
(
|
(
|
||||||
await $db.Role().findAll({
|
await $db.Role().findAll({
|
||||||
where: {
|
where: {
|
||||||
[Op.or]: {
|
[Op.or]: query,
|
||||||
id: rolesToFetch,
|
|
||||||
name: rolesToFetch,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
).map((role) => (role as any).id)
|
).map((role) => (role as any).id)
|
||||||
|
|
Loading…
Add table
Reference in a new issue