Initial commit: page-manager Next.js app with Coolify deploy support
Replace Vercel-specific infra (Cron, Blob) with self-hosted equivalents for deployment to Coolify: in-container cron scheduler (scripts/cron-runner.mjs) and disk-based upload storage served via /api/uploads/[...path]. Add Dockerfile, .dockerignore, and /api/health for container health checks. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
import { cookies } from "next/headers";
|
||||
import { NextResponse } from "next/server";
|
||||
import type { PageRole, User } from "@prisma/client";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { COOKIE_NAME, verifySessionToken } from "@/lib/auth/session";
|
||||
import { ACTION_DENIED_MESSAGE, ACTION_MIN_ROLE, roleAtLeast, type PageAction } from "@/lib/auth/roles";
|
||||
|
||||
export type SessionUser = Pick<User, "id" | "email" | "name" | "avatarUrl" | "globalRole" | "isActive">;
|
||||
|
||||
const USER_SELECT = { id: true, email: true, name: true, avatarUrl: true, globalRole: true, isActive: true } as const;
|
||||
|
||||
/**
|
||||
* The token only proves which account is calling. Role and active status are
|
||||
* always re-read from the database so revoking access takes effect immediately.
|
||||
*/
|
||||
export async function getCurrentUser(): Promise<SessionUser | null> {
|
||||
const token = (await cookies()).get(COOKIE_NAME)?.value;
|
||||
if (!token) return null;
|
||||
const claims = await verifySessionToken(token);
|
||||
if (!claims) return null;
|
||||
const user = await prisma.user.findUnique({ where: { id: claims.userId }, select: USER_SELECT });
|
||||
if (!user || !user.isActive) return null;
|
||||
return user;
|
||||
}
|
||||
|
||||
export const isSuperadmin = (u: Pick<SessionUser, "globalRole">) => u.globalRole === "SUPERADMIN";
|
||||
|
||||
/** Effective role on a page. SUPERADMIN counts as MANAGER everywhere. */
|
||||
export async function pageRoleOf(user: SessionUser, pageId: string): Promise<PageRole | null> {
|
||||
if (isSuperadmin(user)) return "MANAGER";
|
||||
const m = await prisma.pageMember.findUnique({
|
||||
where: { userId_pageId: { userId: user.id, pageId } },
|
||||
select: { role: true },
|
||||
});
|
||||
return m?.role ?? null;
|
||||
}
|
||||
|
||||
/** Page ids the user can see (null = all pages, for SUPERADMIN). */
|
||||
export async function accessiblePageIds(user: SessionUser): Promise<string[] | null> {
|
||||
if (isSuperadmin(user)) return null;
|
||||
const rows = await prisma.pageMember.findMany({ where: { userId: user.id }, select: { pageId: true } });
|
||||
return rows.map((r) => r.pageId);
|
||||
}
|
||||
|
||||
/** Prisma `where` fragment restricting to pages the user can see. */
|
||||
export async function pageScope(user: SessionUser): Promise<{ id?: { in: string[] } }> {
|
||||
const ids = await accessiblePageIds(user);
|
||||
return ids === null ? {} : { id: { in: ids } };
|
||||
}
|
||||
|
||||
export type AuthResult = { ok: true; user: SessionUser } | { ok: false; response: NextResponse };
|
||||
export type PageAuthResult =
|
||||
| { ok: true; user: SessionUser; role: PageRole }
|
||||
| { ok: false; response: NextResponse };
|
||||
|
||||
export async function requireUser(): Promise<AuthResult> {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return { ok: false, response: NextResponse.json({ error: "กรุณาเข้าสู่ระบบใหม่" }, { status: 401 }) };
|
||||
}
|
||||
return { ok: true, user };
|
||||
}
|
||||
|
||||
export async function requireSuperadmin(): Promise<AuthResult> {
|
||||
const auth = await requireUser();
|
||||
if (!auth.ok) return auth;
|
||||
if (!isSuperadmin(auth.user)) {
|
||||
return { ok: false, response: NextResponse.json({ error: "เฉพาะผู้ดูแลระบบเท่านั้น" }, { status: 403 }) };
|
||||
}
|
||||
return auth;
|
||||
}
|
||||
|
||||
/** Every route handler touching page data calls this — the proxy only checks login. */
|
||||
export async function requirePageAction(pageId: string, action: PageAction): Promise<PageAuthResult> {
|
||||
const auth = await requireUser();
|
||||
if (!auth.ok) return auth;
|
||||
const role = await pageRoleOf(auth.user, pageId);
|
||||
if (!roleAtLeast(role, ACTION_MIN_ROLE[action])) {
|
||||
// 404 rather than 403 when the user has no membership at all, so page ids aren't probeable.
|
||||
const status = role ? 403 : 404;
|
||||
const error = role ? ACTION_DENIED_MESSAGE[action] : "ไม่พบเพจ";
|
||||
return { ok: false, response: NextResponse.json({ error }, { status }) };
|
||||
}
|
||||
return { ok: true, user: auth.user, role: role! };
|
||||
}
|
||||
|
||||
export function actorLabel(user: Pick<SessionUser, "name" | "email">): string {
|
||||
return user.name || user.email;
|
||||
}
|
||||
Reference in New Issue
Block a user