feat: add workspace
This commit is contained in:
parent
2193ff4bcb
commit
84c663985a
@ -1,2 +1,9 @@
|
|||||||
# tianji
|
# tianji
|
||||||
Tianji must not be leaked
|
Tianji must not be leaked
|
||||||
|
|
||||||
|
|
||||||
|
## Open Source
|
||||||
|
|
||||||
|
`Tianji` is open source with `Apache 2.0` license.
|
||||||
|
|
||||||
|
And its inspired by `umami` license which under `MIT` and `uptime-kuma` which under `MIT` license too
|
||||||
|
@ -8,7 +8,7 @@ generator client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
model User {
|
model User {
|
||||||
id String @id @unique @db.Uuid @default(uuid())
|
id String @id @unique @default(uuid()) @db.Uuid
|
||||||
username String @unique @db.VarChar(255)
|
username String @unique @db.VarChar(255)
|
||||||
password String @db.VarChar(60)
|
password String @db.VarChar(60)
|
||||||
role String @db.VarChar(50)
|
role String @db.VarChar(50)
|
||||||
@ -16,25 +16,50 @@ model User {
|
|||||||
updatedAt DateTime? @updatedAt @db.Timestamptz(6)
|
updatedAt DateTime? @updatedAt @db.Timestamptz(6)
|
||||||
deletedAt DateTime? @db.Timestamptz(6)
|
deletedAt DateTime? @db.Timestamptz(6)
|
||||||
|
|
||||||
|
workspaces WorkspacesOnUsers[]
|
||||||
|
}
|
||||||
|
|
||||||
|
model Workspace {
|
||||||
|
id String @id @unique @default(uuid()) @db.Uuid
|
||||||
|
name String @db.VarChar(100)
|
||||||
|
createdAt DateTime? @default(now()) @db.Timestamptz(6)
|
||||||
|
updatedAt DateTime? @updatedAt @db.Timestamptz(6)
|
||||||
|
|
||||||
|
users WorkspacesOnUsers[]
|
||||||
website Website[]
|
website Website[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
model WorkspacesOnUsers {
|
||||||
|
userId String @db.Uuid
|
||||||
|
workspaceId String @db.Uuid
|
||||||
|
role String @db.VarChar(100)
|
||||||
|
createdAt DateTime? @default(now()) @db.Timestamptz(6)
|
||||||
|
updatedAt DateTime? @updatedAt @db.Timestamptz(6)
|
||||||
|
|
||||||
|
user User @relation(fields: [userId], references: [id])
|
||||||
|
workspace Workspace @relation(fields: [workspaceId], references: [id])
|
||||||
|
|
||||||
|
@@id([userId, workspaceId])
|
||||||
|
@@index([userId])
|
||||||
|
@@index([workspaceId])
|
||||||
|
}
|
||||||
|
|
||||||
model Website {
|
model Website {
|
||||||
id String @id @unique @db.Uuid @default(uuid())
|
id String @id @unique @default(uuid()) @db.Uuid
|
||||||
name String @db.VarChar(100)
|
name String @db.VarChar(100)
|
||||||
domain String? @db.VarChar(500)
|
domain String? @db.VarChar(500)
|
||||||
shareId String? @unique @db.VarChar(50)
|
shareId String? @unique @db.VarChar(50)
|
||||||
resetAt DateTime? @db.Timestamptz(6)
|
resetAt DateTime? @db.Timestamptz(6)
|
||||||
userId String? @db.Uuid
|
workspace Workspace @relation(fields: [workspaceId], references: [id])
|
||||||
|
workspaceId String @db.Uuid
|
||||||
createdAt DateTime? @default(now()) @db.Timestamptz(6)
|
createdAt DateTime? @default(now()) @db.Timestamptz(6)
|
||||||
updatedAt DateTime? @updatedAt @db.Timestamptz(6)
|
updatedAt DateTime? @updatedAt @db.Timestamptz(6)
|
||||||
deletedAt DateTime? @db.Timestamptz(6)
|
deletedAt DateTime? @db.Timestamptz(6)
|
||||||
|
|
||||||
user User? @relation(fields: [userId], references: [id])
|
|
||||||
eventData EventData[]
|
eventData EventData[]
|
||||||
sessionData SessionData[]
|
sessionData SessionData[]
|
||||||
|
|
||||||
@@index([userId])
|
@@index([workspaceId])
|
||||||
@@index([createdAt])
|
@@index([createdAt])
|
||||||
@@index([shareId])
|
@@index([shareId])
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { prisma } from './_client';
|
import { prisma } from './_client';
|
||||||
import bcryptjs from 'bcryptjs';
|
import bcryptjs from 'bcryptjs';
|
||||||
|
import { ROLES } from '../utils/const';
|
||||||
|
|
||||||
async function hashPassword(password: string) {
|
async function hashPassword(password: string) {
|
||||||
return await bcryptjs.hash(password, 10);
|
return await bcryptjs.hash(password, 10);
|
||||||
@ -19,17 +20,51 @@ export async function createAdminUser(username: string, password: string) {
|
|||||||
data: {
|
data: {
|
||||||
username,
|
username,
|
||||||
password: await hashPassword(password),
|
password: await hashPassword(password),
|
||||||
role: 'admin',
|
role: ROLES.admin,
|
||||||
|
workspaces: {
|
||||||
|
create: [
|
||||||
|
{
|
||||||
|
role: ROLES.owner,
|
||||||
|
workspace: {
|
||||||
|
create: {
|
||||||
|
name: username,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function createUser(username: string, password: string) {
|
export async function createUser(username: string, password: string) {
|
||||||
|
const existCount = await prisma.user.count({
|
||||||
|
where: {
|
||||||
|
username,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (existCount > 0) {
|
||||||
|
throw new Error('User already exists');
|
||||||
|
}
|
||||||
|
|
||||||
await prisma.user.create({
|
await prisma.user.create({
|
||||||
data: {
|
data: {
|
||||||
username,
|
username,
|
||||||
password: await hashPassword(password),
|
password: await hashPassword(password),
|
||||||
role: 'normal',
|
role: ROLES.user,
|
||||||
|
workspaces: {
|
||||||
|
create: [
|
||||||
|
{
|
||||||
|
role: ROLES.owner,
|
||||||
|
workspace: {
|
||||||
|
create: {
|
||||||
|
name: username,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -40,6 +75,14 @@ export async function authUser(username: string, password: string) {
|
|||||||
username,
|
username,
|
||||||
password: await hashPassword(password),
|
password: await hashPassword(password),
|
||||||
},
|
},
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
username: true,
|
||||||
|
role: true,
|
||||||
|
createdAt: true,
|
||||||
|
updatedAt: true,
|
||||||
|
deletedAt: true,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
|
9
src/server/utils/const.ts
Normal file
9
src/server/utils/const.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
export const ROLES = {
|
||||||
|
// System Role
|
||||||
|
admin: 'admin',
|
||||||
|
user: 'user',
|
||||||
|
|
||||||
|
// Workspace Role
|
||||||
|
owner: 'owner',
|
||||||
|
readOnly: 'readOnly',
|
||||||
|
} as const;
|
@ -1,5 +1,5 @@
|
|||||||
import { defineConfig } from "vite";
|
import { defineConfig } from 'vite';
|
||||||
import react from "@vitejs/plugin-react";
|
import react from '@vitejs/plugin-react';
|
||||||
|
|
||||||
// https://vitejs.dev/config/
|
// https://vitejs.dev/config/
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
|
Loading…
Reference in New Issue
Block a user