mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-10-01 15:48:23 -05:00
Move app to apps/web-react in pnpm workspace layout
This commit is contained in:
33
apps/web-react/app/modules/permissions/guards.server.ts
Normal file
33
apps/web-react/app/modules/permissions/guards.server.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { requireUser } from "~/features/auth/core/user.server";
|
||||
import type { EntityWithPermissions, Role } from "~/modules/permissions/types";
|
||||
import { hasPermission } from "./utils";
|
||||
|
||||
/**
|
||||
* Checks if a user has the required global role.
|
||||
*
|
||||
* @throws {Response} - Throws a 403 Forbidden response if the user does not have the required role.
|
||||
*/
|
||||
export function requireRole(role: Role) {
|
||||
const user = requireUser();
|
||||
if (!user.roles.includes(role)) {
|
||||
throw new Response("Forbidden", { status: 403 });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a user has the required permission to perform an action on a given entity.
|
||||
*
|
||||
* @throws {Response} - Throws a 403 Forbidden response if the user does not have the required permission.
|
||||
*/
|
||||
export function requirePermission<
|
||||
T extends EntityWithPermissions,
|
||||
K extends keyof T["permissions"],
|
||||
>(obj: T, permission: K) {
|
||||
const user = requireUser();
|
||||
|
||||
if (hasPermission(obj, permission, user)) {
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Response("Forbidden", { status: 403 });
|
||||
}
|
||||
Reference in New Issue
Block a user