From 902a85fac0039c4360e6068b98b5964b0d6ffaeb Mon Sep 17 00:00:00 2001 From: Bright Date: Wed, 29 Jan 2025 17:56:05 +0700 Subject: [PATCH 1/2] fix issue #1073 --- src/controllers/UserController.ts | 144 +++++++++++++++--------------- 1 file changed, 72 insertions(+), 72 deletions(-) diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index c8557eba..070c41cd 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -82,78 +82,6 @@ export class KeycloakController extends Controller { return userDataWithRoles; } - @Post("user") - @Security("bearerAuth", ["system", "admin"]) - async createUser( - @Request() request: { user: { sub: string; preferred_username: string } }, - @Body() - body: { - username: string; - password: string; - firstName?: string; - lastName?: string; - email?: string; - roles: string[]; - profileId?: string; - }, - ) { - const checkUser = await getUserByUsername(body.username); - let userId: any = ""; - if (checkUser.length == 0) { - userId = await createUser(body.username, body.password, { - firstName: body.firstName, - lastName: body.lastName, - // email: body.email, - }); - if (typeof userId !== "string") { - throw new Error(userId.errorMessage); - } - } else { - userId = checkUser[0].id; - } - - 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.roles.includes(v.id)), - ); - - if (!result) { - throw new Error("Failed. Cannot set user's role."); - } - const profile = await this.profileRepo.findOne({ - where: { - id: body.profileId, - }, - }); - - if (profile) { - let _null: any = null; - if (typeof userId === "string") { - profile.keycloak = userId; - } - profile.email = body.email == null ? _null : body.email; - await this.profileRepo.save(profile); - if (body.roles != null && body.roles.length > 0) { - const roleKeycloak = await this.roleKeycloakRepo.find({ - where: { id: In(body.roles) }, - }); - const _profile = await this.profileRepo.findOne({ - where: { keycloak: userId }, - relations: ["roleKeycloaks"], - }); - if (_profile) { - _profile.roleKeycloaks = Array.from( - new Set([..._profile.roleKeycloaks, ...roleKeycloak]), - ); - this.profileRepo.save(_profile); - } - } - } - return userId; - } - @Post("user-emp") @Security("bearerAuth", ["system", "admin"]) async createUserEmployee( @@ -654,6 +582,78 @@ export class KeycloakController extends Controller { } } + @Post("user/emp") + @Security("bearerAuth", ["system", "admin"]) + async createUser( + @Request() request: { user: { sub: string; preferred_username: string } }, + @Body() + body: { + username: string; + password: string; + firstName?: string; + lastName?: string; + email?: string; + roles: string[]; + profileId?: string; + }, + ) { + const checkUser = await getUserByUsername(body.username); + let userId: any = ""; + if (checkUser.length == 0) { + userId = await createUser(body.username, body.password, { + firstName: body.firstName, + lastName: body.lastName, + // email: body.email, + }); + if (typeof userId !== "string") { + throw new Error(userId.errorMessage); + } + } else { + userId = checkUser[0].id; + } + + 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.roles.includes(v.id)), + ); + + if (!result) { + throw new Error("Failed. Cannot set user's role."); + } + const profile = await this.profileRepo.findOne({ + where: { + id: body.profileId, + }, + }); + + if (profile) { + let _null: any = null; + if (typeof userId === "string") { + profile.keycloak = userId; + } + profile.email = body.email == null ? _null : body.email; + await this.profileRepo.save(profile); + if (body.roles != null && body.roles.length > 0) { + const roleKeycloak = await this.roleKeycloakRepo.find({ + where: { id: In(body.roles) }, + }); + const _profile = await this.profileRepo.findOne({ + where: { keycloak: userId }, + relations: ["roleKeycloaks"], + }); + if (_profile) { + _profile.roleKeycloaks = Array.from( + new Set([..._profile.roleKeycloaks, ...roleKeycloak]), + ); + this.profileRepo.save(_profile); + } + } + } + return userId; + } + @Delete("group/{groupId}") async deleteGroup(@Path() groupId: string) { const result = await deleteGroup(groupId); From 278617b1ae6984ceb1c74db28ec0730bc6b473e6 Mon Sep 17 00:00:00 2001 From: Bright Date: Wed, 29 Jan 2025 18:02:50 +0700 Subject: [PATCH 2/2] no message --- src/controllers/UserController.ts | 82 +++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 5 deletions(-) diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index 070c41cd..cba039c9 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -82,6 +82,78 @@ export class KeycloakController extends Controller { return userDataWithRoles; } + @Post("user") + @Security("bearerAuth", ["system", "admin"]) + async createUser( + @Request() request: { user: { sub: string; preferred_username: string } }, + @Body() + body: { + username: string; + password: string; + firstName?: string; + lastName?: string; + email?: string; + roles: string[]; + profileId?: string; + }, + ) { + const checkUser = await getUserByUsername(body.username); + let userId: any = ""; + if (checkUser.length == 0) { + userId = await createUser(body.username, body.password, { + firstName: body.firstName, + lastName: body.lastName, + // email: body.email, + }); + if (typeof userId !== "string") { + throw new Error(userId.errorMessage); + } + } else { + userId = checkUser[0].id; + } + + 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.roles.includes(v.id)), + ); + + if (!result) { + throw new Error("Failed. Cannot set user's role."); + } + const profile = await this.profileRepo.findOne({ + where: { + id: body.profileId, + }, + }); + + if (profile) { + let _null: any = null; + if (typeof userId === "string") { + profile.keycloak = userId; + } + profile.email = body.email == null ? _null : body.email; + await this.profileRepo.save(profile); + if (body.roles != null && body.roles.length > 0) { + const roleKeycloak = await this.roleKeycloakRepo.find({ + where: { id: In(body.roles) }, + }); + const _profile = await this.profileRepo.findOne({ + where: { keycloak: userId }, + relations: ["roleKeycloaks"], + }); + if (_profile) { + _profile.roleKeycloaks = Array.from( + new Set([..._profile.roleKeycloaks, ...roleKeycloak]), + ); + this.profileRepo.save(_profile); + } + } + } + return userId; + } + @Post("user-emp") @Security("bearerAuth", ["system", "admin"]) async createUserEmployee( @@ -584,7 +656,7 @@ export class KeycloakController extends Controller { @Post("user/emp") @Security("bearerAuth", ["system", "admin"]) - async createUser( + async createUserEmp( @Request() request: { user: { sub: string; preferred_username: string } }, @Body() body: { @@ -622,7 +694,7 @@ export class KeycloakController extends Controller { if (!result) { throw new Error("Failed. Cannot set user's role."); } - const profile = await this.profileRepo.findOne({ + const profile = await this.profileEmpRepo.findOne({ where: { id: body.profileId, }, @@ -634,12 +706,12 @@ export class KeycloakController extends Controller { profile.keycloak = userId; } profile.email = body.email == null ? _null : body.email; - await this.profileRepo.save(profile); + await this.profileEmpRepo.save(profile); if (body.roles != null && body.roles.length > 0) { const roleKeycloak = await this.roleKeycloakRepo.find({ where: { id: In(body.roles) }, }); - const _profile = await this.profileRepo.findOne({ + const _profile = await this.profileEmpRepo.findOne({ where: { keycloak: userId }, relations: ["roleKeycloaks"], }); @@ -647,7 +719,7 @@ export class KeycloakController extends Controller { _profile.roleKeycloaks = Array.from( new Set([..._profile.roleKeycloaks, ...roleKeycloak]), ); - this.profileRepo.save(_profile); + this.profileEmpRepo.save(_profile); } } }