ผูก 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();
|
||||
}
|
||||
|
||||
@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 { 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<string, any> }) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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" })
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
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