ผูก menu กับ redis
This commit is contained in:
parent
1e3699b049
commit
73ae3210dc
8 changed files with 164 additions and 119 deletions
|
|
@ -235,40 +235,4 @@ export class AuthRoleController extends Controller {
|
||||||
|
|
||||||
return new HttpSuccess();
|
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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,8 @@ import { AuthRoleAttr } from "../entities/AuthRoleAttr";
|
||||||
import { PosMaster } from "../entities/PosMaster";
|
import { PosMaster } from "../entities/PosMaster";
|
||||||
import { Profile } from "../entities/Profile";
|
import { Profile } from "../entities/Profile";
|
||||||
import { AuthSys } from "../entities/AuthSys";
|
import { AuthSys } from "../entities/AuthSys";
|
||||||
|
import { promisify } from "util";
|
||||||
|
import { In } from "typeorm";
|
||||||
const REDIS_HOST = process.env.REDIS_HOST;
|
const REDIS_HOST = process.env.REDIS_HOST;
|
||||||
const REDIS_PORT = process.env.REDIS_PORT;
|
const REDIS_PORT = process.env.REDIS_PORT;
|
||||||
|
|
||||||
|
|
@ -30,85 +32,128 @@ export class PermissionController extends Controller {
|
||||||
host: REDIS_HOST,
|
host: REDIS_HOST,
|
||||||
port: REDIS_PORT,
|
port: REDIS_PORT,
|
||||||
});
|
});
|
||||||
// const formattedData = null;
|
const getAsync = promisify(redisClient.get).bind(redisClient);
|
||||||
// await Promise.all([
|
|
||||||
// return new HttpSuccess(
|
let reply = await getAsync("role_" + request.user.sub);
|
||||||
// redisClient.get(request.user.sub, async (err: any, reply: any) => {
|
if (reply != null) {
|
||||||
// if (reply != null) {
|
reply = JSON.parse(reply);
|
||||||
// return JSON.parse(reply);
|
} else {
|
||||||
// } else {
|
const profile = await this.profileRepo.findOne({
|
||||||
const profile = await this.profileRepo.findOne({
|
select: ["id"],
|
||||||
select: ["id"],
|
where: { keycloak: request.user.sub },
|
||||||
where: { keycloak: request.user.sub },
|
});
|
||||||
});
|
if (!profile) {
|
||||||
if (!profile) {
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ");
|
||||||
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));
|
||||||
}
|
}
|
||||||
|
return new HttpSuccess(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;
|
|
||||||
// }
|
|
||||||
// }),
|
|
||||||
// );
|
|
||||||
// ]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get("menu")
|
@Get("menu")
|
||||||
public async listAuthSys() {
|
public async listAuthSys(@Request() request: { user: Record<string, any> }) {
|
||||||
const getList = await this.authSysRepo.find({
|
const redisClient = await this.redis.createClient({
|
||||||
select: ["id", "parentId", "sysName", "sysDescription", "icon", "path", "order"],
|
host: REDIS_HOST,
|
||||||
|
port: REDIS_PORT,
|
||||||
});
|
});
|
||||||
|
const getAsync = promisify(redisClient.get).bind(redisClient);
|
||||||
|
|
||||||
if (!getList || getList.length === 0) {
|
let reply = await getAsync("menu_" + request.user.sub);
|
||||||
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
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
|
return new HttpSuccess(reply);
|
||||||
.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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,10 +26,10 @@ export class AuthRole extends EntityBase {
|
||||||
authRoles: AuthRoleAttr[];
|
authRoles: AuthRoleAttr[];
|
||||||
|
|
||||||
@OneToMany(() => PosMaster, (posMaster) => posMaster.authRole)
|
@OneToMany(() => PosMaster, (posMaster) => posMaster.authRole)
|
||||||
posMaster: PosMaster[];
|
posMasters: PosMaster[];
|
||||||
|
|
||||||
@OneToMany(() => EmployeePosMaster, (posMasters) => posMasters.authRole)
|
@OneToMany(() => EmployeePosMaster, (posMasters) => posMasters.authRole)
|
||||||
posMasterEmp: EmployeePosMaster[];
|
posMasterEmps: EmployeePosMaster[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export class CreateAuthRole {
|
export class CreateAuthRole {
|
||||||
|
|
|
||||||
|
|
@ -75,9 +75,9 @@ export class AuthRoleAttr extends EntityBase {
|
||||||
})
|
})
|
||||||
parentNode?: string;
|
parentNode?: string;
|
||||||
|
|
||||||
// @ManyToOne(() => AuthSys, (authSys) => authSys)
|
@ManyToOne(() => AuthSys, (authSys) => authSys.authRoleAttrs)
|
||||||
// @JoinColumn({ name: "authSysId" })
|
@JoinColumn({ name: "authSysId" })
|
||||||
// authRoleAttrForSys: AuthSys;
|
authRoleAttrForSys: AuthSys;
|
||||||
|
|
||||||
@ManyToOne(() => AuthRole, (authRole) => authRole.authRoles)
|
@ManyToOne(() => AuthRole, (authRole) => authRole.authRoles)
|
||||||
@JoinColumn({ name: "authRoleId" })
|
@JoinColumn({ name: "authRoleId" })
|
||||||
|
|
|
||||||
|
|
@ -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")
|
@Entity("authSys")
|
||||||
export class AuthSys {
|
export class AuthSys {
|
||||||
|
|
@ -80,8 +88,8 @@ export class AuthSys {
|
||||||
})
|
})
|
||||||
sysDescription: string;
|
sysDescription: string;
|
||||||
|
|
||||||
// @OneToMany(() => AuthRoleAttr, (authRoleAttr) => authRoleAttr.authRoleAttrForSys)
|
@OneToMany(() => AuthRoleAttr, (authRoleAttr) => authRoleAttr.authRoleAttrForSys)
|
||||||
// authSys: AuthRoleAttr[];
|
authRoleAttrs: AuthRoleAttr[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export class CreateAuthSys {
|
export class CreateAuthSys {
|
||||||
|
|
|
||||||
|
|
@ -180,7 +180,7 @@ export class EmployeePosMaster extends EntityBase {
|
||||||
})
|
})
|
||||||
authRoleId: string;
|
authRoleId: string;
|
||||||
|
|
||||||
@ManyToOne(() => AuthRole, (authRole) => authRole.posMasterEmp)
|
@ManyToOne(() => AuthRole, (authRole) => authRole.posMasterEmps)
|
||||||
@JoinColumn({ name: "authRoleId" })
|
@JoinColumn({ name: "authRoleId" })
|
||||||
authRole: AuthRole;
|
authRole: AuthRole;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -180,7 +180,7 @@ export class PosMaster extends EntityBase {
|
||||||
})
|
})
|
||||||
authRoleId: string;
|
authRoleId: string;
|
||||||
|
|
||||||
@ManyToOne(() => AuthRole, (authRole) => authRole.posMaster)
|
@ManyToOne(() => AuthRole, (authRole) => authRole.posMasters)
|
||||||
@JoinColumn({ name: "authRoleId" })
|
@JoinColumn({ name: "authRoleId" })
|
||||||
authRole: AuthRole;
|
authRole: AuthRole;
|
||||||
|
|
||||||
|
|
|
||||||
28
src/migration/1721872671794-update_table_authSys_add_key.ts
Normal file
28
src/migration/1721872671794-update_table_authSys_add_key.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
import { MigrationInterface, QueryRunner } from "typeorm";
|
||||||
|
|
||||||
|
export class UpdateTableAuthSysAddKey1721872671794 implements MigrationInterface {
|
||||||
|
name = 'UpdateTableAuthSysAddKey1721872671794'
|
||||||
|
|
||||||
|
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
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<void> {
|
||||||
|
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`);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue