ich geh behindert
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
"use server";
|
||||
|
||||
import { lucia, validateRequest } from "@/server/auth";
|
||||
import { AuthenticationError } from "@/utils/errors";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
export async function logout(path?: string) {
|
||||
const { session } = await validateRequest();
|
||||
|
||||
if (!session) {
|
||||
throw new AuthenticationError();
|
||||
}
|
||||
|
||||
try {
|
||||
await lucia.invalidateSession(session.id);
|
||||
} catch (error) {
|
||||
throw new AuthenticationError();
|
||||
}
|
||||
|
||||
const sessionCookie = lucia.createBlankSessionCookie();
|
||||
cookies().set(sessionCookie.name, sessionCookie.value, sessionCookie.attributes);
|
||||
|
||||
revalidatePath(path ?? "/");
|
||||
}
|
||||
269
LEGACY-torben_frontend/src/server/actions/printJobs.ts
Normal file
269
LEGACY-torben_frontend/src/server/actions/printJobs.ts
Normal file
@@ -0,0 +1,269 @@
|
||||
"use server";
|
||||
|
||||
import { validateRequest } from "@/server/auth";
|
||||
import { UserRole } from "@/server/auth/permissions";
|
||||
import { db } from "@/server/db";
|
||||
import { printJobs, users } from "@/server/db/schema";
|
||||
import { PermissionError } from "@/utils/errors";
|
||||
import { IS, guard } from "@/utils/guard";
|
||||
import { type InferInsertModel, eq } from "drizzle-orm";
|
||||
import { revalidatePath } from "next/cache";
|
||||
|
||||
export async function createPrintJob(printJob: InferInsertModel<typeof printJobs>) {
|
||||
const { user } = await validateRequest();
|
||||
|
||||
if (guard(user, IS, UserRole.GUEST)) {
|
||||
throw new PermissionError();
|
||||
}
|
||||
|
||||
const dbUser = await db.query.users.findFirst({
|
||||
// biome-ignore lint/style/noNonNullAssertion: guard already checks against null
|
||||
where: eq(users.id, user!.id),
|
||||
});
|
||||
|
||||
if (guard(dbUser, IS, UserRole.GUEST)) {
|
||||
throw new PermissionError();
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await db.insert(printJobs).values(printJob).returning({
|
||||
jobId: printJobs.id,
|
||||
});
|
||||
|
||||
return result[0].jobId;
|
||||
} catch (error) {
|
||||
throw new Error("Druckauftrag konnte nicht hinzugefügt werden.");
|
||||
}
|
||||
}
|
||||
|
||||
/* async function updatePrintJob(jobId: string, printJob: InferInsertModel<typeof printJobs>) {
|
||||
const { user } = await validateRequest();
|
||||
|
||||
if (guard(user, is, UserRole.GUEST)) {
|
||||
throw new PermissionError();
|
||||
}
|
||||
|
||||
const dbUser = await db.query.users.findFirst({
|
||||
// biome-ignore lint/style/noNonNullAssertion: guard already checks against null
|
||||
where: eq(users.id, user!.id),
|
||||
});
|
||||
|
||||
if (guard(dbUser, is, UserRole.GUEST)) {
|
||||
throw new PermissionError();
|
||||
}
|
||||
|
||||
await db.update(printJobs).set(printJob).where(eq(printJobs.id, jobId));
|
||||
} */
|
||||
|
||||
export async function abortPrintJob(jobId: string, reason: string) {
|
||||
const { user } = await validateRequest();
|
||||
|
||||
if (guard(user, IS, UserRole.GUEST)) {
|
||||
throw new PermissionError();
|
||||
}
|
||||
|
||||
const dbUser = await db.query.users.findFirst({
|
||||
// biome-ignore lint/style/noNonNullAssertion: guard already checks against null
|
||||
where: eq(users.id, user!.id),
|
||||
});
|
||||
|
||||
if (guard(dbUser, IS, UserRole.GUEST)) {
|
||||
throw new PermissionError();
|
||||
}
|
||||
|
||||
// Get the print job
|
||||
const printJob = await db.query.printJobs.findFirst({
|
||||
where: eq(printJobs.id, jobId),
|
||||
});
|
||||
|
||||
if (!printJob) {
|
||||
throw new Error("Druckauftrag nicht gefunden");
|
||||
}
|
||||
|
||||
// Check if the print job is already aborted or completed
|
||||
if (printJob.aborted) {
|
||||
throw new Error("Druckauftrag wurde bereits abgebrochen");
|
||||
}
|
||||
|
||||
if (new Date(printJob.startAt).getTime() + printJob.durationInMinutes * 60 * 1000 < Date.now()) {
|
||||
throw new Error("Druckauftrag ist bereits abgeschlossen");
|
||||
}
|
||||
|
||||
// Check if user is the owner of the print job
|
||||
// biome-ignore lint/style/noNonNullAssertion: guard already checks against null
|
||||
if (printJob.userId !== dbUser!.id || dbUser!.role !== UserRole.ADMIN) {
|
||||
throw new PermissionError();
|
||||
}
|
||||
|
||||
// Get duration in minutes since startAt
|
||||
const duration = Math.floor((Date.now() - new Date(printJob.startAt).getTime()) / 1000 / 60);
|
||||
|
||||
await db
|
||||
.update(printJobs)
|
||||
.set({
|
||||
aborted: true,
|
||||
abortReason: reason,
|
||||
durationInMinutes: duration,
|
||||
comments: `${printJob.comments}\n\n---${dbUser?.username}: Druckauftrag abgebrochen`,
|
||||
})
|
||||
.where(eq(printJobs.id, jobId));
|
||||
|
||||
revalidatePath("/");
|
||||
}
|
||||
|
||||
export async function earlyFinishPrintJob(jobId: string) {
|
||||
const { user } = await validateRequest();
|
||||
|
||||
if (guard(user, IS, UserRole.GUEST)) {
|
||||
throw new PermissionError();
|
||||
}
|
||||
|
||||
const dbUser = await db.query.users.findFirst({
|
||||
// biome-ignore lint/style/noNonNullAssertion: guard already checks against null
|
||||
where: eq(users.id, user!.id),
|
||||
});
|
||||
|
||||
if (guard(dbUser, IS, UserRole.GUEST)) {
|
||||
throw new PermissionError();
|
||||
}
|
||||
|
||||
// Get the print job
|
||||
const printJob = await db.query.printJobs.findFirst({
|
||||
where: eq(printJobs.id, jobId),
|
||||
});
|
||||
|
||||
if (!printJob) {
|
||||
throw new Error("Druckauftrag nicht gefunden");
|
||||
}
|
||||
|
||||
// Check if the print job is already aborted or completed
|
||||
if (printJob.aborted) {
|
||||
throw new Error("Druckauftrag wurde bereits abgebrochen");
|
||||
}
|
||||
|
||||
if (new Date(printJob.startAt).getTime() + printJob.durationInMinutes * 60 * 1000 < Date.now()) {
|
||||
throw new Error("Druckauftrag ist bereits abgeschlossen");
|
||||
}
|
||||
|
||||
// Check if user is the owner of the print job
|
||||
// biome-ignore lint/style/noNonNullAssertion: guard already checks against null
|
||||
if (printJob.userId !== dbUser!.id || dbUser!.role !== UserRole.ADMIN) {
|
||||
throw new PermissionError();
|
||||
}
|
||||
|
||||
// Get duration in minutes since startAt
|
||||
const duration = Math.floor((Date.now() - new Date(printJob.startAt).getTime()) / 1000 / 60);
|
||||
|
||||
await db
|
||||
.update(printJobs)
|
||||
.set({
|
||||
durationInMinutes: duration,
|
||||
comments: `${printJob.comments}\n\n---${dbUser?.username}: Druckauftrag vorzeitig abgeschlossen`,
|
||||
})
|
||||
.where(eq(printJobs.id, jobId));
|
||||
|
||||
revalidatePath("/");
|
||||
}
|
||||
|
||||
export async function extendPrintJob(jobId: string, minutes: number, hours: number) {
|
||||
const { user } = await validateRequest();
|
||||
|
||||
if (guard(user, IS, UserRole.GUEST)) {
|
||||
throw new PermissionError();
|
||||
}
|
||||
|
||||
const dbUser = await db.query.users.findFirst({
|
||||
// biome-ignore lint/style/noNonNullAssertion: guard already checks against null
|
||||
where: eq(users.id, user!.id),
|
||||
});
|
||||
|
||||
if (guard(dbUser, IS, UserRole.GUEST)) {
|
||||
throw new PermissionError();
|
||||
}
|
||||
|
||||
// Get the print job
|
||||
const printJob = await db.query.printJobs.findFirst({
|
||||
where: eq(printJobs.id, jobId),
|
||||
});
|
||||
|
||||
if (!printJob) {
|
||||
throw new Error("Druckauftrag nicht gefunden");
|
||||
}
|
||||
|
||||
// Check if the print job is already aborted or completed
|
||||
if (printJob.aborted) {
|
||||
throw new Error("Druckauftrag wurde bereits abgebrochen");
|
||||
}
|
||||
|
||||
if (new Date(printJob.startAt).getTime() + printJob.durationInMinutes * 60 * 1000 < Date.now()) {
|
||||
throw new Error("Druckauftrag ist bereits abgeschlossen");
|
||||
}
|
||||
|
||||
// Check if user is the owner of the print job
|
||||
// biome-ignore lint/style/noNonNullAssertion: guard already checks against null
|
||||
if (printJob.userId !== dbUser!.id || dbUser!.role !== UserRole.ADMIN) {
|
||||
throw new PermissionError();
|
||||
}
|
||||
|
||||
const duration = minutes + hours * 60;
|
||||
|
||||
await db
|
||||
.update(printJobs)
|
||||
.set({
|
||||
durationInMinutes: printJob.durationInMinutes + duration,
|
||||
comments: `${printJob.comments}\n\n---${dbUser?.username}: Verlängert um ${hours} Stunden und ${minutes} Minuten`,
|
||||
})
|
||||
.where(eq(printJobs.id, jobId));
|
||||
|
||||
revalidatePath("/");
|
||||
}
|
||||
|
||||
export async function updatePrintComments(jobId: string, comments: string) {
|
||||
const { user } = await validateRequest();
|
||||
|
||||
if (guard(user, IS, UserRole.GUEST)) {
|
||||
throw new PermissionError();
|
||||
}
|
||||
|
||||
const dbUser = await db.query.users.findFirst({
|
||||
// biome-ignore lint/style/noNonNullAssertion: guard already checks against null
|
||||
where: eq(users.id, user!.id),
|
||||
});
|
||||
|
||||
if (guard(dbUser, IS, UserRole.GUEST)) {
|
||||
throw new PermissionError();
|
||||
}
|
||||
|
||||
// Get the print job
|
||||
const printJob = await db.query.printJobs.findFirst({
|
||||
where: eq(printJobs.id, jobId),
|
||||
});
|
||||
|
||||
if (!printJob) {
|
||||
throw new Error("Druckauftrag nicht gefunden");
|
||||
}
|
||||
|
||||
// Check if the print job is already aborted or completed
|
||||
if (printJob.aborted) {
|
||||
throw new Error("Druckauftrag wurde bereits abgebrochen");
|
||||
}
|
||||
|
||||
if (new Date(printJob.startAt).getTime() + printJob.durationInMinutes * 60 * 1000 < Date.now()) {
|
||||
throw new Error("Druckauftrag ist bereits abgeschlossen");
|
||||
}
|
||||
|
||||
// Check if user is the owner of the print job
|
||||
// biome-ignore lint/style/noNonNullAssertion: guard already checks against null
|
||||
if (printJob.userId !== dbUser!.id || dbUser!.role !== UserRole.ADMIN) {
|
||||
throw new PermissionError();
|
||||
}
|
||||
|
||||
await db
|
||||
.update(printJobs)
|
||||
.set({
|
||||
comments,
|
||||
})
|
||||
.where(eq(printJobs.id, jobId));
|
||||
|
||||
revalidatePath("/");
|
||||
}
|
||||
107
LEGACY-torben_frontend/src/server/actions/printers.ts
Normal file
107
LEGACY-torben_frontend/src/server/actions/printers.ts
Normal file
@@ -0,0 +1,107 @@
|
||||
"use server";
|
||||
|
||||
import { validateRequest } from "@/server/auth";
|
||||
import { UserRole } from "@/server/auth/permissions";
|
||||
import { db } from "@/server/db";
|
||||
import { printers, users } from "@/server/db/schema";
|
||||
import { PermissionError } from "@/utils/errors";
|
||||
import { IS_NOT, guard } from "@/utils/guard";
|
||||
import { type InferInsertModel, eq } from "drizzle-orm";
|
||||
import { revalidatePath } from "next/cache";
|
||||
|
||||
export async function createPrinter(printer: InferInsertModel<typeof printers>) {
|
||||
const { user } = await validateRequest();
|
||||
|
||||
if (guard(user, IS_NOT, UserRole.ADMIN)) {
|
||||
throw new PermissionError();
|
||||
}
|
||||
|
||||
const dbUser = await db.query.users.findFirst({
|
||||
// biome-ignore lint/style/noNonNullAssertion: guard already checks against null
|
||||
where: eq(users.id, user!.id),
|
||||
});
|
||||
|
||||
if (guard(dbUser, IS_NOT, UserRole.ADMIN)) {
|
||||
throw new PermissionError();
|
||||
}
|
||||
|
||||
if (!printer) {
|
||||
throw new Error("Druckerdaten sind erforderlich.");
|
||||
}
|
||||
|
||||
try {
|
||||
await db.insert(printers).values(printer);
|
||||
} catch (error) {
|
||||
throw new Error("Drucker konnte nicht hinzugefügt werden.");
|
||||
}
|
||||
|
||||
revalidatePath("/");
|
||||
}
|
||||
|
||||
export async function updatePrinter(id: string, data: InferInsertModel<typeof printers>) {
|
||||
const { user } = await validateRequest();
|
||||
|
||||
if (guard(user, IS_NOT, UserRole.ADMIN)) {
|
||||
throw new PermissionError();
|
||||
}
|
||||
|
||||
const dbUser = await db.query.users.findFirst({
|
||||
// biome-ignore lint/style/noNonNullAssertion: guard already checks against null
|
||||
where: eq(users.id, user!.id),
|
||||
});
|
||||
|
||||
if (guard(dbUser, IS_NOT, UserRole.ADMIN)) {
|
||||
throw new PermissionError();
|
||||
}
|
||||
|
||||
if (!data) {
|
||||
throw new Error("Druckerdaten sind erforderlich.");
|
||||
}
|
||||
|
||||
try {
|
||||
await db.update(printers).set(data).where(eq(printers.id, id));
|
||||
} catch (error) {
|
||||
throw new Error("Drucker konnte nicht aktualisiert werden.");
|
||||
}
|
||||
|
||||
revalidatePath("/");
|
||||
}
|
||||
|
||||
export async function deletePrinter(id: string) {
|
||||
const { user } = await validateRequest();
|
||||
|
||||
if (guard(user, IS_NOT, UserRole.ADMIN)) {
|
||||
throw new PermissionError();
|
||||
}
|
||||
|
||||
const dbUser = await db.query.users.findFirst({
|
||||
// biome-ignore lint/style/noNonNullAssertion: guard already checks against null
|
||||
where: eq(users.id, user!.id),
|
||||
});
|
||||
|
||||
if (guard(dbUser, IS_NOT, UserRole.ADMIN)) {
|
||||
throw new PermissionError();
|
||||
}
|
||||
|
||||
try {
|
||||
await db.delete(printers).where(eq(printers.id, id));
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
throw new Error(error.message);
|
||||
}
|
||||
throw new Error("Ein unbekannter Fehler ist aufgetreten.");
|
||||
}
|
||||
|
||||
revalidatePath("/");
|
||||
}
|
||||
|
||||
export async function getPrinters() {
|
||||
return await db.query.printers.findMany({
|
||||
with: {
|
||||
printJobs: {
|
||||
limit: 1,
|
||||
orderBy: (printJobs, { desc }) => [desc(printJobs.startAt)],
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
7
LEGACY-torben_frontend/src/server/actions/timer.ts
Normal file
7
LEGACY-torben_frontend/src/server/actions/timer.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
"use server";
|
||||
|
||||
import { revalidatePath } from "next/cache";
|
||||
|
||||
export async function revalidate() {
|
||||
revalidatePath("/");
|
||||
}
|
||||
48
LEGACY-torben_frontend/src/server/actions/user/delete.ts
Normal file
48
LEGACY-torben_frontend/src/server/actions/user/delete.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
"use server";
|
||||
|
||||
import { validateRequest } from "@/server/auth";
|
||||
import { UserRole } from "@/server/auth/permissions";
|
||||
import { db } from "@/server/db";
|
||||
import { users } from "@/server/db/schema";
|
||||
import { PermissionError } from "@/utils/errors";
|
||||
import { IS, IS_NOT, guard } from "@/utils/guard";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { revalidatePath } from "next/cache";
|
||||
|
||||
/**
|
||||
* Deletes a user from the database
|
||||
* @param userId User ID to delete
|
||||
* @param path Path to revalidate
|
||||
*/
|
||||
export async function deleteUser(userId: string, path?: string) {
|
||||
const { user } = await validateRequest();
|
||||
|
||||
if (guard(user, IS_NOT, UserRole.ADMIN)) {
|
||||
throw new PermissionError();
|
||||
}
|
||||
|
||||
const dbUser = await db.query.users.findFirst({
|
||||
// biome-ignore lint/style/noNonNullAssertion: guard already checks against null
|
||||
where: eq(users.id, user!.id),
|
||||
});
|
||||
|
||||
if (guard(dbUser, IS_NOT, UserRole.ADMIN)) {
|
||||
throw new PermissionError();
|
||||
}
|
||||
|
||||
const targetUser = await db.query.users.findFirst({
|
||||
where: eq(users.id, userId),
|
||||
});
|
||||
|
||||
if (!targetUser) {
|
||||
throw new Error("Benutzer nicht gefunden");
|
||||
}
|
||||
|
||||
if (guard(targetUser, IS, UserRole.ADMIN)) {
|
||||
throw new Error("Kann keinen Admin löschen");
|
||||
}
|
||||
|
||||
await db.delete(users).where(eq(users.id, userId));
|
||||
|
||||
revalidatePath(path ?? "/admin/users");
|
||||
}
|
||||
37
LEGACY-torben_frontend/src/server/actions/user/update.ts
Normal file
37
LEGACY-torben_frontend/src/server/actions/user/update.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import type { formSchema } from "@/app/admin/users/form";
|
||||
import { validateRequest } from "@/server/auth";
|
||||
import { UserRole } from "@/server/auth/permissions";
|
||||
import { db } from "@/server/db";
|
||||
import { users } from "@/server/db/schema";
|
||||
import { PermissionError } from "@/utils/errors";
|
||||
import { IS_NOT, guard } from "@/utils/guard";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import type { z } from "zod";
|
||||
|
||||
/**
|
||||
* Updates a user in the database
|
||||
* @param userId User ID to update
|
||||
* @param data Updated user data
|
||||
* @param path Path to revalidate
|
||||
*/
|
||||
export async function updateUser(userId: string, data: z.infer<typeof formSchema>, path?: string) {
|
||||
const { user } = await validateRequest();
|
||||
|
||||
if (guard(user, IS_NOT, UserRole.ADMIN)) {
|
||||
throw new PermissionError();
|
||||
}
|
||||
|
||||
const dbUser = await db.query.users.findFirst({
|
||||
// biome-ignore lint/style/noNonNullAssertion: guard already checks against null
|
||||
where: eq(users.id, user!.id),
|
||||
});
|
||||
|
||||
if (guard(dbUser, IS_NOT, UserRole.ADMIN)) {
|
||||
throw new PermissionError();
|
||||
}
|
||||
|
||||
await db.update(users).set(data).where(eq(users.id, userId));
|
||||
|
||||
revalidatePath(path ?? "/admin/users");
|
||||
}
|
||||
72
LEGACY-torben_frontend/src/server/actions/users.ts
Normal file
72
LEGACY-torben_frontend/src/server/actions/users.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
"use server";
|
||||
|
||||
import type { formSchema } from "@/app/admin/users/form";
|
||||
import { validateRequest } from "@/server/auth";
|
||||
import { UserRole } from "@/server/auth/permissions";
|
||||
import { db } from "@/server/db";
|
||||
import { users } from "@/server/db/schema";
|
||||
import { PermissionError } from "@/utils/errors";
|
||||
import { IS, IS_NOT, guard } from "@/utils/guard";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import type { z } from "zod";
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
export async function updateUser(userId: string, data: z.infer<typeof formSchema>) {
|
||||
const { user } = await validateRequest();
|
||||
|
||||
if (guard(user, IS_NOT, UserRole.ADMIN)) {
|
||||
throw new PermissionError();
|
||||
}
|
||||
|
||||
const dbUser = await db.query.users.findFirst({
|
||||
// biome-ignore lint/style/noNonNullAssertion: guard already checks against null
|
||||
where: eq(users.id, user!.id),
|
||||
});
|
||||
|
||||
if (guard(dbUser, IS_NOT, UserRole.ADMIN)) {
|
||||
throw new PermissionError();
|
||||
}
|
||||
|
||||
await db.update(users).set(data).where(eq(users.id, userId));
|
||||
|
||||
revalidatePath("/admin/users");
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
export async function deleteUser(userId: string) {
|
||||
const { user } = await validateRequest();
|
||||
|
||||
if (guard(user, IS_NOT, UserRole.ADMIN)) {
|
||||
throw new PermissionError();
|
||||
}
|
||||
|
||||
const dbUser = await db.query.users.findFirst({
|
||||
// biome-ignore lint/style/noNonNullAssertion: guard already checks against null
|
||||
where: eq(users.id, user!.id),
|
||||
});
|
||||
|
||||
if (guard(dbUser, IS_NOT, UserRole.ADMIN)) {
|
||||
throw new PermissionError();
|
||||
}
|
||||
|
||||
const targetUser = await db.query.users.findFirst({
|
||||
where: eq(users.id, userId),
|
||||
});
|
||||
|
||||
if (!targetUser) {
|
||||
throw new Error("Benutzer nicht gefunden");
|
||||
}
|
||||
|
||||
if (guard(targetUser, IS, UserRole.ADMIN)) {
|
||||
throw new Error("Kann keinen Admin löschen");
|
||||
}
|
||||
|
||||
await db.delete(users).where(eq(users.id, userId));
|
||||
|
||||
revalidatePath("/admin/users");
|
||||
}
|
||||
72
LEGACY-torben_frontend/src/server/auth/index.ts
Normal file
72
LEGACY-torben_frontend/src/server/auth/index.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import type { UserRole } from "@/server/auth/permissions";
|
||||
import { db } from "@/server/db";
|
||||
import { sessions, users } from "@/server/db/schema";
|
||||
import { env } from "@/utils/env";
|
||||
import { DrizzleSQLiteAdapter } from "@lucia-auth/adapter-drizzle";
|
||||
import { Lucia, type RegisteredDatabaseUserAttributes, type Session } from "lucia";
|
||||
import { cookies } from "next/headers";
|
||||
import { cache } from "react";
|
||||
|
||||
const adapter = new DrizzleSQLiteAdapter(db, sessions, users);
|
||||
|
||||
export const lucia = new Lucia(adapter, {
|
||||
sessionCookie: {
|
||||
expires: false,
|
||||
attributes: {
|
||||
secure: env.RUNTIME_ENVIRONMENT === "prod",
|
||||
},
|
||||
},
|
||||
getUserAttributes: (attributes) => {
|
||||
return {
|
||||
id: attributes.id,
|
||||
username: attributes.username,
|
||||
displayName: attributes.displayName,
|
||||
email: attributes.email,
|
||||
role: attributes.role,
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export const validateRequest = cache(
|
||||
async (): Promise<{ user: RegisteredDatabaseUserAttributes; session: Session } | { user: null; session: null }> => {
|
||||
const sessionId = cookies().get(lucia.sessionCookieName)?.value ?? null;
|
||||
if (!sessionId) {
|
||||
return {
|
||||
user: null,
|
||||
session: null,
|
||||
};
|
||||
}
|
||||
|
||||
const result = await lucia.validateSession(sessionId);
|
||||
// next.js throws when you attempt to set cookie when rendering page
|
||||
try {
|
||||
if (result.session?.fresh) {
|
||||
const sessionCookie = lucia.createSessionCookie(result.session.id);
|
||||
cookies().set(sessionCookie.name, sessionCookie.value, sessionCookie.attributes);
|
||||
}
|
||||
if (!result.session) {
|
||||
const sessionCookie = lucia.createBlankSessionCookie();
|
||||
cookies().set(sessionCookie.name, sessionCookie.value, sessionCookie.attributes);
|
||||
}
|
||||
} catch {}
|
||||
|
||||
return result as {
|
||||
user: RegisteredDatabaseUserAttributes;
|
||||
session: Session;
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
declare module "lucia" {
|
||||
interface Register {
|
||||
Lucia: typeof Lucia;
|
||||
DatabaseUserAttributes: {
|
||||
id: string;
|
||||
github_id: number;
|
||||
username: string;
|
||||
displayName: string;
|
||||
email: string;
|
||||
role: UserRole;
|
||||
};
|
||||
}
|
||||
}
|
||||
13
LEGACY-torben_frontend/src/server/auth/oauth.ts
Normal file
13
LEGACY-torben_frontend/src/server/auth/oauth.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { env } from "@/utils/env";
|
||||
import { GitHub } from "arctic";
|
||||
|
||||
export const github = new GitHub(env.OAUTH.CLIENT_ID, env.OAUTH.CLIENT_SECRET, {
|
||||
enterpriseDomain: "https://git.i.mercedes-benz.com",
|
||||
});
|
||||
|
||||
export interface GitHubUserResult {
|
||||
id: number;
|
||||
login: string;
|
||||
name: string;
|
||||
email: string;
|
||||
}
|
||||
28
LEGACY-torben_frontend/src/server/auth/permissions.ts
Normal file
28
LEGACY-torben_frontend/src/server/auth/permissions.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import type { RegisteredDatabaseUserAttributes } from "lucia";
|
||||
|
||||
export enum UserRole {
|
||||
ADMIN = "admin",
|
||||
USER = "user",
|
||||
GUEST = "guest",
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
export function hasRole(user: RegisteredDatabaseUserAttributes | null | undefined, role: UserRole) {
|
||||
return user?.role === role;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
export function translateUserRole(role: UserRole) {
|
||||
switch (role) {
|
||||
case UserRole.ADMIN:
|
||||
return "Administrator";
|
||||
case UserRole.USER:
|
||||
return "Benutzer";
|
||||
case UserRole.GUEST:
|
||||
return "Gast";
|
||||
}
|
||||
}
|
||||
7
LEGACY-torben_frontend/src/server/db/index.ts
Normal file
7
LEGACY-torben_frontend/src/server/db/index.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { env } from "@/utils/env";
|
||||
import Database from "better-sqlite3";
|
||||
import { drizzle } from "drizzle-orm/better-sqlite3";
|
||||
import * as schema from "@/server/db/schema";
|
||||
|
||||
const sqlite = new Database(env.DB_PATH);
|
||||
export const db = drizzle(sqlite, { schema });
|
||||
4
LEGACY-torben_frontend/src/server/db/migrate.ts
Normal file
4
LEGACY-torben_frontend/src/server/db/migrate.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { migrate } from "drizzle-orm/better-sqlite3/migrator";
|
||||
import { db } from "@/server/db";
|
||||
|
||||
migrate(db, { migrationsFolder: "./drizzle" });
|
||||
78
LEGACY-torben_frontend/src/server/db/schema.ts
Normal file
78
LEGACY-torben_frontend/src/server/db/schema.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { relations } from "drizzle-orm";
|
||||
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
|
||||
|
||||
// MYP tables
|
||||
export const printers = sqliteTable("printer", {
|
||||
id: text("id")
|
||||
.primaryKey()
|
||||
.$defaultFn(() => crypto.randomUUID()),
|
||||
name: text("name").notNull(),
|
||||
description: text("description").notNull(),
|
||||
status: integer("status").notNull().default(0),
|
||||
});
|
||||
export const printerRelations = relations(printers, ({ many }) => ({
|
||||
printJobs: many(printJobs),
|
||||
}));
|
||||
|
||||
export const printJobs = sqliteTable("printJob", {
|
||||
id: text("id")
|
||||
.primaryKey()
|
||||
.$defaultFn(() => crypto.randomUUID()),
|
||||
printerId: text("printerId")
|
||||
.notNull()
|
||||
.references(() => printers.id, {
|
||||
onDelete: "cascade",
|
||||
}),
|
||||
userId: text("userId")
|
||||
.notNull()
|
||||
.references(() => users.id, {
|
||||
onDelete: "cascade",
|
||||
}),
|
||||
startAt: integer("startAt", { mode: "timestamp_ms" })
|
||||
.notNull()
|
||||
.$defaultFn(() => new Date()),
|
||||
durationInMinutes: integer("durationInMinutes").notNull(),
|
||||
comments: text("comments"),
|
||||
aborted: integer("aborted", { mode: "boolean" }).notNull().default(false),
|
||||
abortReason: text("abortReason"),
|
||||
});
|
||||
export const printJobRelations = relations(printJobs, ({ one }) => ({
|
||||
printer: one(printers, {
|
||||
fields: [printJobs.printerId],
|
||||
references: [printers.id],
|
||||
}),
|
||||
user: one(users, {
|
||||
fields: [printJobs.userId],
|
||||
references: [users.id],
|
||||
}),
|
||||
}));
|
||||
|
||||
// Auth Tables
|
||||
export const users = sqliteTable("user", {
|
||||
id: text("id").notNull().primaryKey(),
|
||||
github_id: integer("github_id").notNull(),
|
||||
username: text("name"),
|
||||
displayName: text("displayName"),
|
||||
email: text("email").notNull(),
|
||||
role: text("role").default("guest"),
|
||||
});
|
||||
export const userRelations = relations(users, ({ many }) => ({
|
||||
printJobs: many(printJobs),
|
||||
sessions: many(sessions),
|
||||
}));
|
||||
|
||||
export const sessions = sqliteTable("session", {
|
||||
id: text("id").notNull().primaryKey(),
|
||||
userId: text("user_id")
|
||||
.notNull()
|
||||
.references(() => users.id, {
|
||||
onDelete: "cascade",
|
||||
}),
|
||||
expiresAt: integer("expires_at").notNull(),
|
||||
});
|
||||
export const sessionRelations = relations(sessions, ({ one }) => ({
|
||||
user: one(users, {
|
||||
fields: [sessions.userId],
|
||||
references: [users.id],
|
||||
}),
|
||||
}));
|
||||
Reference in New Issue
Block a user