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.
TypeScript aeoai-agentai-searchanswer-engine-optimizationbrand-monitoringchatgptclaudecopilotdockergeminigeogoogle-ai-overviewsgrokmcpnextjsopen-sourceperplexityseosupabasetypescript
fix(db): keep a profile's role and organization to the server (#1317)
Gökhan Geyik8273174e1a11
diff --git a/supabase/migrations/00228_profiles_server_columns.sql b/supabase/migrations/00228_profiles_server_columns.sqlnew file mode 100644index 000000000..62c5991e5--- /dev/null+++ b/supabase/migrations/00228_profiles_server_columns.sql@@ -0,0 +1,58 @@+-- ============================================================================+-- 00228: a profile's role and organization are set by the server+-- ============================================================================+-- Roles change through team management, and a person joins an organization+-- through an invitation; both run with the service role. A member's own+-- update of their profile is for their name, avatar and onboarding state.+--+-- The one organization a person sets for themselves is the first, in+-- onboarding: right after creating it, while it has no other member. This+-- trigger keeps a profile's own update to that:+-- - `role` only changes with the service role;+-- - `organization_id` only changes with the service role, or from null to+-- an organization nobody else belongs to yet.++create or replace function public.organization_has_other_members(p_org uuid, p_except uuid)+returns boolean+language sql+stable+security definer+set search_path = ''+as $$+ select exists (+ select 1 from public.profiles p+ where p.organization_id = p_org and p.id <> p_except+ );+$$;+revoke all on function public.organization_has_other_members(uuid, uuid) from public, anon;+grant execute on function public.organization_has_other_members(uuid, uuid) to authenticated, service_role;++create or replace function public.guard_profile_server_columns()+returns trigger+language plpgsql+set search_path = ''+as $$+begin+ if (select rolbypassrls from pg_catalog.pg_roles where rolname = current_user) then+ return new;+ end if;+ if new.role is distinct from old.role then+ raise exception 'profile role is managed server-side' using errcode = '42501';+ end if;+ if new.organization_id is distinct from old.organization_id+ and (+ old.organization_id is not null+ or new.organization_id is null+ or public.organization_has_other_members(new.organization_id, new.id)+ )+ then+ raise exception 'profile organization is managed server-side' using errcode = '42501';+ end if;+ return new;+end;+$$;++drop trigger if exists profiles_guard_server_columns on public.profiles;+create trigger profiles_guard_server_columns+ before update on public.profiles+ for each row execute function public.guard_profile_server_columns();diff --git a/supabase/schema.sql b/supabase/schema.sqlindex ec6773dbd..795384f2c 100644--- a/supabase/schema.sql+++ b/supabase/schema.sql@@ -31210,3 +31210,65 @@ alter table public.prompt_result_citations set (autovacuum_analyze_scale_factor = 0.02);+-- ─────────────────────────────────────────────────────────────────────────+-- migrations/00228_profiles_server_columns.sql+-- ─────────────────────────────────────────────────────────────────────────+-- ============================================================================+-- 00228: a profile's role and organization are set by the server+-- ============================================================================+-- Roles change through team management, and a person joins an organization+-- through an invitation; both run with the service role. A member's own+-- update of their profile is for their name, avatar and onboarding state.+--+-- The one organization a person sets for themselves is the first, in+-- onboarding: right after creating it, while it has no other member. This+-- trigger keeps a profile's own update to that:+-- - `role` only changes with the service role;+-- - `organization_id` only changes with the service role, or from null to+-- an organization nobody else belongs to yet.++create or replace function public.organization_has_other_members(p_org uuid, p_except uuid)+returns boolean+language sql+stable+security definer+set search_path = ''+as $$+ select exists (+ select 1 from public.profiles p+ where p.organization_id = p_org and p.id <> p_except+ );+$$;+revoke all on function public.organization_has_other_members(uuid, uuid) from public, anon;+grant execute on function public.organization_has_other_members(uuid, uuid) to authenticated, service_role;++create or replace function public.guard_profile_server_columns()+returns trigger+language plpgsql+set search_path = ''+as $$+begin+ if (select rolbypassrls from pg_catalog.pg_roles where rolname = current_user) then+ return new;+ end if;+ if new.role is distinct from old.role then+ raise exception 'profile role is managed server-side' using errcode = '42501';+ end if;+ if new.organization_id is distinct from old.organization_id+ and (+ old.organization_id is not null+ or new.organization_id is null+ or public.organization_has_other_members(new.organization_id, new.id)+ )+ then+ raise exception 'profile organization is managed server-side' using errcode = '42501';+ end if;+ return new;+end;+$$;++drop trigger if exists profiles_guard_server_columns on public.profiles;+create trigger profiles_guard_server_columns+ before update on public.profiles+ for each row execute function public.guard_profile_server_columns();+diff --git a/web/src/types/supabase.ts b/web/src/types/supabase.tsindex adac6e552..7aa83822a 100644--- a/web/src/types/supabase.ts+++ b/web/src/types/supabase.ts@@ -4045,6 +4045,10 @@ export type Database = {locations: number}[]}+ organization_has_other_members: {+ Args: { p_except: string; p_org: string }+ Returns: boolean+ }prompt_performance_aggregates: {Args: {p_brand_id: string