keycloak get role
This commit is contained in:
parent
2fb1f4f841
commit
27500667a3
2 changed files with 44 additions and 3 deletions
|
|
@ -27,6 +27,7 @@ import {
|
||||||
getUserList,
|
getUserList,
|
||||||
removeUserGroup,
|
removeUserGroup,
|
||||||
removeUserRoles,
|
removeUserRoles,
|
||||||
|
getRoleMappings,
|
||||||
} from "../keycloak";
|
} from "../keycloak";
|
||||||
// import * as io from "../lib/websocket";
|
// import * as io from "../lib/websocket";
|
||||||
// import elasticsearch from "../elasticsearch";
|
// import elasticsearch from "../elasticsearch";
|
||||||
|
|
@ -46,9 +47,25 @@ function stripLeadingSlash(str: string) {
|
||||||
@Security("bearerAuth")
|
@Security("bearerAuth")
|
||||||
export class KeycloakController extends Controller {
|
export class KeycloakController extends Controller {
|
||||||
@Get("user/{id}")
|
@Get("user/{id}")
|
||||||
async getUser(@Path() id: string) {
|
async getUser(@Path("id") id: string) {
|
||||||
return await getUser(id);
|
const userData = await getUser(id);
|
||||||
|
if (!userData) {
|
||||||
|
throw new Error("User not found");
|
||||||
|
}
|
||||||
|
const rolesData = await getRoleMappings(id);
|
||||||
|
if (!rolesData) {
|
||||||
|
throw new Error("Role mappings not found");
|
||||||
|
}
|
||||||
|
const userDataWithRoles = {
|
||||||
|
...userData,
|
||||||
|
roles: rolesData,
|
||||||
|
};
|
||||||
|
|
||||||
|
return userDataWithRoles;
|
||||||
}
|
}
|
||||||
|
// async getUser(@Path() id: string) {
|
||||||
|
// return await getUser(id);
|
||||||
|
// }
|
||||||
|
|
||||||
@Post("user")
|
@Post("user")
|
||||||
@Security("bearerAuth", ["system", "admin"])
|
@Security("bearerAuth", ["system", "admin"])
|
||||||
|
|
@ -135,7 +152,6 @@ export class KeycloakController extends Controller {
|
||||||
}
|
}
|
||||||
// @Security("bearerAuth", ["system", "admin"])
|
// @Security("bearerAuth", ["system", "admin"])
|
||||||
|
|
||||||
|
|
||||||
@Get("role")
|
@Get("role")
|
||||||
async getRole() {
|
async getRole() {
|
||||||
const role = await getRoles();
|
const role = await getRoles();
|
||||||
|
|
|
||||||
|
|
@ -206,6 +206,31 @@ export async function deleteUser(userId: string) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get keycloak user by uuid
|
||||||
|
*
|
||||||
|
* Client must have permission to manage realm's user
|
||||||
|
*
|
||||||
|
* @returns user if success, false otherwise.
|
||||||
|
*/
|
||||||
|
export async function getRoleMappings(userId: string) {
|
||||||
|
const res = await fetch(
|
||||||
|
`${KC_URL}/admin/realms/${KC_REALM}/users/${userId}/role-mappings/realm`,
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
authorization: `Bearer ${await getToken()}`,
|
||||||
|
"content-type": `application/json`,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
).catch((e) => console.log("Keycloak Error: ", e));
|
||||||
|
if (!res) return false;
|
||||||
|
if (!res.ok) {
|
||||||
|
return Boolean(console.error("Keycloak Error Response: ", await res.json()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return await res.json();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get roles list or specific role data
|
* Get roles list or specific role data
|
||||||
*
|
*
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue