fix issue #1073
This commit is contained in:
parent
8eac85385c
commit
902a85fac0
1 changed files with 72 additions and 72 deletions
|
|
@ -82,78 +82,6 @@ export class KeycloakController extends Controller {
|
||||||
return userDataWithRoles;
|
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")
|
@Post("user-emp")
|
||||||
@Security("bearerAuth", ["system", "admin"])
|
@Security("bearerAuth", ["system", "admin"])
|
||||||
async createUserEmployee(
|
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}")
|
@Delete("group/{groupId}")
|
||||||
async deleteGroup(@Path() groupId: string) {
|
async deleteGroup(@Path() groupId: string) {
|
||||||
const result = await deleteGroup(groupId);
|
const result = await deleteGroup(groupId);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue