MathTutor / database /02_rls.sql
rsreenivasa's picture
AI MVP
e46d575
Raw History Blame Contribute Delete
3.76 kB
-- ============================================================
-- MathMind AI β€” Row Level Security (RLS)
-- Run AFTER 01_schema.sql
-- ============================================================
-- Enable RLS on all student-data tables
alter table public.profiles enable row level security;
alter table public.sessions enable row level security;
alter table public.practice_attempts enable row level security;
alter table public.test_results enable row level security;
alter table public.student_badges enable row level security;
-- badges table is read-only for all authenticated users (definitions, not student data)
alter table public.badges enable row level security;
-- ── profiles ───────────────────────────────────────────────
create policy "Students can view own profile"
on public.profiles for select
using (auth.uid() = id);
create policy "Students can update own profile"
on public.profiles for update
using (auth.uid() = id);
create policy "Students can insert own profile"
on public.profiles for insert
with check (auth.uid() = id);
-- ── sessions ───────────────────────────────────────────────
create policy "Students can view own sessions"
on public.sessions for select
using (auth.uid() = user_id);
create policy "Students can insert own sessions"
on public.sessions for insert
with check (auth.uid() = user_id);
-- ── practice_attempts ──────────────────────────────────────
-- Students access their own attempts via session ownership
create policy "Students can view own practice attempts"
on public.practice_attempts for select
using (
exists (
select 1 from public.sessions s
where s.id = practice_attempts.session_id
and s.user_id = auth.uid()
)
);
create policy "Students can insert own practice attempts"
on public.practice_attempts for insert
with check (
exists (
select 1 from public.sessions s
where s.id = practice_attempts.session_id
and s.user_id = auth.uid()
)
);
-- ── test_results ───────────────────────────────────────────
create policy "Students can view own test results"
on public.test_results for select
using (auth.uid() = user_id);
create policy "Students can insert own test results"
on public.test_results for insert
with check (auth.uid() = user_id);
-- ── badges (definitions β€” read-only for all auth users) ────
create policy "All authenticated users can read badges"
on public.badges for select
using (auth.role() = 'authenticated');
-- ── student_badges ─────────────────────────────────────────
create policy "Students can view own earned badges"
on public.student_badges for select
using (auth.uid() = user_id);
create policy "Students can insert own earned badges"
on public.student_badges for insert
with check (auth.uid() = user_id);
-- ── Service role bypass ────────────────────────────────────
-- The FastAPI backend uses service_role key which bypasses RLS.
-- This is intentional β€” backend does server-side validation before
-- writing to the DB. Never expose service_role key to the client.