add-role-staff/user
This commit is contained in:
parent
191ca586c5
commit
91281ba995
1 changed files with 97 additions and 8 deletions
|
|
@ -852,10 +852,15 @@ export class KeycloakController extends Controller {
|
||||||
// _item.birthDate.toISOString().slice(5, 7) +
|
// _item.birthDate.toISOString().slice(5, 7) +
|
||||||
// gregorianYear;
|
// gregorianYear;
|
||||||
// password = formattedDate;
|
// password = formattedDate;
|
||||||
const _date = new Date(_item.birthDate.toDateString()).getDate().toString().padStart(2, "0");
|
const _date = new Date(_item.birthDate.toDateString())
|
||||||
const _month = (new Date(_item.birthDate.toDateString()).getMonth()+1).toString().padStart(2, "0");
|
.getDate()
|
||||||
const _year = (new Date(_item.birthDate.toDateString()).getFullYear()+543);
|
.toString()
|
||||||
password = `${_date}${_month}${_year}`
|
.padStart(2, "0");
|
||||||
|
const _month = (new Date(_item.birthDate.toDateString()).getMonth() + 1)
|
||||||
|
.toString()
|
||||||
|
.padStart(2, "0");
|
||||||
|
const _year = new Date(_item.birthDate.toDateString()).getFullYear() + 543;
|
||||||
|
password = `${_date}${_month}${_year}`;
|
||||||
}
|
}
|
||||||
const checkUser = await getUserByUsername(_item.citizenId);
|
const checkUser = await getUserByUsername(_item.citizenId);
|
||||||
let userId: any = "";
|
let userId: any = "";
|
||||||
|
|
@ -918,10 +923,15 @@ export class KeycloakController extends Controller {
|
||||||
// _item.birthDate.toISOString().slice(5, 7) +
|
// _item.birthDate.toISOString().slice(5, 7) +
|
||||||
// gregorianYear;
|
// gregorianYear;
|
||||||
// password = formattedDate;
|
// password = formattedDate;
|
||||||
const _date = new Date(_item.birthDate.toDateString()).getDate().toString().padStart(2, "0");
|
const _date = new Date(_item.birthDate.toDateString())
|
||||||
const _month = (new Date(_item.birthDate.toDateString()).getMonth()+1).toString().padStart(2, "0");
|
.getDate()
|
||||||
const _year = (new Date(_item.birthDate.toDateString()).getFullYear()+543);
|
.toString()
|
||||||
password = `${_date}${_month}${_year}`
|
.padStart(2, "0");
|
||||||
|
const _month = (new Date(_item.birthDate.toDateString()).getMonth() + 1)
|
||||||
|
.toString()
|
||||||
|
.padStart(2, "0");
|
||||||
|
const _year = new Date(_item.birthDate.toDateString()).getFullYear() + 543;
|
||||||
|
password = `${_date}${_month}${_year}`;
|
||||||
}
|
}
|
||||||
const checkUser = await getUserByUsername(_item.citizenId);
|
const checkUser = await getUserByUsername(_item.citizenId);
|
||||||
let userId: any = "";
|
let userId: any = "";
|
||||||
|
|
@ -990,4 +1000,83 @@ export class KeycloakController extends Controller {
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Post("add-role-staff/user/{child1Id}")
|
||||||
|
@Security("bearerAuth", ["system", "admin"])
|
||||||
|
async addroleStaffToUser(
|
||||||
|
@Path() child1Id: string,
|
||||||
|
@Request() request: { user: { sub: string; preferred_username: string } },
|
||||||
|
) {
|
||||||
|
const profiles = await this.profileRepo.find({
|
||||||
|
where: {
|
||||||
|
keycloak: Not(IsNull()),
|
||||||
|
current_holders: {
|
||||||
|
orgChild1Id: child1Id,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
relations: ["roleKeycloaks"],
|
||||||
|
});
|
||||||
|
// return profiles.length;
|
||||||
|
|
||||||
|
for await (const _item of profiles) {
|
||||||
|
let password = _item.citizenId;
|
||||||
|
if (_item.birthDate != null) {
|
||||||
|
const _date = new Date(_item.birthDate.toDateString())
|
||||||
|
.getDate()
|
||||||
|
.toString()
|
||||||
|
.padStart(2, "0");
|
||||||
|
const _month = (new Date(_item.birthDate.toDateString()).getMonth() + 1)
|
||||||
|
.toString()
|
||||||
|
.padStart(2, "0");
|
||||||
|
const _year = new Date(_item.birthDate.toDateString()).getFullYear() + 543;
|
||||||
|
password = `${_date}${_month}${_year}`;
|
||||||
|
}
|
||||||
|
const checkUser = await getUserByUsername(_item.citizenId);
|
||||||
|
let userId: any = "";
|
||||||
|
if (checkUser.length == 0) {
|
||||||
|
userId = await createUser(_item.citizenId, password, {
|
||||||
|
firstName: _item.firstName,
|
||||||
|
lastName: _item.lastName,
|
||||||
|
});
|
||||||
|
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 resultUser = await addUserRoles(
|
||||||
|
userId,
|
||||||
|
list.filter((v) => v.id == "8a1a0dc9-304c-4e5b-a90a-65f841048212"),
|
||||||
|
);
|
||||||
|
const resultStaff = await addUserRoles(
|
||||||
|
userId,
|
||||||
|
list.filter((v) => v.id == "f1fff8db-0795-47c1-9952-f3c18d5b6172"),
|
||||||
|
);
|
||||||
|
|
||||||
|
// if (!resultUser) {
|
||||||
|
// throw new Error("Failed. Cannot set user's role.");
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if (!resultStaff) {
|
||||||
|
// throw new Error("Failed. Cannot set staff's role.");
|
||||||
|
// }
|
||||||
|
if (typeof userId === "string") {
|
||||||
|
_item.keycloak = userId;
|
||||||
|
}
|
||||||
|
const roleKeycloakUser = await this.roleKeycloakRepo.find({
|
||||||
|
where: { id: "8a1a0dc9-304c-4e5b-a90a-65f841048212" },
|
||||||
|
});
|
||||||
|
const roleKeycloakStaff = await this.roleKeycloakRepo.find({
|
||||||
|
where: { id: "f1fff8db-0795-47c1-9952-f3c18d5b6172" },
|
||||||
|
});
|
||||||
|
if (_item) {
|
||||||
|
_item.roleKeycloaks = Array.from(new Set([...roleKeycloakUser, ...roleKeycloakStaff]));
|
||||||
|
this.profileRepo.save(_item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue