File size: 3,762 Bytes
e46d575
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
-- ============================================================
-- 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.