add group from keycloak
All checks were successful
Spell Check / Spell Check with Typos (push) Successful in 7s
All checks were successful
Spell Check / Spell Check with Typos (push) Successful in 7s
This commit is contained in:
parent
3bf2446611
commit
94c7de89eb
3 changed files with 92 additions and 1 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
import { Body, Controller, Delete, Get, Path, Post, Route, Security, Tags } from "tsoa";
|
import { Body, Controller, Delete, Get, Path, Post, Route, Security, Tags } from "tsoa";
|
||||||
import { addUserRoles, listRole, removeUserRoles } from "../services/keycloak";
|
import { addUserRoles, getGroup, listRole, removeUserRoles } from "../services/keycloak";
|
||||||
|
|
||||||
@Route("api/v1/keycloak")
|
@Route("api/v1/keycloak")
|
||||||
@Tags("Single-Sign On")
|
@Tags("Single-Sign On")
|
||||||
|
|
@ -44,4 +44,12 @@ export class KeycloakController extends Controller {
|
||||||
);
|
);
|
||||||
if (!result) throw new Error("Failed. Cannot remove user's role.");
|
if (!result) throw new Error("Failed. Cannot remove user's role.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Get("group")
|
||||||
|
async getGroup() {
|
||||||
|
const group = await getGroup();
|
||||||
|
if (!Array.isArray(group)) throw new Error("Failed. Cannot get group(s) data from the server.");
|
||||||
|
|
||||||
|
return group;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@ import {
|
||||||
listRole,
|
listRole,
|
||||||
getUserRoles,
|
getUserRoles,
|
||||||
removeUserRoles,
|
removeUserRoles,
|
||||||
|
getGroupUser,
|
||||||
} from "../services/keycloak";
|
} from "../services/keycloak";
|
||||||
import { isSystem } from "../utils/keycloak";
|
import { isSystem } from "../utils/keycloak";
|
||||||
import {
|
import {
|
||||||
|
|
@ -947,3 +948,17 @@ export class UserSignatureController extends Controller {
|
||||||
await deleteFile(fileLocation.user.signature(userId));
|
await deleteFile(fileLocation.user.signature(userId));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Route("api/v1/user/{userId}/group")
|
||||||
|
@Tags("User")
|
||||||
|
@Security("keycloak")
|
||||||
|
export class UserGroupController extends Controller {
|
||||||
|
@Get()
|
||||||
|
async getUserGroup(@Path() userId: string) {
|
||||||
|
const groupUser = await getGroupUser(userId);
|
||||||
|
if (!Array.isArray(groupUser))
|
||||||
|
throw new Error("Failed. Cannot get user group(s) data from the server.");
|
||||||
|
|
||||||
|
return groupUser;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -346,6 +346,74 @@ export async function removeUserRoles(userId: string, roles: { id: string; name:
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getGroup() {
|
||||||
|
const res = await fetch(`${KC_URL}/admin/realms/${KC_REALM}/groups`, {
|
||||||
|
headers: {
|
||||||
|
authorization: `Bearer ${await getToken()}`,
|
||||||
|
"content-type": `application/json`,
|
||||||
|
},
|
||||||
|
method: "GET",
|
||||||
|
});
|
||||||
|
|
||||||
|
const dataMainGroup = await res.json();
|
||||||
|
|
||||||
|
const fetchSubGroups = async (group: any) => {
|
||||||
|
const resSub = await fetch(`${KC_URL}/admin/realms/${KC_REALM}/groups/${group.id}/children`, {
|
||||||
|
headers: {
|
||||||
|
authorization: `Bearer ${await getToken()}`,
|
||||||
|
"content-type": `application/json`,
|
||||||
|
},
|
||||||
|
method: "GET",
|
||||||
|
});
|
||||||
|
|
||||||
|
const dataSubGroup = await resSub.json();
|
||||||
|
let fullSubGroup = await Promise.all(
|
||||||
|
dataSubGroup.map(async (subGroupsData: any) => {
|
||||||
|
if (subGroupsData.subGroupCount > 0) {
|
||||||
|
return await fetchSubGroups(subGroupsData);
|
||||||
|
} else {
|
||||||
|
return {
|
||||||
|
id: subGroupsData.id,
|
||||||
|
name: subGroupsData.name,
|
||||||
|
path: subGroupsData.path,
|
||||||
|
subGroupCount: subGroupsData.subGroupCount,
|
||||||
|
subGroups: [],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
return {
|
||||||
|
id: group.id,
|
||||||
|
name: group.name,
|
||||||
|
path: group.path,
|
||||||
|
subGroupCount: group.subGroupCount,
|
||||||
|
subGroups: fullSubGroup,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const fullMainGroup = await Promise.all(dataMainGroup.map(fetchSubGroups));
|
||||||
|
return fullMainGroup;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getGroupUser(userId: string) {
|
||||||
|
const res = await fetch(`${KC_URL}/admin/realms/${KC_REALM}/users/${userId}/groups`, {
|
||||||
|
headers: {
|
||||||
|
authorization: `Bearer ${await getToken()}`,
|
||||||
|
"content-type": `application/json`,
|
||||||
|
},
|
||||||
|
method: "GET",
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await res.json();
|
||||||
|
return data.map((item: any) => {
|
||||||
|
return {
|
||||||
|
id: item.id,
|
||||||
|
name: item.name,
|
||||||
|
path: item.path,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
createUser,
|
createUser,
|
||||||
listRole,
|
listRole,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue