Skip to content

Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.

fix(db): keep topic changes to admins and managers (#1790)

Gökhan Geyika19fc733edd0
diff --git a/supabase/migrations/00382_topics_manager_writes.sql b/supabase/migrations/00382_topics_manager_writes.sql
new file mode 100644
index 000000000..9eca53b15
--- /dev/null
+++ b/supabase/migrations/00382_topics_manager_writes.sql
@@ -0,0 +1,90 @@
+-- ============================================================================
+-- 00382: topic changes are an admin's or a manager's
+-- ============================================================================
+-- The app already keeps adding, renaming and deleting topics, and accepting
+-- or dismissing topic suggestions, to admins and managers (the Topics page,
+-- the API and the MCP check the role). The rows' rules now say the same:
+--
+-- * topics: every member of the brand's organization reads them; an admin
+-- or a manager of that organization adds, changes and deletes them.
+-- * topic_suggestions: every member reads them; an admin or a manager
+-- updates them (accept, dismiss). Nothing else changes: the server
+-- writes new suggestions with the service role, which these rules don't
+-- apply to.
+--
+-- Setting up a brand keeps working: a new organization's first person is
+-- its admin, and only admins and managers add brands.
+
+-- ── topics ──────────────────────────────────────────────────────────────────
+
+drop policy if exists "topics: member insert" on public.topics;
+drop policy if exists "topics: member update" on public.topics;
+drop policy if exists "topics: member delete" on public.topics;
+
+drop policy if exists "topics: admin/manager insert" on public.topics;
+create policy "topics: admin/manager insert" on public.topics
+ for insert
+ with check (
+ brand_id in (
+ select b.id
+ from public.brands b
+ join public.profiles p on p.organization_id = b.organization_id
+ where p.id = auth.uid() and p.role in ('admin', 'manager')
+ )
+ );
+
+drop policy if exists "topics: admin/manager update" on public.topics;
+create policy "topics: admin/manager update" on public.topics
+ for update
+ using (
+ brand_id in (
+ select b.id
+ from public.brands b
+ join public.profiles p on p.organization_id = b.organization_id
+ where p.id = auth.uid() and p.role in ('admin', 'manager')
+ )
+ )
+ with check (
+ brand_id in (
+ select b.id
+ from public.brands b
+ join public.profiles p on p.organization_id = b.organization_id
+ where p.id = auth.uid() and p.role in ('admin', 'manager')
+ )
+ );
+
+drop policy if exists "topics: admin/manager delete" on public.topics;
+create policy "topics: admin/manager delete" on public.topics
+ for delete
+ using (
+ brand_id in (
+ select b.id
+ from public.brands b
+ join public.profiles p on p.organization_id = b.organization_id
+ where p.id = auth.uid() and p.role in ('admin', 'manager')
+ )
+ );
+
+-- ── topic_suggestions ───────────────────────────────────────────────────────
+
+drop policy if exists "topic_suggestions: admin/manager/analyst update" on public.topic_suggestions;
+
+drop policy if exists "topic_suggestions: admin/manager update" on public.topic_suggestions;
+create policy "topic_suggestions: admin/manager update" on public.topic_suggestions
+ for update
+ using (
+ brand_id in (
+ select b.id
+ from public.brands b
+ join public.profiles p on p.organization_id = b.organization_id
+ where p.id = auth.uid() and p.role in ('admin', 'manager')
+ )
+ )
+ with check (
+ brand_id in (
+ select b.id
+ from public.brands b
+ join public.profiles p on p.organization_id = b.organization_id
+ where p.id = auth.uid() and p.role in ('admin', 'manager')
+ )
+ );
diff --git a/supabase/schema.sql b/supabase/schema.sql
index 9531fae4d..1460dcd47 100644
--- a/supabase/schema.sql
+++ b/supabase/schema.sql
@@ -44154,6 +44154,100 @@ create or replace trigger prompt_target_urls_refuse_demo_brand_writes
before insert or update or delete on public.prompt_target_urls
for each row execute function public.refuse_demo_brand_writes('prompt_id', 'prompts', 'prompt_set_id', 'prompt_sets');
+-- ─────────────────────────────────────────────────────────────────────────
+-- migrations/00382_topics_manager_writes.sql
+-- ─────────────────────────────────────────────────────────────────────────
+-- ============================================================================
+-- 00382: topic changes are an admin's or a manager's
+-- ============================================================================
+-- The app already keeps adding, renaming and deleting topics, and accepting
+-- or dismissing topic suggestions, to admins and managers (the Topics page,
+-- the API and the MCP check the role). The rows' rules now say the same:
+--
+-- * topics: every member of the brand's organization reads them; an admin
+-- or a manager of that organization adds, changes and deletes them.
+-- * topic_suggestions: every member reads them; an admin or a manager
+-- updates them (accept, dismiss). Nothing else changes: the server
+-- writes new suggestions with the service role, which these rules don't
+-- apply to.
+--
+-- Setting up a brand keeps working: a new organization's first person is
+-- its admin, and only admins and managers add brands.
+
+-- ── topics ──────────────────────────────────────────────────────────────────
+
+drop policy if exists "topics: member insert" on public.topics;
+drop policy if exists "topics: member update" on public.topics;
+drop policy if exists "topics: member delete" on public.topics;
+
+drop policy if exists "topics: admin/manager insert" on public.topics;
+create policy "topics: admin/manager insert" on public.topics
+ for insert
+ with check (
+ brand_id in (
+ select b.id
+ from public.brands b
+ join public.profiles p on p.organization_id = b.organization_id
+ where p.id = auth.uid() and p.role in ('admin', 'manager')
+ )
+ );
+
+drop policy if exists "topics: admin/manager update" on public.topics;
+create policy "topics: admin/manager update" on public.topics
+ for update
+ using (
+ brand_id in (
+ select b.id
+ from public.brands b
+ join public.profiles p on p.organization_id = b.organization_id
+ where p.id = auth.uid() and p.role in ('admin', 'manager')
+ )
+ )
+ with check (
+ brand_id in (
+ select b.id
+ from public.brands b
+ join public.profiles p on p.organization_id = b.organization_id
+ where p.id = auth.uid() and p.role in ('admin', 'manager')
+ )
+ );
+
+drop policy if exists "topics: admin/manager delete" on public.topics;
+create policy "topics: admin/manager delete" on public.topics
+ for delete
+ using (
+ brand_id in (
+ select b.id
+ from public.brands b
+ join public.profiles p on p.organization_id = b.organization_id
+ where p.id = auth.uid() and p.role in ('admin', 'manager')
+ )
+ );
+
+-- ── topic_suggestions ───────────────────────────────────────────────────────
+
+drop policy if exists "topic_suggestions: admin/manager/analyst update" on public.topic_suggestions;
+
+drop policy if exists "topic_suggestions: admin/manager update" on public.topic_suggestions;
+create policy "topic_suggestions: admin/manager update" on public.topic_suggestions
+ for update
+ using (
+ brand_id in (
+ select b.id
+ from public.brands b
+ join public.profiles p on p.organization_id = b.organization_id
+ where p.id = auth.uid() and p.role in ('admin', 'manager')
+ )
+ )
+ with check (
+ brand_id in (
+ select b.id
+ from public.brands b
+ join public.profiles p on p.organization_id = b.organization_id
+ where p.id = auth.uid() and p.role in ('admin', 'manager')
+ )
+ );
+
-- ─────────────────────────────────────────────────────────────────────────
-- migrations/00400_chat_core.sql
-- ─────────────────────────────────────────────────────────────────────────
diff --git a/web/src/lib/topic-policies.test.ts b/web/src/lib/topic-policies.test.ts
new file mode 100644
index 000000000..0757ee006
--- /dev/null
+++ b/web/src/lib/topic-policies.test.ts
@@ -0,0 +1,82 @@
+import { readdirSync, readFileSync } from 'node:fs';
+import { join } from 'node:path';
+import { describe, expect, it } from 'vitest';
+
+/**
+ * The row rules on topics and topic_suggestions as every migration leaves
+ * them, applied in order: a `create policy` adds one, a `drop policy` takes
+ * it away. Read from the migration files, so a later migration that loosens
+ * a rule again fails here.
+ */
+const dir = join(__dirname, '../../../supabase/migrations');
+const files = readdirSync(dir)
+ .filter((f) => /^\d+_.*\.sql$/.test(f))
+ .sort();
+
+interface Policy {
+ command: string;
+ body: string;
+}
+
+function policiesOf(table: string): Map<string, Policy> {
+ const policies = new Map<string, Policy>();
+ const on = `on\\s+(?:"?public"?\\.)?"?${table}"?(?:\\s|$)`;
+ for (const file of files) {
+ const sql = readFileSync(join(dir, file), 'utf8').replace(/--[^\n]*/g, '');
+ for (const statement of sql.split(';')) {
+ const text = statement.replace(/\s+/g, ' ').trim();
+ const drop = text.match(new RegExp(`^drop policy (?:if exists )?"([^"]+)" ${on}`, 'i'));
+ if (drop) policies.delete(drop[1]);
+ const create = text.match(
+ new RegExp(`^create policy "([^"]+)" ${on}(?:as \\w+ )?(?:for (\\w+))?`, 'i'),
+ );
+ if (create) {
+ policies.set(create[1], { command: (create[2] ?? 'all').toLowerCase(), body: text });
+ }
+ }
+ }
+ return policies;
+}
+
+/** Only an admin or a manager of the brand's organization passes the rule. */
+const managersOnly = (body: string) => {
+ const roles = [...body.matchAll(/'(admin|manager|analyst|agency_partner|viewer)'/g)].map(
+ (m) => m[1],
+ );
+ return (
+ /auth"?\."?uid/.test(body) &&
+ roles.length > 0 &&
+ roles.every((r) => r === 'admin' || r === 'manager')
+ );
+};
+
+describe.each(['topics', 'topic_suggestions'])('%s row rules', (table) => {
+ const policies = [...policiesOf(table).values()];
+
+ it('lets every member of the organization read', () => {
+ const reads = policies.filter((p) => p.command === 'select');
+ expect(reads).toHaveLength(1);
+ expect(managersOnly(reads[0].body)).toBe(false);
+ expect(reads[0].body).toMatch(/organization_id/);
+ });
+
+ it('keeps every change to admins and managers', () => {
+ const writes = policies.filter((p) => p.command !== 'select');
+ expect(writes.length).toBeGreaterThan(0);
+ for (const write of writes) expect(managersOnly(write.body), write.body).toBe(true);
+ });
+});
+
+describe('topics row rules', () => {
+ it('cover adding, changing and deleting', () => {
+ const commands = [...policiesOf('topics').values()].map((p) => p.command).sort();
+ expect(commands).toEqual(['delete', 'insert', 'select', 'update']);
+ });
+});
+
+describe('topic_suggestions row rules', () => {
+ it('leave adding and deleting to the server', () => {
+ const commands = [...policiesOf('topic_suggestions').values()].map((p) => p.command).sort();
+ expect(commands).toEqual(['select', 'update']);
+ });
+});