feat(admin): add group-based access scoping for routes and logs

Introduce optional route groups so non-super admins can be delegated
edit/view access to a subset of routes and their send logs.

- add Group model and Route.groupId
- add groups.ts (load/save groups, resolveScope, permission helpers)
- scope /api/routes and /api/logs by the caller's accessible groups;
  add /api/me and /api/groups (group management is super-admin only)
- require a groupId on every route in validateRoutes
- allow group admins (not just super admins) to sign in to the console
This commit is contained in:
RhenCloud committed 2026-08-02 07:39:35 +08:00
1 parent 225d5b015e
commit fc243811f0
4 files changed
+216 -20

No files matched your search

+6 -3
View File
@@ -1,7 +1,8 @@
import { Hono } from "hono";
import { getOAuthURL, handleOAuthCallback } from "./github-oauth";
import { removeToken, saveDiscordLink } from "./token-store";
import { isAdminUser, createAdminSession, adminCookie } from "./admin-session";
import { createAdminSession, adminCookie } from "./admin-session";
import { loadGroups, resolveScope, hasAnyAccess } from "./groups";
import type { Env } from "./types";
interface PendingState {
@@ -84,8 +85,10 @@ export function createOAuthRoutes(): Hono<{ Bindings: Env }> {
const isBrowser = (c.req.header("accept") ?? "").includes("text/html");
if (isBrowser) {
if (!isAdminUser(c.env, result.userId, result.login)) {
return c.redirect("/?error=forbidden");
const groups = await loadGroups(c.env.KV);
const scope = resolveScope(c.env, groups, result.userId, result.login);
if (!hasAnyAccess(scope)) {
return c.redirect("/admin?error=forbidden");
}
const sessionId = await createAdminSession(c.env.KV, result.userId, result.login);
c.header("Set-Cookie", adminCookie(sessionId));