add api tables with permission

This commit is contained in:
Julian Krauser 2025-01-21 11:03:48 +01:00
parent 0bbe22e9ae
commit 26d2f288e9
4 changed files with 97 additions and 0 deletions

View file

@ -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",

19
src/entity/user/api.ts Normal file
View file

@ -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;
}

View file

@ -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;
}

View file

@ -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<void> {
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<void> {
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");
}
}