feat: get role list of user
This commit is contained in:
parent
57f56671b5
commit
4592192327
1 changed files with 39 additions and 0 deletions
|
|
@ -187,6 +187,45 @@ export async function getRoles(name?: string) {
|
|||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get roles list of user
|
||||
*
|
||||
* Client must have permission to get realms roles
|
||||
*
|
||||
* @returns role's info (array if not specify name) if success, null if not found, false otherwise.
|
||||
*/
|
||||
export async function getUserRoles(userId: string) {
|
||||
const res = await fetch(
|
||||
`${KC_URL}/admin/realms/${KC_REALM}/users/${userId}/role-mappings/realm`,
|
||||
{
|
||||
// prettier-ignore
|
||||
headers: {
|
||||
"authorization": `Bearer ${await getToken()}`,
|
||||
},
|
||||
},
|
||||
).catch((e) => console.log(e));
|
||||
|
||||
if (!res) return false;
|
||||
if (!res.ok && res.status !== 404) {
|
||||
return Boolean(console.error("Keycloak Error Response: ", await res.json()));
|
||||
}
|
||||
|
||||
if (res.status === 404) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const data = await res.json();
|
||||
|
||||
if (Array.isArray(data)) {
|
||||
return data.map((v: Record<string, string>) => ({ id: v.id, name: v.name }));
|
||||
}
|
||||
|
||||
return {
|
||||
id: data.id,
|
||||
name: data.name,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign role to user
|
||||
*
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue