Skip to content

Commit 9eba6ed

Browse files
committed
add userdatabse
1 parent 8e19f0e commit 9eba6ed

File tree

10 files changed

+241
-41
lines changed

10 files changed

+241
-41
lines changed

server/package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,16 @@
2020
"test:watch": "jest --watch",
2121
"test:cov": "jest --coverage",
2222
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
23-
"test:e2e": "jest --config ./test/jest-e2e.json"
23+
"test:e2e": "jest --config ./test/jest-e2e.json",
24+
"prisma:generate": "npx prisma generate",
25+
"prisma:studio": "npx prisma studio",
26+
"prisma:reset": "npx prisma migrate reset --force",
27+
"prisma:init": "npx prisma migrate dev --name init --create-only",
28+
"prisma:deploy": "npx prisma migrate deploy",
29+
"prisma:migrate": "npx prisma migrate deploy",
30+
"prisma:push": "npx prisma db push",
31+
"prisma:db:seed": "npx prisma db seed --preview-feature",
32+
"prisma:db:seed:test": "npx prisma db seed --preview-feature --schema=./prisma/schema.prod.prisma"
2433
},
2534
"dependencies": {
2635
"@kubernetes/client-node": "^0.22.3",

server/prisma/dev_qa.db

Whitespace-only changes.

server/prisma/migrations/20250612204421_init/migration.sql

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
-- CreateTable
2+
CREATE TABLE "Audit" (
3+
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
4+
"timestamp" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
5+
"user" TEXT NOT NULL,
6+
"severity" TEXT NOT NULL DEFAULT 'normal',
7+
"action" TEXT NOT NULL,
8+
"namespace" TEXT NOT NULL,
9+
"phase" TEXT NOT NULL,
10+
"app" TEXT NOT NULL,
11+
"pipeline" TEXT NOT NULL,
12+
"resource" TEXT NOT NULL DEFAULT 'unknown',
13+
"message" TEXT NOT NULL,
14+
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
15+
"updatedAt" DATETIME NOT NULL
16+
);
17+
18+
-- CreateTable
19+
CREATE TABLE "User" (
20+
"id" TEXT NOT NULL PRIMARY KEY,
21+
"name" TEXT,
22+
"email" TEXT NOT NULL,
23+
"emailVerified" DATETIME,
24+
"password" TEXT NOT NULL,
25+
"twoFaSecret" TEXT,
26+
"twoFaEnabled" BOOLEAN NOT NULL DEFAULT false,
27+
"image" TEXT,
28+
"roleId" TEXT,
29+
"isActive" BOOLEAN NOT NULL DEFAULT true,
30+
"lastLogin" DATETIME,
31+
"lastIp" TEXT,
32+
"provider" TEXT DEFAULT 'local',
33+
"providerId" TEXT,
34+
"providerData" TEXT,
35+
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
36+
"updatedAt" DATETIME NOT NULL,
37+
CONSTRAINT "User_roleId_fkey" FOREIGN KEY ("roleId") REFERENCES "Role" ("id") ON DELETE SET NULL ON UPDATE CASCADE
38+
);
39+
40+
-- CreateTable
41+
CREATE TABLE "UserGroup" (
42+
"id" TEXT NOT NULL PRIMARY KEY,
43+
"name" TEXT NOT NULL,
44+
"description" TEXT,
45+
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
46+
"updatedAt" DATETIME NOT NULL
47+
);
48+
49+
-- CreateTable
50+
CREATE TABLE "Role" (
51+
"id" TEXT NOT NULL PRIMARY KEY,
52+
"name" TEXT NOT NULL,
53+
"description" TEXT,
54+
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
55+
"updatedAt" DATETIME NOT NULL
56+
);
57+
58+
-- CreateTable
59+
CREATE TABLE "Token" (
60+
"id" TEXT NOT NULL PRIMARY KEY,
61+
"userId" TEXT NOT NULL,
62+
"token" TEXT NOT NULL,
63+
"expiresAt" DATETIME NOT NULL,
64+
"isActive" BOOLEAN NOT NULL DEFAULT true,
65+
"lastUsed" DATETIME,
66+
"lastIp" TEXT,
67+
"description" TEXT,
68+
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
69+
"updatedAt" DATETIME NOT NULL,
70+
CONSTRAINT "Token_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
71+
);
72+
73+
-- CreateTable
74+
CREATE TABLE "Permission" (
75+
"id" TEXT NOT NULL PRIMARY KEY,
76+
"resource" TEXT NOT NULL,
77+
"action" TEXT NOT NULL,
78+
"namespace" TEXT,
79+
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
80+
"updatedAt" DATETIME NOT NULL
81+
);
82+
83+
-- CreateTable
84+
CREATE TABLE "_UserToUserGroup" (
85+
"A" TEXT NOT NULL,
86+
"B" TEXT NOT NULL,
87+
CONSTRAINT "_UserToUserGroup_A_fkey" FOREIGN KEY ("A") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
88+
CONSTRAINT "_UserToUserGroup_B_fkey" FOREIGN KEY ("B") REFERENCES "UserGroup" ("id") ON DELETE CASCADE ON UPDATE CASCADE
89+
);
90+
91+
-- CreateTable
92+
CREATE TABLE "_PermissionToRole" (
93+
"A" TEXT NOT NULL,
94+
"B" TEXT NOT NULL,
95+
CONSTRAINT "_PermissionToRole_A_fkey" FOREIGN KEY ("A") REFERENCES "Permission" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
96+
CONSTRAINT "_PermissionToRole_B_fkey" FOREIGN KEY ("B") REFERENCES "Role" ("id") ON DELETE CASCADE ON UPDATE CASCADE
97+
);
98+
99+
-- CreateTable
100+
CREATE TABLE "_PermissionToToken" (
101+
"A" TEXT NOT NULL,
102+
"B" TEXT NOT NULL,
103+
CONSTRAINT "_PermissionToToken_A_fkey" FOREIGN KEY ("A") REFERENCES "Permission" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
104+
CONSTRAINT "_PermissionToToken_B_fkey" FOREIGN KEY ("B") REFERENCES "Token" ("id") ON DELETE CASCADE ON UPDATE CASCADE
105+
);
106+
107+
-- CreateIndex
108+
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
109+
110+
-- CreateIndex
111+
CREATE UNIQUE INDEX "UserGroup_name_key" ON "UserGroup"("name");
112+
113+
-- CreateIndex
114+
CREATE UNIQUE INDEX "Role_name_key" ON "Role"("name");
115+
116+
-- CreateIndex
117+
CREATE UNIQUE INDEX "Token_token_key" ON "Token"("token");
118+
119+
-- CreateIndex
120+
CREATE UNIQUE INDEX "_UserToUserGroup_AB_unique" ON "_UserToUserGroup"("A", "B");
121+
122+
-- CreateIndex
123+
CREATE INDEX "_UserToUserGroup_B_index" ON "_UserToUserGroup"("B");
124+
125+
-- CreateIndex
126+
CREATE UNIQUE INDEX "_PermissionToRole_AB_unique" ON "_PermissionToRole"("A", "B");
127+
128+
-- CreateIndex
129+
CREATE INDEX "_PermissionToRole_B_index" ON "_PermissionToRole"("B");
130+
131+
-- CreateIndex
132+
CREATE UNIQUE INDEX "_PermissionToToken_AB_unique" ON "_PermissionToToken"("A", "B");
133+
134+
-- CreateIndex
135+
CREATE INDEX "_PermissionToToken_B_index" ON "_PermissionToToken"("B");

server/prisma/schema.prisma

Lines changed: 89 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,20 @@ generator client {
1919
}
2020

2121
model Audit {
22-
id Int @id @default(autoincrement())
23-
timestamp DateTime @default(now())
22+
id Int @id @default(autoincrement())
23+
timestamp DateTime @default(now())
2424
user String
25-
severity Severity @default(normal)
25+
severity Severity @default(normal)
2626
action String
2727
namespace String
2828
phase String
2929
app String
3030
pipeline String
31-
resource ResourceType @default(unknown)
31+
resource ResourceType @default(unknown)
3232
message String
33+
34+
createdAt DateTime @default(now())
35+
updatedAt DateTime @updatedAt
3336
}
3437

3538
enum Severity {
@@ -58,4 +61,85 @@ enum ResourceType {
5861
config
5962
addons
6063
kubernetes
61-
}
64+
}
65+
66+
model User {
67+
id String @id @default(cuid())
68+
name String?
69+
email String @unique
70+
emailVerified DateTime?
71+
password String
72+
twoFaSecret String?
73+
twoFaEnabled Boolean @default(false)
74+
image String?
75+
76+
roleId String?
77+
role Role? @relation(fields: [roleId], references: [id])
78+
userGroups UserGroup[] // Many-to-many relationship with UserGroup
79+
80+
isActive Boolean @default(true)
81+
lastLogin DateTime?
82+
lastIp String? // Last known IP address
83+
84+
provider String? @default("local") // e.g., "github", "local", ...
85+
providerId String? // ID from the external provider (e.g., GitHub ID)
86+
providerData String? // JSON string for additional provider data
87+
88+
tokens Token[]
89+
90+
createdAt DateTime @default(now())
91+
updatedAt DateTime @updatedAt
92+
}
93+
94+
model UserGroup {
95+
id String @id @default(uuid())
96+
name String @unique
97+
description String?
98+
99+
users User[]
100+
101+
createdAt DateTime @default(now())
102+
updatedAt DateTime @updatedAt
103+
}
104+
105+
model Role {
106+
id String @id @default(cuid())
107+
name String @unique
108+
description String?
109+
110+
users User[] // Users associated with this role
111+
permissions Permission[] // Permissions directly assigned to this role
112+
113+
createdAt DateTime @default(now())
114+
updatedAt DateTime @updatedAt
115+
}
116+
117+
model Token {
118+
id String @id @default(cuid())
119+
userId String
120+
user User @relation(fields: [userId], references: [id])
121+
token String @unique
122+
expiresAt DateTime
123+
isActive Boolean @default(true)
124+
lastUsed DateTime?
125+
lastIp String? // Last known IP address used for this token
126+
description String? // Description of the token's purpose
127+
128+
permissions Permission[] // Permissions associated with this token
129+
130+
createdAt DateTime @default(now())
131+
updatedAt DateTime @updatedAt
132+
}
133+
134+
model Permission {
135+
id String @id @default(cuid())
136+
resource ResourceType
137+
action String // e.g., "create", "read", "update", "delete"
138+
namespace String? // Optional namespace for scoping permissions
139+
140+
roles Role[] // Roles that have this permission
141+
tokens Token[] // Tokens that have this permission
142+
143+
createdAt DateTime @default(now())
144+
updatedAt DateTime @updatedAt
145+
}

server/src/audit/audit.service.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export class AuditService {
1818
this.enabled = false;
1919
Logger.log('⏸️ Audit logging not enabled', 'Feature');
2020
return;
21+
} else {
22+
Logger.log('✅ Audit logging enabled', 'Feature');
2123
}
2224
this.init();
2325
}
@@ -26,9 +28,6 @@ export class AuditService {
2628
if (!this.enabled) {
2729
return;
2830
}
29-
// Prisma migriert das Schema automatisch, falls nötig (z.B. mit prisma migrate deploy)
30-
Logger.log('✅ Audit logging enabled', 'Feature');
31-
3231
const auditEntry: AuditEntry = {
3332
user: 'kubero',
3433
severity: 'normal',
@@ -41,7 +40,7 @@ export class AuditService {
4140
message: 'server started',
4241
};
4342

44-
await this.log(auditEntry);
43+
await this.logDelayed(auditEntry, 5000);
4544
}
4645

4746
public logDelayed(entry: AuditEntry, delay: number = 1000) {

server/src/database/database.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Global, Module, Logger } from '@nestjs/common';
22
import { DatabaseService } from './database.service';
33
import { PrismaClient } from '@prisma/client';
44

5-
DatabaseService.Init();
5+
DatabaseService.Init(); // configing the database connection initialization
66

77
@Global()
88
@Module({

server/src/database/database.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ export class DatabaseService {
66

77
public static async Init() {
88
if (process.env.DATABASE_URL === '' || process.env.DATABASE_URL === undefined) {
9-
process.env.DATABASE_URL = 'file:../db/kubero.db';
9+
process.env.DATABASE_URL = 'file:../db/kubero.sqlite';
10+
process.env.DATABASE_TYPE = 'sqlite';
1011
Logger.debug(
1112
'DATABASE_URL is not set. Using SQLite database: ' + process.env.DATABASE_URL,
1213
'DatabaseService',

server/src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ async function bootstrap() {
3131
cors: true,
3232
});
3333

34-
await DatabaseService.RunMigrations();
34+
DatabaseService.RunMigrations();
3535

3636
app.use(
3737
helmet({

server/src/users/users.service.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,6 @@ export type User = any;
88

99
@Injectable()
1010
export class UsersService {
11-
/*
12-
private readonly users = [
13-
{
14-
userId: 1,
15-
username: 'foo',
16-
password: 'bar',
17-
},
18-
{
19-
userId: 2,
20-
username: 'mms-gianni',
21-
password: 'bar',
22-
},
23-
];
24-
*/
2511
private readonly users = [] as User[];
2612
private logger = new Logger(UsersService.name);
2713

0 commit comments

Comments
 (0)