mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
fix: grant admin role to anonymous user and add DEFAULT_TOOL_VIEW env var
When AUTH_ENABLED=false, the anonymous user was assigned the "user" role which lacks settings:write permission, making all settings saves return 403. Since no admin exists when auth is disabled, settings were permanently read-only. Promote the anonymous user to "admin" so the single user has full control of the instance. Also adds DEFAULT_TOOL_VIEW env var (sidebar|fullscreen) following the existing DEFAULT_THEME pattern, seeded via ensureDefaultSettings() on first boot. Closes #135
This commit is contained in:
@@ -63,6 +63,7 @@ function ensureDefaultSettings() {
|
||||
const defaults: Record<string, string> = {
|
||||
defaultTheme: env.DEFAULT_THEME,
|
||||
defaultLocale: env.DEFAULT_LOCALE,
|
||||
defaultToolView: env.DEFAULT_TOOL_VIEW,
|
||||
};
|
||||
for (const [key, value] of Object.entries(defaults)) {
|
||||
const existing = db.select().from(schema.settings).where(eq(schema.settings.key, key)).get();
|
||||
|
||||
@@ -27,6 +27,7 @@ const envSchema = z
|
||||
WORKSPACE_PATH: z.string().default("./tmp/workspace"),
|
||||
DEFAULT_THEME: z.enum(["light", "dark", "system"]).default("light"),
|
||||
DEFAULT_LOCALE: z.string().default("en"),
|
||||
DEFAULT_TOOL_VIEW: z.enum(["sidebar", "fullscreen"]).default("sidebar"),
|
||||
CORS_ORIGIN: z.string().default(""),
|
||||
MAX_USERS: z.coerce.number().default(0),
|
||||
LOG_LEVEL: z.enum(["fatal", "error", "warn", "info", "debug", "trace"]).default("info"),
|
||||
|
||||
@@ -847,13 +847,11 @@ function isPublicRoute(url: string): boolean {
|
||||
|
||||
export async function authMiddleware(app: FastifyInstance): Promise<void> {
|
||||
app.addHook("preHandler", async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
// When auth is disabled, attach a synthetic non-admin user so tools work
|
||||
// but admin-only routes (user management, settings write, etc.) stay locked
|
||||
if (!env.AUTH_ENABLED) {
|
||||
(request as FastifyRequest & { user?: AuthUser }).user = {
|
||||
id: "anonymous",
|
||||
username: "anonymous",
|
||||
role: "user",
|
||||
role: "admin",
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user