org root add responsibility

This commit is contained in:
kittapath 2025-03-14 13:26:37 +07:00
parent 11c79abc31
commit 856315b7bc
8 changed files with 74 additions and 45 deletions

View file

@ -30,7 +30,7 @@ async function main() {
); );
app.use(express.json()); app.use(express.json());
app.use(express.urlencoded({ extended: true })); app.use(express.urlencoded({ extended: true }));
// app.use(logMiddleware); app.use(logMiddleware);
app.use("/", express.static("static")); app.use("/", express.static("static"));
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument)); app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));

View file

@ -54,6 +54,7 @@ export class OrgChild1Controller {
} }
const getOrgChild1 = { const getOrgChild1 = {
orgChild1Id: orgChild1.id, orgChild1Id: orgChild1.id,
responsibility: orgChild1.responsibility,
orgRootName: orgChild1.orgRoot.orgRootName, orgRootName: orgChild1.orgRoot.orgRootName,
orgName: `${orgChild1.orgChild1Name}/${orgChild1.orgRoot.orgRootName}`, orgName: `${orgChild1.orgChild1Name}/${orgChild1.orgRoot.orgRootName}`,
orgChild1Name: orgChild1.orgChild1Name, orgChild1Name: orgChild1.orgChild1Name,

View file

@ -67,6 +67,7 @@ export class OrgChild2Controller extends Controller {
} }
const getOrgChild2 = { const getOrgChild2 = {
orgChild2Id: orgChild2.id, orgChild2Id: orgChild2.id,
responsibility: orgChild2.responsibility,
orgRootName: orgChild2.orgRoot.orgRootName, orgRootName: orgChild2.orgRoot.orgRootName,
orgName: `${orgChild2.orgChild2Name}/${orgChild2.orgChild1.orgChild1Name}/${orgChild2.orgRoot.orgRootName}`, orgName: `${orgChild2.orgChild2Name}/${orgChild2.orgChild1.orgChild1Name}/${orgChild2.orgRoot.orgRootName}`,
orgChild2Name: orgChild2.orgChild2Name, orgChild2Name: orgChild2.orgChild2Name,

View file

@ -51,6 +51,7 @@ export class OrgChild3Controller {
} }
const getOrgChild3 = { const getOrgChild3 = {
orgChild3Id: orgChild3.id, orgChild3Id: orgChild3.id,
responsibility: orgChild3.responsibility,
orgRootName: orgChild3.orgRoot.orgRootName, orgRootName: orgChild3.orgRoot.orgRootName,
orgName: `${orgChild3.orgChild3Name}/${orgChild3.orgChild2.orgChild2Name}/${orgChild3.orgChild1.orgChild1Name}/${orgChild3.orgRoot.orgRootName}`, orgName: `${orgChild3.orgChild3Name}/${orgChild3.orgChild2.orgChild2Name}/${orgChild3.orgChild1.orgChild1Name}/${orgChild3.orgRoot.orgRootName}`,
orgChild3Name: orgChild3.orgChild3Name, orgChild3Name: orgChild3.orgChild3Name,

View file

@ -64,6 +64,7 @@ export class OrgChild4Controller extends Controller {
} }
const getOrgChild4 = { const getOrgChild4 = {
orgChild4Id: orgChild4.id, orgChild4Id: orgChild4.id,
responsibility: orgChild4.responsibility,
orgRootName: orgChild4.orgRoot.orgRootName, orgRootName: orgChild4.orgRoot.orgRootName,
orgName: `${orgChild4.orgChild4Name}/${orgChild4.orgChild3.orgChild3Name}/${orgChild4.orgChild2.orgChild2Name}/${orgChild4.orgChild1.orgChild1Name}/${orgChild4.orgRoot.orgRootName}`, orgName: `${orgChild4.orgChild4Name}/${orgChild4.orgChild3.orgChild3Name}/${orgChild4.orgChild2.orgChild2Name}/${orgChild4.orgChild1.orgChild1Name}/${orgChild4.orgRoot.orgRootName}`,
orgChild4Name: orgChild4.orgChild4Name, orgChild4Name: orgChild4.orgChild4Name,

View file

@ -64,6 +64,7 @@ export class OrgRootController extends Controller {
} }
const getOrgRoot = { const getOrgRoot = {
orgRootId: orgRoot.id, orgRootId: orgRoot.id,
responsibility: orgRoot.responsibility,
orgRootName: orgRoot.orgRootName, orgRootName: orgRoot.orgRootName,
orgName: "-", orgName: "-",
orgRootShortName: orgRoot.orgRootShortName, orgRootShortName: orgRoot.orgRootShortName,

View file

@ -39,7 +39,7 @@ import { Profile } from "../entities/Profile";
import { ProfileEmployee } from "../entities/ProfileEmployee"; import { ProfileEmployee } from "../entities/ProfileEmployee";
import { RequestWithUser } from "../middlewares/user"; import { RequestWithUser } from "../middlewares/user";
import HttpSuccess from "../interfaces/http-success"; import HttpSuccess from "../interfaces/http-success";
import { Brackets, In, IsNull } from "typeorm"; import { Brackets, In, IsNull, Not } from "typeorm";
import HttpError from "../interfaces/http-error"; import HttpError from "../interfaces/http-error";
import HttpStatus from "../interfaces/http-status"; import HttpStatus from "../interfaces/http-status";
import { RoleKeycloak } from "../entities/RoleKeycloak"; import { RoleKeycloak } from "../entities/RoleKeycloak";
@ -830,7 +830,7 @@ export class KeycloakController extends Controller {
return result; return result;
} }
@Post("user/asdasdasd") @Post("user/create")
@Security("bearerAuth", ["system", "admin"]) @Security("bearerAuth", ["system", "admin"])
async createUserImport( async createUserImport(
@Request() request: { user: { sub: string; preferred_username: string } }, @Request() request: { user: { sub: string; preferred_username: string } },
@ -872,7 +872,7 @@ export class KeycloakController extends Controller {
if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server."); if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server.");
const result = await addUserRoles( const result = await addUserRoles(
userId, userId,
list.filter((v) => v.id == "8a1a0dc9-304c-4e5b-a90a-65f841048212"), list.filter((v) => v.id == "f8619dc2-dc0d-4aab-957f-66bdf905e9d0"),
); );
if (!result) { if (!result) {
@ -882,13 +882,32 @@ export class KeycloakController extends Controller {
_item.keycloak = userId; _item.keycloak = userId;
} }
const roleKeycloak = await this.roleKeycloakRepo.find({ const roleKeycloak = await this.roleKeycloakRepo.find({
where: { id: "8a1a0dc9-304c-4e5b-a90a-65f841048212" }, where: { id: "f8619dc2-dc0d-4aab-957f-66bdf905e9d0" },
}); });
if (_item) { if (_item) {
_item.roleKeycloaks = Array.from(new Set([..._item.roleKeycloaks, ...roleKeycloak])); _item.roleKeycloaks = Array.from(new Set([..._item.roleKeycloaks, ...roleKeycloak]));
this.profileRepo.save(_item); this.profileRepo.save(_item);
} }
} }
return "555+"; return "";
}
@Post("user/change-password-all")
async changeUserPasswordAll(
@Request() request: { user: { sub: string; preferred_username: string } },
) {
const profiles = await this.profileRepo.find({
where: {
keycloak: Not(IsNull()),
},
});
for await (const _item of profiles) {
const result = await changeUserPassword(_item.keycloak, "P@ssw0rd");
if (!result) {
continue;
}
}
return;
} }
} }

View file

@ -4,8 +4,8 @@ const KC_URL = process.env.KC_URL;
const KC_REALMS = process.env.KC_REALMS; const KC_REALMS = process.env.KC_REALMS;
const KC_CLIENT_ID = process.env.KC_SERVICE_ACCOUNT_CLIENT_ID; const KC_CLIENT_ID = process.env.KC_SERVICE_ACCOUNT_CLIENT_ID;
const KC_SECRET = process.env.KC_SERVICE_ACCOUNT_SECRET; const KC_SECRET = process.env.KC_SERVICE_ACCOUNT_SECRET;
const AUTH_ACCOUNT_SECRET = process.env.AUTH_ACCOUNT_SECRET const AUTH_ACCOUNT_SECRET = process.env.AUTH_ACCOUNT_SECRET;
const API_KEY = process.env.API_KEY const API_KEY = process.env.API_KEY;
let token: string | null = null; let token: string | null = null;
let decoded: DecodedJwt | null = null; let decoded: DecodedJwt | null = null;
@ -742,7 +742,6 @@ export async function removeUserGroup(userId: string, groupId: string) {
// Function to change user password // Function to change user password
export async function changeUserPassword(userId: string, newPassword: string) { export async function changeUserPassword(userId: string, newPassword: string) {
try { try {
console.log(await getToken());
const res = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}/reset-password`, { const res = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}/reset-password`, {
// prettier-ignore // prettier-ignore
headers: { headers: {
@ -788,27 +787,33 @@ export async function resetPassword(username: string) {
// const tokenData = await tokenResponse.json(); // const tokenData = await tokenResponse.json();
// const adminToken = tokenData.access_token; // const adminToken = tokenData.access_token;
const users = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/users?email=${encodeURIComponent(username)}`, { const users = await fetch(
`${KC_URL}/admin/realms/${KC_REALMS}/users?email=${encodeURIComponent(username)}`,
{
headers: { headers: {
"authorization": `Bearer ${await getToken()}`, authorization: `Bearer ${await getToken()}`,
// "authorization": `Bearer ${adminToken}`, // "authorization": `Bearer ${adminToken}`,
"content-type": `application/json`, "content-type": `application/json`,
}, },
}); },
);
if (!users.ok) { if (!users.ok) {
return false; return false;
} }
const usersData = await users.json(); const usersData = await users.json();
const userId = usersData[0].id; const userId = usersData[0].id;
const resetResponse = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}/execute-actions-email`, { const resetResponse = await fetch(
`${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}/execute-actions-email`,
{
method: "PUT", method: "PUT",
headers: { headers: {
"Authorization": `Bearer ${await getToken()}`, Authorization: `Bearer ${await getToken()}`,
// "Authorization": `Bearer ${adminToken}`, // "Authorization": `Bearer ${adminToken}`,
"Content-Type": "application/json" "Content-Type": "application/json",
}, },
body: JSON.stringify(["UPDATE_PASSWORD"]) body: JSON.stringify(["UPDATE_PASSWORD"]),
}); },
);
if (!resetResponse.ok) { if (!resetResponse.ok) {
return false; return false;
} }