chore: restructure
This commit is contained in:
parent
88a7a4431a
commit
b5fb9d3408
4 changed files with 14 additions and 14 deletions
51
src/controllers/keycloak-controller.ts
Normal file
51
src/controllers/keycloak-controller.ts
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
import { Body, Controller, Delete, Get, Path, Post, Route, Security, Tags } from "tsoa";
|
||||
import { addUserRoles, createUser, getRoles, removeUserRoles } from "../services/keycloak";
|
||||
|
||||
@Route("api/keycloak")
|
||||
@Tags("Keycloak")
|
||||
@Security("keycloak")
|
||||
export class KeycloakController extends Controller {
|
||||
@Post("user")
|
||||
async createUser(
|
||||
@Body() body: { username: string; password: string; firstName?: string; lastName?: string },
|
||||
) {
|
||||
return await createUser(body.username, body.password, {
|
||||
firstName: body.firstName,
|
||||
lastName: body.lastName,
|
||||
});
|
||||
}
|
||||
|
||||
@Get("role")
|
||||
async getRole() {
|
||||
const role = await getRoles();
|
||||
if (Array.isArray(role)) return role;
|
||||
throw new Error("Failed. Cannot get role.");
|
||||
}
|
||||
|
||||
@Post("{userId}/role")
|
||||
async addRole(@Path() userId: string, @Body() body: { role: string[] }) {
|
||||
const list = await getRoles();
|
||||
|
||||
if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server.");
|
||||
|
||||
const result = await addUserRoles(
|
||||
userId,
|
||||
list.filter((v) => body.role.includes(v.id)),
|
||||
);
|
||||
|
||||
if (!result) throw new Error("Failed. Cannot set user's role.");
|
||||
}
|
||||
|
||||
@Delete("{userId}/role/{roleId}")
|
||||
async deleteRole(@Path() userId: string, @Path() roleId: string) {
|
||||
const list = await getRoles();
|
||||
|
||||
if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server.");
|
||||
|
||||
const result = await removeUserRoles(
|
||||
userId,
|
||||
list.filter((v) => roleId === v.id),
|
||||
);
|
||||
if (!result) throw new Error("Failed. Cannot remove user's role.");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue