feat: add onboarding and user invitations
This commit is contained in:
parent
24351eb897
commit
762cfe2b16
37 changed files with 5208 additions and 384 deletions
40
packages/db/src/migrations/0004_lean_jazinda.sql
Normal file
40
packages/db/src/migrations/0004_lean_jazinda.sql
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
ALTER TABLE "user" ADD COLUMN "must_change_password" boolean DEFAULT false NOT NULL;
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "user_invitation" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"token_hash" text NOT NULL,
|
||||
"token_encrypted" text NOT NULL,
|
||||
"token_iv" text NOT NULL,
|
||||
"created_by_user_id" text,
|
||||
"accepted_user_id" text,
|
||||
"revoked_at" timestamp,
|
||||
"accepted_at" timestamp,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "user_invitation_token_hash_unique" UNIQUE("token_hash")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "user_invitation_role" (
|
||||
"invitation_id" text NOT NULL,
|
||||
"role_id" text NOT NULL,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "user_invitation_role_invitation_id_role_id_pk" PRIMARY KEY("invitation_id","role_id")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "user_invitation" ADD CONSTRAINT "user_invitation_created_by_user_id_user_id_fk" FOREIGN KEY ("created_by_user_id") REFERENCES "public"."user"("id") ON DELETE set null ON UPDATE no action;
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "user_invitation" ADD CONSTRAINT "user_invitation_accepted_user_id_user_id_fk" FOREIGN KEY ("accepted_user_id") REFERENCES "public"."user"("id") ON DELETE set null ON UPDATE no action;
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "user_invitation_role" ADD CONSTRAINT "user_invitation_role_invitation_id_user_invitation_id_fk" FOREIGN KEY ("invitation_id") REFERENCES "public"."user_invitation"("id") ON DELETE cascade ON UPDATE no action;
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "user_invitation_role" ADD CONSTRAINT "user_invitation_role_role_id_role_id_fk" FOREIGN KEY ("role_id") REFERENCES "public"."role"("id") ON DELETE cascade ON UPDATE no action;
|
||||
--> statement-breakpoint
|
||||
CREATE INDEX "user_invitation_created_by_user_id_idx" ON "user_invitation" USING btree ("created_by_user_id");
|
||||
--> statement-breakpoint
|
||||
CREATE INDEX "user_invitation_accepted_user_id_idx" ON "user_invitation" USING btree ("accepted_user_id");
|
||||
--> statement-breakpoint
|
||||
CREATE INDEX "user_invitation_created_at_idx" ON "user_invitation" USING btree ("created_at");
|
||||
--> statement-breakpoint
|
||||
CREATE INDEX "user_invitation_role_invitation_id_idx" ON "user_invitation_role" USING btree ("invitation_id");
|
||||
--> statement-breakpoint
|
||||
CREATE INDEX "user_invitation_role_role_id_idx" ON "user_invitation_role" USING btree ("role_id");
|
||||
1625
packages/db/src/migrations/meta/0004_snapshot.json
Normal file
1625
packages/db/src/migrations/meta/0004_snapshot.json
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -29,6 +29,13 @@
|
|||
"when": 1786104000000,
|
||||
"tag": "0003_add_domain_check_history",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 4,
|
||||
"version": "7",
|
||||
"when": 1786105359203,
|
||||
"tag": "0004_lean_jazinda",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +1,19 @@
|
|||
import { relations } from "drizzle-orm";
|
||||
import { pgTable, text, timestamp, boolean, index, primaryKey } from "drizzle-orm/pg-core";
|
||||
import {
|
||||
pgTable,
|
||||
text,
|
||||
timestamp,
|
||||
boolean,
|
||||
index,
|
||||
primaryKey,
|
||||
} from "drizzle-orm/pg-core";
|
||||
|
||||
export const user = pgTable("user", {
|
||||
id: text("id").primaryKey(),
|
||||
name: text("name").notNull(),
|
||||
email: text("email").notNull().unique(),
|
||||
emailVerified: boolean("email_verified").default(false).notNull(),
|
||||
mustChangePassword: boolean("must_change_password").default(false).notNull(),
|
||||
image: text("image"),
|
||||
createdAt: timestamp("created_at").defaultNow().notNull(),
|
||||
updatedAt: timestamp("updated_at")
|
||||
|
|
@ -131,10 +139,62 @@ export const userRole = pgTable(
|
|||
],
|
||||
);
|
||||
|
||||
export const userInvitation = pgTable(
|
||||
"user_invitation",
|
||||
{
|
||||
id: text("id").primaryKey(),
|
||||
tokenHash: text("token_hash").notNull().unique(),
|
||||
tokenEncrypted: text("token_encrypted").notNull(),
|
||||
tokenIv: text("token_iv").notNull(),
|
||||
createdByUserId: text("created_by_user_id").references(() => user.id, {
|
||||
onDelete: "set null",
|
||||
}),
|
||||
acceptedUserId: text("accepted_user_id").references(() => user.id, {
|
||||
onDelete: "set null",
|
||||
}),
|
||||
revokedAt: timestamp("revoked_at"),
|
||||
acceptedAt: timestamp("accepted_at"),
|
||||
createdAt: timestamp("created_at").defaultNow().notNull(),
|
||||
updatedAt: timestamp("updated_at")
|
||||
.defaultNow()
|
||||
.$onUpdate(() => /* @__PURE__ */ new Date())
|
||||
.notNull(),
|
||||
},
|
||||
(table) => [
|
||||
index("user_invitation_created_by_user_id_idx").on(table.createdByUserId),
|
||||
index("user_invitation_accepted_user_id_idx").on(table.acceptedUserId),
|
||||
index("user_invitation_created_at_idx").on(table.createdAt),
|
||||
],
|
||||
);
|
||||
|
||||
export const userInvitationRole = pgTable(
|
||||
"user_invitation_role",
|
||||
{
|
||||
invitationId: text("invitation_id")
|
||||
.notNull()
|
||||
.references(() => userInvitation.id, { onDelete: "cascade" }),
|
||||
roleId: text("role_id")
|
||||
.notNull()
|
||||
.references(() => role.id, { onDelete: "cascade" }),
|
||||
createdAt: timestamp("created_at").defaultNow().notNull(),
|
||||
},
|
||||
(table) => [
|
||||
primaryKey({ columns: [table.invitationId, table.roleId] }),
|
||||
index("user_invitation_role_invitation_id_idx").on(table.invitationId),
|
||||
index("user_invitation_role_role_id_idx").on(table.roleId),
|
||||
],
|
||||
);
|
||||
|
||||
export const userRelations = relations(user, ({ many }) => ({
|
||||
sessions: many(session),
|
||||
accounts: many(account),
|
||||
roles: many(userRole),
|
||||
createdInvitations: many(userInvitation, {
|
||||
relationName: "invitationCreator",
|
||||
}),
|
||||
acceptedInvitations: many(userInvitation, {
|
||||
relationName: "invitationRecipient",
|
||||
}),
|
||||
}));
|
||||
|
||||
export const sessionRelations = relations(session, ({ one }) => ({
|
||||
|
|
@ -181,3 +241,34 @@ export const userRoleRelations = relations(userRole, ({ one }) => ({
|
|||
references: [role.id],
|
||||
}),
|
||||
}));
|
||||
|
||||
export const userInvitationRelations = relations(
|
||||
userInvitation,
|
||||
({ one, many }) => ({
|
||||
createdBy: one(user, {
|
||||
fields: [userInvitation.createdByUserId],
|
||||
references: [user.id],
|
||||
relationName: "invitationCreator",
|
||||
}),
|
||||
acceptedUser: one(user, {
|
||||
fields: [userInvitation.acceptedUserId],
|
||||
references: [user.id],
|
||||
relationName: "invitationRecipient",
|
||||
}),
|
||||
roles: many(userInvitationRole),
|
||||
}),
|
||||
);
|
||||
|
||||
export const userInvitationRoleRelations = relations(
|
||||
userInvitationRole,
|
||||
({ one }) => ({
|
||||
invitation: one(userInvitation, {
|
||||
fields: [userInvitationRole.invitationId],
|
||||
references: [userInvitation.id],
|
||||
}),
|
||||
role: one(role, {
|
||||
fields: [userInvitationRole.roleId],
|
||||
references: [role.id],
|
||||
}),
|
||||
}),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -4,25 +4,13 @@ dotenv.config({
|
|||
path: new URL("../../../apps/server/.env", import.meta.url).pathname,
|
||||
});
|
||||
|
||||
import { db } from "./index";
|
||||
import { seed, seedRolesAndPermissions } from "./seed";
|
||||
import { seedRolesAndPermissions } from "./seed";
|
||||
|
||||
async function seedIfEmpty() {
|
||||
const existingUser = await db.query.user.findFirst({
|
||||
columns: {
|
||||
id: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (existingUser) {
|
||||
await seedRolesAndPermissions(existingUser.id);
|
||||
console.info("Database already has users; ensured default roles and permissions.");
|
||||
return;
|
||||
}
|
||||
|
||||
await seed();
|
||||
console.info("Seeded auth admin and monitoring dashboard sample data.");
|
||||
console.info("Admin login: admin@minmon.local / admin12345");
|
||||
await seedRolesAndPermissions();
|
||||
console.info(
|
||||
"Ensured default roles and permissions. First-run setup will create the administrator.",
|
||||
);
|
||||
}
|
||||
|
||||
seedIfEmpty()
|
||||
|
|
|
|||
|
|
@ -7,8 +7,21 @@ dotenv.config({
|
|||
});
|
||||
|
||||
import { db } from "./index";
|
||||
import { account, permission, role, rolePermission, user, userRole } from "./schema/auth";
|
||||
import { domain, domainCheckHistory, note, server, service } from "./schema/monitoring";
|
||||
import {
|
||||
account,
|
||||
permission,
|
||||
role,
|
||||
rolePermission,
|
||||
user,
|
||||
userRole,
|
||||
} from "./schema/auth";
|
||||
import {
|
||||
domain,
|
||||
domainCheckHistory,
|
||||
note,
|
||||
server,
|
||||
service,
|
||||
} from "./schema/monitoring";
|
||||
|
||||
const now = new Date();
|
||||
const earlierToday = new Date(now.getTime() - 45 * 60 * 1000);
|
||||
|
|
@ -30,7 +43,8 @@ const servers = [
|
|||
operatingSystem: "Ubuntu 24.04 LTS",
|
||||
location: "Singapore",
|
||||
provider: "DigitalOcean",
|
||||
description: "Main production server hosting the dashboard web app, API, and reverse proxy.",
|
||||
description:
|
||||
"Main production server hosting the dashboard web app, API, and reverse proxy.",
|
||||
status: "active" as const,
|
||||
},
|
||||
{
|
||||
|
|
@ -52,7 +66,8 @@ const servers = [
|
|||
operatingSystem: "Ubuntu 22.04 LTS",
|
||||
location: "Campus Lab",
|
||||
provider: "University Lab",
|
||||
description: "Used for student practice and lecturer demonstration in class.",
|
||||
description:
|
||||
"Used for student practice and lecturer demonstration in class.",
|
||||
status: "inactive" as const,
|
||||
},
|
||||
];
|
||||
|
|
@ -67,7 +82,8 @@ const services = [
|
|||
externalPort: 443,
|
||||
protocol: "https" as const,
|
||||
status: "active" as const,
|
||||
description: "Frontend application for the private server management dashboard.",
|
||||
description:
|
||||
"Frontend application for the private server management dashboard.",
|
||||
},
|
||||
{
|
||||
id: "service_dashboard_api",
|
||||
|
|
@ -89,7 +105,8 @@ const services = [
|
|||
externalPort: 25,
|
||||
protocol: "tcp" as const,
|
||||
status: "maintenance" as const,
|
||||
description: "Mail service used as a sample SMTP gateway for the assignment.",
|
||||
description:
|
||||
"Mail service used as a sample SMTP gateway for the assignment.",
|
||||
},
|
||||
{
|
||||
id: "service_lab_proxy",
|
||||
|
|
@ -100,7 +117,8 @@ const services = [
|
|||
externalPort: 80,
|
||||
protocol: "http" as const,
|
||||
status: "down" as const,
|
||||
description: "Reverse proxy for local practice deployments in the campus lab.",
|
||||
description:
|
||||
"Reverse proxy for local practice deployments in the campus lab.",
|
||||
},
|
||||
];
|
||||
|
||||
|
|
@ -151,7 +169,8 @@ const domains = [
|
|||
resolutionStatus: "UNRESOLVED" as const,
|
||||
lastCheckedAt: yesterday,
|
||||
remarks: "Lab VM is powered off outside teaching hours.",
|
||||
lastCheckMessage: "DNS lookup failed because the host could not be resolved.",
|
||||
lastCheckMessage:
|
||||
"DNS lookup failed because the host could not be resolved.",
|
||||
},
|
||||
];
|
||||
|
||||
|
|
@ -160,7 +179,8 @@ const notes = [
|
|||
id: "note_prod_backup",
|
||||
serverId: "server_prod_1",
|
||||
title: "Weekly backup reminder",
|
||||
content: "Verify the PostgreSQL backup every Friday before the lecturer demo session.",
|
||||
content:
|
||||
"Verify the PostgreSQL backup every Friday before the lecturer demo session.",
|
||||
category: "maintenance",
|
||||
},
|
||||
{
|
||||
|
|
@ -175,14 +195,16 @@ const notes = [
|
|||
id: "note_mail_dns",
|
||||
serverId: "server_mail_1",
|
||||
title: "Update stale A record",
|
||||
content: "Point mail.minmon.dev to the current mail server before final presentation week.",
|
||||
content:
|
||||
"Point mail.minmon.dev to the current mail server before final presentation week.",
|
||||
category: "todo",
|
||||
},
|
||||
{
|
||||
id: "note_lab_status",
|
||||
serverId: "server_lab_1",
|
||||
title: "Lab VM inactive",
|
||||
content: "The campus lab VM is intentionally inactive outside class hours, so related checks may fail.",
|
||||
content:
|
||||
"The campus lab VM is intentionally inactive outside class hours, so related checks may fail.",
|
||||
category: "incident",
|
||||
},
|
||||
];
|
||||
|
|
@ -202,7 +224,8 @@ async function seedAuthAdmin() {
|
|||
id: adminUser.id,
|
||||
name: adminUser.name,
|
||||
email: adminUser.email,
|
||||
emailVerified: true,
|
||||
emailVerified: false,
|
||||
mustChangePassword: false,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
});
|
||||
|
|
@ -221,42 +244,54 @@ async function seedAuthAdmin() {
|
|||
}
|
||||
|
||||
export async function seedRolesAndPermissions(adminUserId?: string) {
|
||||
await db.insert(permission).values(
|
||||
permissions.map((name) => ({
|
||||
id: name,
|
||||
name,
|
||||
description: `Allows ${name.replace(".", " ")}`,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
})),
|
||||
).onConflictDoNothing();
|
||||
|
||||
await db.insert(role).values(
|
||||
roleDefinitions.map((item) => ({
|
||||
id: item.id,
|
||||
name: item.name,
|
||||
description: item.description,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
})),
|
||||
).onConflictDoNothing();
|
||||
|
||||
await db.insert(rolePermission).values(
|
||||
roleDefinitions.flatMap((item) =>
|
||||
item.permissions.map((permissionName) => ({
|
||||
roleId: item.id,
|
||||
permissionId: permissionName,
|
||||
await db
|
||||
.insert(permission)
|
||||
.values(
|
||||
permissions.map((name) => ({
|
||||
id: name,
|
||||
name,
|
||||
description: `Allows ${name.replace(".", " ")}`,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
})),
|
||||
),
|
||||
).onConflictDoNothing();
|
||||
)
|
||||
.onConflictDoNothing();
|
||||
|
||||
await db
|
||||
.insert(role)
|
||||
.values(
|
||||
roleDefinitions.map((item) => ({
|
||||
id: item.id,
|
||||
name: item.name,
|
||||
description: item.description,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
})),
|
||||
)
|
||||
.onConflictDoNothing();
|
||||
|
||||
await db
|
||||
.insert(rolePermission)
|
||||
.values(
|
||||
roleDefinitions.flatMap((item) =>
|
||||
item.permissions.map((permissionName) => ({
|
||||
roleId: item.id,
|
||||
permissionId: permissionName,
|
||||
createdAt: now,
|
||||
})),
|
||||
),
|
||||
)
|
||||
.onConflictDoNothing();
|
||||
|
||||
if (adminUserId) {
|
||||
await db.insert(userRole).values({
|
||||
userId: adminUserId,
|
||||
roleId: "role_admin",
|
||||
createdAt: now,
|
||||
}).onConflictDoNothing();
|
||||
await db
|
||||
.insert(userRole)
|
||||
.values({
|
||||
userId: adminUserId,
|
||||
roleId: "role_admin",
|
||||
createdAt: now,
|
||||
})
|
||||
.onConflictDoNothing();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue