เช็ค update role keycloak #802
This commit is contained in:
parent
2d2f35847a
commit
27f82f3652
1 changed files with 79 additions and 87 deletions
|
|
@ -80,9 +80,6 @@ export class KeycloakController extends Controller {
|
|||
|
||||
return userDataWithRoles;
|
||||
}
|
||||
// async getUser(@Path() id: string) {
|
||||
// return await getUser(id);
|
||||
// }
|
||||
|
||||
@Post("user")
|
||||
@Security("bearerAuth", ["system", "admin"])
|
||||
|
|
@ -106,70 +103,62 @@ export class KeycloakController extends Controller {
|
|||
});
|
||||
|
||||
if (typeof userId !== "string") {
|
||||
// throw new Error("ไม่สามารถติดต่อกับระบบจัดการผู้ใช้งานได้");
|
||||
throw new Error(userId.errorMessage);
|
||||
}
|
||||
else {
|
||||
const now = new Date().toISOString();
|
||||
const folderData: any = {
|
||||
pathname: stripLeadingSlash(`${body.username.trim()}/`),
|
||||
path: "",
|
||||
name: body.username.trim(),
|
||||
hidden: false,
|
||||
permissionGroup: [],
|
||||
permissionUser: [],
|
||||
permissionOther: {
|
||||
create: false,
|
||||
read: false,
|
||||
update: false,
|
||||
delete: false,
|
||||
perm: false,
|
||||
},
|
||||
favourite: false,
|
||||
color: "default",
|
||||
type: "folder",
|
||||
owner: body.username,
|
||||
ownerId: userId,
|
||||
createdAt: now,
|
||||
createdBy: request.user.preferred_username,
|
||||
createdByUserId: request.user.sub,
|
||||
updatedAt: now,
|
||||
updatedBy: request.user.preferred_username,
|
||||
updatedByUserId: request.user.sub,
|
||||
};
|
||||
|
||||
const now = new Date().toISOString();
|
||||
const folderData: any = {
|
||||
pathname: stripLeadingSlash(`${body.username.trim()}/`),
|
||||
path: "",
|
||||
name: body.username.trim(),
|
||||
hidden: false,
|
||||
permissionGroup: [],
|
||||
permissionUser: [],
|
||||
permissionOther: {
|
||||
create: false,
|
||||
read: false,
|
||||
update: false,
|
||||
delete: false,
|
||||
perm: false,
|
||||
},
|
||||
favourite: false,
|
||||
color: "default",
|
||||
type: "folder",
|
||||
owner: body.username,
|
||||
ownerId: userId,
|
||||
createdAt: now,
|
||||
createdBy: request.user.preferred_username,
|
||||
createdByUserId: request.user.sub,
|
||||
updatedAt: now,
|
||||
updatedBy: request.user.preferred_username,
|
||||
updatedByUserId: request.user.sub,
|
||||
};
|
||||
const profile = await this.profileRepo.findOne({
|
||||
where: {
|
||||
id: body.profileId,
|
||||
},
|
||||
});
|
||||
|
||||
// await elasticsearch.index({
|
||||
// index: DEFAULT_INDEX!,
|
||||
// document: folderData,
|
||||
// refresh: "wait_for",
|
||||
// });
|
||||
|
||||
// io.getInstance()?.emit("FolderCreate", folderData);
|
||||
|
||||
const profile = await this.profileRepo.findOne({
|
||||
where: {
|
||||
id: body.profileId,
|
||||
},
|
||||
});
|
||||
|
||||
if (profile) {
|
||||
let _null: any = null;
|
||||
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);
|
||||
if (profile) {
|
||||
let _null: any = null;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -249,38 +238,41 @@ export class KeycloakController extends Controller {
|
|||
|
||||
@Post("{userId}/role")
|
||||
async addRole(@Path() userId: string, @Body() body: { role: string[] }) {
|
||||
const roleKeycloak = await this.roleKeycloakRepo.find({
|
||||
where: { id: In(body.role) },
|
||||
});
|
||||
const profile = await this.profileRepo.findOne({
|
||||
where: { keycloak: userId },
|
||||
relations: ["roleKeycloaks"],
|
||||
});
|
||||
if (!profile) {
|
||||
const profileEmp = await this.profileEmpRepo.findOne({
|
||||
where: { keycloak: userId },
|
||||
relations: ["roleKeycloaks"],
|
||||
});
|
||||
if (!profileEmp) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||
profileEmp.roleKeycloaks = Array.from(
|
||||
new Set([...profileEmp.roleKeycloaks, ...roleKeycloak]),
|
||||
);
|
||||
this.profileEmpRepo.save(profileEmp);
|
||||
} else {
|
||||
profile.roleKeycloaks = Array.from(new Set([...profile.roleKeycloaks, ...roleKeycloak]));
|
||||
this.profileRepo.save(profile);
|
||||
}
|
||||
|
||||
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.");
|
||||
if (!result) {
|
||||
throw new Error("Failed. Cannot set user's role.");
|
||||
}
|
||||
else {
|
||||
const roleKeycloak = await this.roleKeycloakRepo.find({
|
||||
where: { id: In(body.role) },
|
||||
});
|
||||
const profile = await this.profileRepo.findOne({
|
||||
where: { keycloak: userId },
|
||||
relations: ["roleKeycloaks"],
|
||||
});
|
||||
if (!profile) {
|
||||
const profileEmp = await this.profileEmpRepo.findOne({
|
||||
where: { keycloak: userId },
|
||||
relations: ["roleKeycloaks"],
|
||||
});
|
||||
if (!profileEmp) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||
profileEmp.roleKeycloaks = Array.from(
|
||||
new Set([...profileEmp.roleKeycloaks, ...roleKeycloak]),
|
||||
);
|
||||
this.profileEmpRepo.save(profileEmp);
|
||||
} else {
|
||||
profile.roleKeycloaks = Array.from(new Set([...profile.roleKeycloaks, ...roleKeycloak]));
|
||||
this.profileRepo.save(profile);
|
||||
}
|
||||
}
|
||||
|
||||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue