Files
neta/server/auth/authorization.ts

46 lines
1.2 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import "server-only";
import type { SessionContext } from "@/server/auth/session";
import type { UserRole } from "@/server/auth/types";
export class AuthorizationError extends Error {
constructor(
message = "Bu işlem için yetkiniz yok.",
public readonly code: "UNAUTHENTICATED" | "FORBIDDEN" | "NOT_FOUND" = "FORBIDDEN",
) {
super(message);
this.name = "AuthorizationError";
}
}
export function assertRole(context: SessionContext | null, allowedRoles: readonly UserRole[]): void {
if (!context) {
throw new AuthorizationError("Oturum gerekli.", "UNAUTHENTICATED");
}
if (!allowedRoles.includes(context.profile.role)) {
throw new AuthorizationError();
}
}
export function assertSameOwner(context: SessionContext | null, ownerAuthUserId: string): void {
if (!context) {
throw new AuthorizationError("Oturum gerekli.", "UNAUTHENTICATED");
}
if (context.user.id !== ownerAuthUserId) {
throw new AuthorizationError("Kaynak bulunamadı.", "NOT_FOUND");
}
}
export function assertEnabledUser(context: SessionContext | null): void {
if (!context) {
throw new AuthorizationError("Oturum gerekli.", "UNAUTHENTICATED");
}
if (context.profile.disabled) {
throw new AuthorizationError();
}
}