From 26d2f288e9c9d3a0789e9e362ae9c10ab0e4d21b Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Tue, 21 Jan 2025 11:03:48 +0100 Subject: [PATCH] add api tables with permission --- src/data-source.ts | 6 +++ src/entity/user/api.ts | 19 +++++++ src/entity/user/api_permission.ts | 19 +++++++ src/migrations/1737453096674-addApiTokens.ts | 53 ++++++++++++++++++++ 4 files changed, 97 insertions(+) create mode 100644 src/entity/user/api.ts create mode 100644 src/entity/user/api_permission.ts create mode 100644 src/migrations/1737453096674-addApiTokens.ts diff --git a/src/data-source.ts b/src/data-source.ts index c7ba457..b3c0227 100644 --- a/src/data-source.ts +++ b/src/data-source.ts @@ -68,6 +68,9 @@ import { Memberlist1736079005086 } from "./migrations/1736079005086-memberlist"; import { ExtendViewValues1736084198860 } from "./migrations/1736084198860-extendViewValues"; import { FinishInternalIdTransfer1736505324488 } from "./migrations/1736505324488-finishInternalIdTransfer"; import { ProtocolPresenceExcuse1737287798828 } from "./migrations/1737287798828-protocolPresenceExcuse"; +import { api } from "./entity/user/api"; +import { apiPermission } from "./entity/user/api_permission"; +import { AddApiTokens1737453096674 } from "./migrations/1737453096674-addApiTokens"; const dataSource = new DataSource({ type: DB_TYPE as any, @@ -117,6 +120,8 @@ const dataSource = new DataSource({ memberExecutivePositionsView, memberQualificationsView, membershipView, + api, + apiPermission, ], migrations: [ Initial1724317398939, @@ -146,6 +151,7 @@ const dataSource = new DataSource({ ExtendViewValues1736084198860, FinishInternalIdTransfer1736505324488, ProtocolPresenceExcuse1737287798828, + AddApiTokens1737453096674, ], migrationsRun: true, migrationsTransactionMode: "each", diff --git a/src/entity/user/api.ts b/src/entity/user/api.ts new file mode 100644 index 0000000..a3ead90 --- /dev/null +++ b/src/entity/user/api.ts @@ -0,0 +1,19 @@ +import { Column, CreateDateColumn, Entity, PrimaryColumn } from "typeorm"; + +@Entity() +export class api { + @PrimaryColumn({ type: "varchar", length: 255 }) + token: string; + + @Column({ type: "varchar", length: 255 }) + title: string; + + @CreateDateColumn() + createdAt: Date; + + @Column({ type: "datetime", nullable: true }) + lastUsage?: Date; + + @Column({ type: "datetime", nullable: true }) + expiry?: Date; +} diff --git a/src/entity/user/api_permission.ts b/src/entity/user/api_permission.ts new file mode 100644 index 0000000..4eb546d --- /dev/null +++ b/src/entity/user/api_permission.ts @@ -0,0 +1,19 @@ +import { Column, Entity, ManyToOne, PrimaryColumn } from "typeorm"; +import { PermissionObject, PermissionString } from "../../type/permissionTypes"; +import { api } from "./api"; + +@Entity() +export class apiPermission { + @PrimaryColumn({ type: "int" }) + apiToken: number; + + @PrimaryColumn({ type: "varchar", length: 255 }) + permission: PermissionString; + + @ManyToOne(() => api, { + nullable: false, + onDelete: "CASCADE", + onUpdate: "RESTRICT", + }) + api: api; +} diff --git a/src/migrations/1737453096674-addApiTokens.ts b/src/migrations/1737453096674-addApiTokens.ts new file mode 100644 index 0000000..182f77f --- /dev/null +++ b/src/migrations/1737453096674-addApiTokens.ts @@ -0,0 +1,53 @@ +import { MigrationInterface, QueryRunner, Table, TableForeignKey } from "typeorm"; +import { DB_TYPE } from "../env.defaults"; + +export class AddApiTokens1737453096674 implements MigrationInterface { + name = "AddApiTokens1737453096674"; + + public async up(queryRunner: QueryRunner): Promise { + const variableType_int = DB_TYPE == "mysql" ? "int" : "integer"; + + await queryRunner.createTable( + new Table({ + name: "api", + columns: [ + { name: "token", type: "varchar", length: "255", isPrimary: true, isNullable: false }, + { name: "title", type: "varchar", length: "255", isNullable: false }, + { name: "createdAt", type: "datetime", default: "CURRENT_TIMESTAMP(6)", isNullable: false }, + { name: "lastUsage", type: "datetime", isNullable: true }, + { name: "expiry", type: "datetime", isNullable: true }, + ], + }), + true + ); + + await queryRunner.createTable( + new Table({ + name: "api_permission", + columns: [ + { name: "apiToken", type: "varchar", length: "255", isPrimary: true, isNullable: false }, + { name: "permission", type: "varchar", length: "255", isPrimary: true, isNullable: false }, + ], + }) + ); + + await queryRunner.createForeignKey( + "api_permission", + new TableForeignKey({ + columnNames: ["apiToken"], + referencedColumnNames: ["token"], + referencedTableName: "api", + onDelete: "CASCADE", + onUpdate: "RESTRICT", + }) + ); + } + + public async down(queryRunner: QueryRunner): Promise { + const table = await queryRunner.getTable("api_permission"); + const foreignKey = table.foreignKeys.find((fk) => fk.columnNames.indexOf("apiToken") !== -1); + await queryRunner.dropForeignKey("api_permission", foreignKey); + await queryRunner.dropTable("api_permission"); + await queryRunner.dropTable("api"); + } +}