This commit is contained in:
Bright 2024-11-21 17:05:01 +07:00
parent 27f82f3652
commit 9ed7447095

View file

@ -99,66 +99,67 @@ export class KeycloakController extends Controller {
const userId = await createUser(body.username, body.password, { const userId = await createUser(body.username, body.password, {
firstName: body.firstName, firstName: body.firstName,
lastName: body.lastName, lastName: body.lastName,
email: body.email, // email: body.email,
}); });
if (typeof userId !== "string") { if (typeof userId !== "string") {
throw new Error(userId.errorMessage); 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 profile = await this.profileRepo.findOne({ const now = new Date().toISOString();
where: { const folderData: any = {
id: body.profileId, 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,
};
if (profile) { const profile = await this.profileRepo.findOne({
let _null: any = null; where: {
id: body.profileId,
},
});
if (profile) {
let _null: any = null;
if (typeof userId === "string") {
profile.keycloak = userId; profile.keycloak = userId;
profile.email = body.email == null ? _null : body.email; }
await this.profileRepo.save(profile); profile.email = body.email == null ? _null : body.email;
if (body.roles != null && body.roles.length > 0) { await this.profileRepo.save(profile);
const roleKeycloak = await this.roleKeycloakRepo.find({ if (body.roles != null && body.roles.length > 0) {
where: { id: In(body.roles) }, const roleKeycloak = await this.roleKeycloakRepo.find({
}); where: { id: In(body.roles) },
const _profile = await this.profileRepo.findOne({ });
where: { keycloak: userId }, const _profile = await this.profileRepo.findOne({
relations: ["roleKeycloaks"], where: { keycloak: userId },
}); relations: ["roleKeycloaks"],
if (_profile) { });
_profile.roleKeycloaks = Array.from( if (_profile) {
new Set([..._profile.roleKeycloaks, ...roleKeycloak]), _profile.roleKeycloaks = Array.from(
); new Set([..._profile.roleKeycloaks, ...roleKeycloak]),
this.profileRepo.save(_profile); );
} this.profileRepo.save(_profile);
} }
} }
} }
@ -249,28 +250,30 @@ export class KeycloakController extends Controller {
if (!result) { if (!result) {
throw new Error("Failed. Cannot set user's role."); throw new Error("Failed. Cannot set user's role.");
} }
else {
const roleKeycloak = await this.roleKeycloakRepo.find({ const roleKeycloak = await this.roleKeycloakRepo.find({
where: { id: In(body.role) }, where: { id: In(body.role) },
}); });
const profile = await this.profileRepo.findOne({ const profile = await this.profileRepo.findOne({
where: { keycloak: userId },
relations: ["roleKeycloaks"],
});
if (!profile) {
const profileEmp = await this.profileEmpRepo.findOne({
where: { keycloak: userId }, where: { keycloak: userId },
relations: ["roleKeycloaks"], relations: ["roleKeycloaks"],
}); });
if (!profile) { // if (!profileEmp) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const profileEmp = await this.profileEmpRepo.findOne({ if(profileEmp) {
where: { keycloak: userId },
relations: ["roleKeycloaks"],
});
if (!profileEmp) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
profileEmp.roleKeycloaks = Array.from( profileEmp.roleKeycloaks = Array.from(
new Set([...profileEmp.roleKeycloaks, ...roleKeycloak]), new Set([...profileEmp.roleKeycloaks, ...roleKeycloak]),
); );
this.profileEmpRepo.save(profileEmp); this.profileEmpRepo.save(profileEmp);
} else {
profile.roleKeycloaks = Array.from(new Set([...profile.roleKeycloaks, ...roleKeycloak]));
this.profileRepo.save(profile);
} }
}
else {
profile.roleKeycloaks = Array.from(new Set([...profile.roleKeycloaks, ...roleKeycloak]));
this.profileRepo.save(profile);
} }
return new HttpSuccess(); return new HttpSuccess();