From 73ae3210dc3f7449b5a84506c84e8c58f2e40d23 Mon Sep 17 00:00:00 2001 From: Kittapath Date: Thu, 25 Jul 2024 09:54:44 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B8=9C=E0=B8=B9=E0=B8=81=20menu=20=E0=B8=81?= =?UTF-8?q?=E0=B8=B1=E0=B8=9A=20redis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/AuthRoleController.ts | 36 ---- src/controllers/PermissionController.ts | 189 +++++++++++------- src/entities/AuthRole.ts | 6 +- src/entities/AuthRoleAttr.ts | 6 +- src/entities/AuthSys.ts | 14 +- src/entities/EmployeePosMaster.ts | 2 +- src/entities/PosMaster.ts | 2 +- ...1872671794-update_table_authSys_add_key.ts | 28 +++ 8 files changed, 164 insertions(+), 119 deletions(-) create mode 100644 src/migration/1721872671794-update_table_authSys_add_key.ts diff --git a/src/controllers/AuthRoleController.ts b/src/controllers/AuthRoleController.ts index b7cc954f..e1ac90d6 100644 --- a/src/controllers/AuthRoleController.ts +++ b/src/controllers/AuthRoleController.ts @@ -235,40 +235,4 @@ export class AuthRoleController extends Controller { return new HttpSuccess(); } - - @Get("test/asdasd/{roleId}") - public async userRoleRedis(@Path() roleId: string) { - const redisClient = await this.redis.createClient({ - host: REDIS_HOST, - port: REDIS_PORT, - }); - - console.log("xxxxxxxxxxxxxxxxx7"); - let result = null; - console.log("xxxxxxxxxxxxxxxxx8"); - await Promise.all([ - await redisClient.get(roleId, async (err: any, reply: any) => { - console.log("xxxxxxxxxxxxxxxxx1"); - console.log(reply); - if (reply != null) { - console.log("xxxxxxxxxxxxxxxxx2"); - // console.log(reply); - // console.log(JSON.parse(reply)); - result = JSON.parse(reply); - // return; - } else { - console.log("xxxxxxxxxxxxxxxxx3"); - result = await this.authRoleRepo.find(); - console.log("xxxxxxxxxxxxxxxxx4"); - redisClient.setex(roleId, 20, JSON.stringify(result)); - // return new HttpSuccess(result); - console.log("xxxxxxxxxxxxxxxxx5"); - } - return new HttpSuccess(result); - }), - ]); - console.log("xxxxxxxxxxxxxxxxx6"); - console.log(result); - return new HttpSuccess("result"); - } } diff --git a/src/controllers/PermissionController.ts b/src/controllers/PermissionController.ts index 25964ae6..b5539005 100644 --- a/src/controllers/PermissionController.ts +++ b/src/controllers/PermissionController.ts @@ -10,6 +10,8 @@ import { AuthRoleAttr } from "../entities/AuthRoleAttr"; import { PosMaster } from "../entities/PosMaster"; import { Profile } from "../entities/Profile"; import { AuthSys } from "../entities/AuthSys"; +import { promisify } from "util"; +import { In } from "typeorm"; const REDIS_HOST = process.env.REDIS_HOST; const REDIS_PORT = process.env.REDIS_PORT; @@ -30,85 +32,128 @@ export class PermissionController extends Controller { host: REDIS_HOST, port: REDIS_PORT, }); - // const formattedData = null; - // await Promise.all([ - // return new HttpSuccess( - // redisClient.get(request.user.sub, async (err: any, reply: any) => { - // if (reply != null) { - // return JSON.parse(reply); - // } else { - const profile = await this.profileRepo.findOne({ - select: ["id"], - where: { keycloak: request.user.sub }, - }); - if (!profile) { - throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ"); + const getAsync = promisify(redisClient.get).bind(redisClient); + + let reply = await getAsync("role_" + request.user.sub); + if (reply != null) { + reply = JSON.parse(reply); + } else { + const profile = await this.profileRepo.findOne({ + select: ["id"], + where: { keycloak: request.user.sub }, + }); + if (!profile) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ"); + } + + const posMaster = await this.posMasterRepository.findOne({ + select: ["authRoleId"], + where: { current_holderId: profile.id }, + }); + if (!posMaster) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลสิทธิ์"); + } + + const getDetail = await this.authRoleRepo.findOne({ + select: ["id", "roleName", "roleDescription"], + where: { id: posMaster.authRoleId }, + }); + if (!getDetail) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + + const roleAttrData = await this.authRoleAttrRepo.find({ + select: [ + "authSysId", + "parentNode", + "attrOwnership", + "attrIsCreate", + "attrIsList", + "attrIsGet", + "attrIsUpdate", + "attrIsDelete", + "attrPrivilege", + ], + where: { authRoleId: getDetail.id }, + }); + + reply = { + ...getDetail, + roles: roleAttrData, + }; + redisClient.set("role_" + request.user.sub, JSON.stringify(reply)); } - - const posMaster = await this.posMasterRepository.findOne({ - select: ["authRoleId"], - where: { current_holderId: profile.id }, - }); - if (!posMaster) { - throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลสิทธิ์"); - } - - const getDetail = await this.authRoleRepo.findOne({ - select: ["id", "roleName", "roleDescription"], - where: { id: posMaster.authRoleId }, - }); - if (!getDetail) { - throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - } - - const roleAttrData = await this.authRoleAttrRepo.find({ - select: [ - "authSysId", - "parentNode", - "attrOwnership", - "attrIsCreate", - "attrIsList", - "attrIsGet", - "attrIsUpdate", - "attrIsDelete", - "attrPrivilege", - ], - where: { authRoleId: getDetail.id }, - }); - - const formattedData = { - ...getDetail, - roles: roleAttrData, - }; - return new HttpSuccess(formattedData); - // redisClient.setex(request.user.sub, 20, JSON.stringify(formattedData)); - // return formattedData; - // } - // }), - // ); - // ]); + return new HttpSuccess(reply); } @Get("menu") - public async listAuthSys() { - const getList = await this.authSysRepo.find({ - select: ["id", "parentId", "sysName", "sysDescription", "icon", "path", "order"], + public async listAuthSys(@Request() request: { user: Record }) { + const redisClient = await this.redis.createClient({ + host: REDIS_HOST, + port: REDIS_PORT, }); + const getAsync = promisify(redisClient.get).bind(redisClient); - if (!getList || getList.length === 0) { - throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + let reply = await getAsync("menu_" + request.user.sub); + if (reply != null) { + reply = JSON.parse(reply); + } else { + console.log(request.user.sub); + const profile = await this.profileRepo.findOne({ + select: ["id"], + where: { keycloak: request.user.sub }, + }); + if (!profile) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ"); + } + const posMaster = await this.posMasterRepository.findOne({ + // select: ["authRoleId"], + where: { + current_holderId: profile.id, + orgRevision: { + orgRevisionIsDraft: false, + orgRevisionIsCurrent: true, + }, + }, + }); + if (!posMaster) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งในโครงสร้าง"); + } + + const authRole = await this.authRoleRepo.findOne({ + select: ["id"], + where: { id: posMaster.authRoleId }, + }); + if (!authRole) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลสิทธิ์"); + } + const roleAttrData = await this.authRoleAttrRepo.find({ + select: ["authSysId"], + where: { authRoleId: authRole.id }, + }); + + const getList = await this.authSysRepo.find({ + select: ["id", "parentId", "sysName", "sysDescription", "icon", "path", "order"], + where: { + id: In(roleAttrData.map((x) => x.authSysId)), + }, + }); + + const reply = getList + .filter((x) => x.parentId == null) + .map((item) => { + return { + ...item, + children: getList + .filter((x) => x.parentId == item.id) + .sort((a, b) => a.order - b.order), + }; + }) + .sort((a, b) => a.order - b.order); + console.log(JSON.stringify(reply)); + redisClient.set("menu_" + request.user.sub, 30, JSON.stringify(reply)); } - const lists = getList - .filter((x) => x.parentId == null) - .map((item) => { - return { - ...item, - children: getList.filter((x) => x.parentId == item.id).sort((a, b) => a.order - b.order), - }; - }) - .sort((a, b) => a.order - b.order); - - return new HttpSuccess(lists); + return new HttpSuccess(reply); } } diff --git a/src/entities/AuthRole.ts b/src/entities/AuthRole.ts index 8ee0b4a1..e6de3abc 100644 --- a/src/entities/AuthRole.ts +++ b/src/entities/AuthRole.ts @@ -26,10 +26,10 @@ export class AuthRole extends EntityBase { authRoles: AuthRoleAttr[]; @OneToMany(() => PosMaster, (posMaster) => posMaster.authRole) - posMaster: PosMaster[]; + posMasters: PosMaster[]; @OneToMany(() => EmployeePosMaster, (posMasters) => posMasters.authRole) - posMasterEmp: EmployeePosMaster[]; + posMasterEmps: EmployeePosMaster[]; } export class CreateAuthRole { @@ -48,4 +48,4 @@ export class CreateAddAuthRole { @Column() posMasterId: string; -} \ No newline at end of file +} diff --git a/src/entities/AuthRoleAttr.ts b/src/entities/AuthRoleAttr.ts index cdd985d5..78fd9372 100644 --- a/src/entities/AuthRoleAttr.ts +++ b/src/entities/AuthRoleAttr.ts @@ -75,9 +75,9 @@ export class AuthRoleAttr extends EntityBase { }) parentNode?: string; - // @ManyToOne(() => AuthSys, (authSys) => authSys) - // @JoinColumn({ name: "authSysId" }) - // authRoleAttrForSys: AuthSys; + @ManyToOne(() => AuthSys, (authSys) => authSys.authRoleAttrs) + @JoinColumn({ name: "authSysId" }) + authRoleAttrForSys: AuthSys; @ManyToOne(() => AuthRole, (authRole) => authRole.authRoles) @JoinColumn({ name: "authRoleId" }) diff --git a/src/entities/AuthSys.ts b/src/entities/AuthSys.ts index 3626578f..e6704f2a 100644 --- a/src/entities/AuthSys.ts +++ b/src/entities/AuthSys.ts @@ -1,4 +1,12 @@ -import { Entity, Column, CreateDateColumn, UpdateDateColumn, PrimaryColumn } from "typeorm"; +import { + Entity, + Column, + CreateDateColumn, + UpdateDateColumn, + PrimaryColumn, + OneToMany, +} from "typeorm"; +import { AuthRoleAttr } from "./AuthRoleAttr"; @Entity("authSys") export class AuthSys { @@ -80,8 +88,8 @@ export class AuthSys { }) sysDescription: string; - // @OneToMany(() => AuthRoleAttr, (authRoleAttr) => authRoleAttr.authRoleAttrForSys) - // authSys: AuthRoleAttr[]; + @OneToMany(() => AuthRoleAttr, (authRoleAttr) => authRoleAttr.authRoleAttrForSys) + authRoleAttrs: AuthRoleAttr[]; } export class CreateAuthSys { diff --git a/src/entities/EmployeePosMaster.ts b/src/entities/EmployeePosMaster.ts index 3b8a59d3..5401bf53 100644 --- a/src/entities/EmployeePosMaster.ts +++ b/src/entities/EmployeePosMaster.ts @@ -180,7 +180,7 @@ export class EmployeePosMaster extends EntityBase { }) authRoleId: string; - @ManyToOne(() => AuthRole, (authRole) => authRole.posMasterEmp) + @ManyToOne(() => AuthRole, (authRole) => authRole.posMasterEmps) @JoinColumn({ name: "authRoleId" }) authRole: AuthRole; diff --git a/src/entities/PosMaster.ts b/src/entities/PosMaster.ts index d3895306..cae1a556 100644 --- a/src/entities/PosMaster.ts +++ b/src/entities/PosMaster.ts @@ -180,7 +180,7 @@ export class PosMaster extends EntityBase { }) authRoleId: string; - @ManyToOne(() => AuthRole, (authRole) => authRole.posMaster) + @ManyToOne(() => AuthRole, (authRole) => authRole.posMasters) @JoinColumn({ name: "authRoleId" }) authRole: AuthRole; diff --git a/src/migration/1721872671794-update_table_authSys_add_key.ts b/src/migration/1721872671794-update_table_authSys_add_key.ts new file mode 100644 index 00000000..8b0820f6 --- /dev/null +++ b/src/migration/1721872671794-update_table_authSys_add_key.ts @@ -0,0 +1,28 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class UpdateTableAuthSysAddKey1721872671794 implements MigrationInterface { + name = 'UpdateTableAuthSysAddKey1721872671794' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`authRoleAttr\` CHANGE \`attrOwnership\` \`attrOwnership\` varchar(255) NULL COMMENT 'ความเป็นเจ้าของ (Ownership)'`); + await queryRunner.query(`ALTER TABLE \`authRoleAttr\` CHANGE \`attrIsCreate\` \`attrIsCreate\` tinyint NOT NULL COMMENT 'สิทธิ์ดำเนินการ (Permission) การ Create' DEFAULT 0`); + await queryRunner.query(`ALTER TABLE \`authRoleAttr\` CHANGE \`attrIsList\` \`attrIsList\` tinyint NOT NULL COMMENT 'สิทธิ์ดำเนินการ (Permission) การ List' DEFAULT 0`); + await queryRunner.query(`ALTER TABLE \`authRoleAttr\` CHANGE \`attrIsGet\` \`attrIsGet\` tinyint NOT NULL COMMENT 'สิทธิ์ดำเนินการ (Permission) การ Get' DEFAULT 0`); + await queryRunner.query(`ALTER TABLE \`authRoleAttr\` CHANGE \`attrIsUpdate\` \`attrIsUpdate\` tinyint NOT NULL COMMENT 'สิทธิ์ดำเนินการ (Permission) การ Update' DEFAULT 0`); + await queryRunner.query(`ALTER TABLE \`authRoleAttr\` CHANGE \`attrIsDelete\` \`attrIsDelete\` tinyint NOT NULL COMMENT 'สิทธิ์ดำเนินการ (Permission) การ Delete' DEFAULT 0`); + await queryRunner.query(`ALTER TABLE \`authRoleAttr\` CHANGE \`attrPrivilege\` \`attrPrivilege\` varchar(255) NULL COMMENT 'สิทธิการเข้าถึง(Privilege)'`); + await queryRunner.query(`ALTER TABLE \`authRoleAttr\` ADD CONSTRAINT \`FK_b5b59c60792d518f4f025379dba\` FOREIGN KEY (\`authSysId\`) REFERENCES \`authSys\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`authRoleAttr\` DROP FOREIGN KEY \`FK_b5b59c60792d518f4f025379dba\``); + await queryRunner.query(`ALTER TABLE \`authRoleAttr\` CHANGE \`attrPrivilege\` \`attrPrivilege\` varchar(255) NULL`); + await queryRunner.query(`ALTER TABLE \`authRoleAttr\` CHANGE \`attrIsDelete\` \`attrIsDelete\` tinyint NOT NULL DEFAULT '0'`); + await queryRunner.query(`ALTER TABLE \`authRoleAttr\` CHANGE \`attrIsUpdate\` \`attrIsUpdate\` tinyint NOT NULL DEFAULT '0'`); + await queryRunner.query(`ALTER TABLE \`authRoleAttr\` CHANGE \`attrIsGet\` \`attrIsGet\` tinyint NOT NULL DEFAULT '0'`); + await queryRunner.query(`ALTER TABLE \`authRoleAttr\` CHANGE \`attrIsList\` \`attrIsList\` tinyint NOT NULL DEFAULT '0'`); + await queryRunner.query(`ALTER TABLE \`authRoleAttr\` CHANGE \`attrIsCreate\` \`attrIsCreate\` tinyint NOT NULL DEFAULT '0'`); + await queryRunner.query(`ALTER TABLE \`authRoleAttr\` CHANGE \`attrOwnership\` \`attrOwnership\` varchar(255) NULL`); + } + +}