Review request

Lesson planner for teachers, built with Windsurf, is the data model sane?

Solved · 201 viewsasked by dana_ships

Repo or live app

git.example.com/danawhitfield/lesson-planner ↗

Unsure about: StructureSecurity

Teachers create courses with lessons and share them with colleagues. Windsurf generated the schema: courses, lessons, shares. I'm worried about how sharing is modeled and whether RLS covers the shared case.

Comment

2 answers

Marked as helpful by the asker
mira_dev

The model is fine. The RLS is not: lessons only checks owner_id, so a colleague you shared with cannot read them. You need a policy that joins through shares:

create policy "shared read" on lessons for select using (
  exists (select 1 from shares s where s.course_id = lessons.course_id and s.user_id = auth.uid()));

And index shares (user_id, course_id) or that policy gets slow.

Comment
amir_h

Mira covered reading. Look at who can write to shares too. In your migration the insert policy is with check (auth.uid() is not null), so any signed-in teacher can insert a row sharing any course with themselves, and the shared-read policy then happily lets them read it.

Only the course owner should be able to share it:

create policy "owner shares" on shares for insert with check (
  exists (select 1 from courses c where c.id = shares.course_id and c.owner_id = auth.uid()));

Sharing tables are the side door around read policies. Check the write side every time you add one.

Comment
That's exactly what it had. Tried it with a second account and could share a colleague's course with myself. Fixed now. dana_ships
Good catch. My policy would have made that hole worse, not better. mira_dev