remove deprecated guard function
This commit is contained in:
parent
007b55f626
commit
822712f4f9
@ -1,7 +1,7 @@
|
||||
import { AdminSidebar } from "@/app/admin/admin-sidebar";
|
||||
import { validateRequest } from "@/server/auth";
|
||||
import { UserRole } from "@/server/auth/permissions";
|
||||
import { guard, is_not } from "@/utils/heimdall";
|
||||
import { IS_NOT, guard } from "@/utils/guard";
|
||||
import { redirect } from "next/navigation";
|
||||
|
||||
interface AdminLayoutProps {
|
||||
@ -14,7 +14,7 @@ export default async function AdminLayout(props: AdminLayoutProps) {
|
||||
const { children } = props;
|
||||
const { user } = await validateRequest();
|
||||
|
||||
if (guard(user, is_not, UserRole.ADMIN)) {
|
||||
if (guard(user, IS_NOT, UserRole.ADMIN)) {
|
||||
redirect("/");
|
||||
}
|
||||
|
||||
|
@ -1,33 +0,0 @@
|
||||
import type { UserRole } from "@/server/auth/permissions";
|
||||
import type { users } from "@/server/db/schema";
|
||||
import type { InferSelectModel } from "drizzle-orm";
|
||||
import type { RegisteredDatabaseUserAttributes } from "lucia";
|
||||
|
||||
// Helpers to improve readability of the guard function
|
||||
export const is = false;
|
||||
export const is_not = true;
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
export function guard(
|
||||
user: RegisteredDatabaseUserAttributes | InferSelectModel<typeof users> | undefined | null,
|
||||
negate: boolean,
|
||||
roleRequirements: UserRole | UserRole[],
|
||||
) {
|
||||
if (!user) {
|
||||
return true; // Guard against unauthenticated users
|
||||
}
|
||||
|
||||
const hasRole = Array.isArray(roleRequirements)
|
||||
? roleRequirements.includes(user?.role as UserRole)
|
||||
: user?.role === roleRequirements;
|
||||
|
||||
return negate ? !hasRole : hasRole;
|
||||
}
|
||||
|
||||
export class PermissionError extends Error {
|
||||
constructor() {
|
||||
super("Du besitzt nicht die erforderlichen Berechtigungen um diese Aktion auszuführen.");
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user