Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Refactor site access control and user permissions

Bill Yang09e08d7f3c8e
diff --git a/server/src/api/sites/getSite.ts b/server/src/api/sites/getSite.ts
index 82804c3ce..b79cc6190 100644
--- a/server/src/api/sites/getSite.ts
+++ b/server/src/api/sites/getSite.ts
@@ -1,8 +1,8 @@
-import { FastifyRequest, FastifyReply } from "fastify";
+import { eq } from "drizzle-orm";
+import { FastifyReply, FastifyRequest } from "fastify";
import { db } from "../../db/postgres/postgres.js";
import { sites } from "../../db/postgres/schema.js";
-import { eq } from "drizzle-orm";
-import { isSitePublic } from "../../utils.js";
+import { getUserHasAccessToSitePublic } from "../../lib/auth-utils.js";
interface GetSiteParams {
Params: {
@@ -28,12 +28,15 @@ export async function getSite(
}
// Check if user is authorized to access this site
- const isPublic = await isSitePublic(id);
const isOwner = site.createdBy === userId;
- // Only allow access if the site is public or the user is the owner
- if (!isPublic && !isOwner) {
- return reply.status(403).send({ error: "Access denied" });
+ // Check user access to site
+ const userHasAccessToSite = await getUserHasAccessToSitePublic(
+ request,
+ site.siteId
+ );
+ if (!userHasAccessToSite) {
+ return reply.status(403).send({ error: "Forbidden" });
}
return reply.status(200).send({
diff --git a/server/src/db/postgres/schema.ts b/server/src/db/postgres/schema.ts
index 20fea5f6d..43112a03f 100644
--- a/server/src/db/postgres/schema.ts
+++ b/server/src/db/postgres/schema.ts
@@ -30,6 +30,8 @@ export const user = pgTable(
stripeCustomerId: text(),
overMonthlyLimit: boolean().default(false),
monthlyEventCount: integer().default(0),
+ // can view all sites. for internal use only.
+ godMode: boolean().default(false),
},
(table) => [
unique("user_username_unique").on(table.username),
diff --git a/server/src/lib/auth-utils.ts b/server/src/lib/auth-utils.ts
index 821e82dcb..8c78e4704 100644
--- a/server/src/lib/auth-utils.ts
+++ b/server/src/lib/auth-utils.ts
@@ -1,6 +1,6 @@
import { FastifyRequest } from "fastify";
import { auth } from "./auth.js";
-import { sites, member } from "../db/postgres/schema.js";
+import { sites, member, user } from "../db/postgres/schema.js";
import { inArray, eq } from "drizzle-orm";
import { db } from "../db/postgres/postgres.js";
import { isSitePublic } from "../utils.js";
@@ -36,11 +36,26 @@ export async function getSitesUserHasAccessTo(
}
try {
- // Get the user's organization IDs directly from the database
- const memberRecords = await db
- .select({ organizationId: member.organizationId, role: member.role })
- .from(member)
- .where(eq(member.userId, userId));
+ // Fetch user godMode status and member records in parallel
+ const [userRecord, memberRecords] = await Promise.all([
+ db
+ .select({ godMode: user.godMode })
+ .from(user)
+ .where(eq(user.id, userId))
+ .limit(1),
+ db
+ .select({ organizationId: member.organizationId, role: member.role })
+ .from(member)
+ .where(eq(member.userId, userId)),
+ ]);
+
+ const hasGodMode = userRecord.length > 0 && userRecord[0].godMode;
+
+ // If user has godMode, return all sites
+ if (hasGodMode) {
+ const allSites = await db.select().from(sites);
+ return allSites;
+ }
if (!memberRecords || memberRecords.length === 0) {
return [];