LifeMate — Personal, Family & Learning Companion

feat/child-identity-schema
chore/phase1-repository-cleanup deploy/stable-web-apk deployment-static design/mobile-accessibility-v2 design/mobile-navigation-review docs/child-education-access-decision docs/offline-data-rescue-options docs/owner-guide-and-delivery-plan docs/phase1-policy-options docs/public-review-live-2026-10-15 feat/aparat-learning-library feat/child-device-safety feat/child-identity-schema feat/flutter-mobile-navigation feat/learning-content-operations feat/local-signing-preflight feat/online-family-stage0 feat/personal-encrypted-recovery feat/private-planner-series-api feat/public-review-page feat/standalone-weekly-planner feat/weekly-planner-android final-feature-1/iran-family-school fix/auth-abuse-expiry-safety fix/auth-expiry-after-lock fix/backend-request-safety fix/lifeguide-bootstrap-naming-ci fix/lifeguide-pages-qa-base-preflight fix/lifeguide-qa-pages-env-deployment fix/local-task-persistence fix/managed-child-session-boundary fix/personal-jalali-dates fix/personal-notification-controls fix/personal-upgrade-delivery-guard fix/sms-approval-gates fix/study-duration-display infra/cloudflare-neon-migration infra/lifeguide-cloudflare-qa infra/lifeguide-github-pages-qa infra/lifeguide-supabase-qa main phase-1/foundation-completion phase-2/product-foundation-identity phase-3/planner-school-family-core phase-4/ai-learning-wellbeing phase-5/hardening-observability-release test/android-reminder-acceptance test/personal-notification-settings test/personal-recovery-acceptance test/role-entry-apk
sql 94 lines 2.97 KB
Raw
1 begin;
2
3 create or replace function can_view_student_academic(p_viewer uuid, p_student uuid) returns boolean
4 language sql stable as $$
5 select p_viewer=p_student or exists(
6 select 1 from guardian_relationship g
7 where g.guardian_user_id=p_viewer
8 and g.minor_user_id=p_student
9 and is_active_guardian(g.family_id,p_viewer,p_student)
10 )
11 $$;
12
13 create or replace function can_view_plan_item(p_viewer uuid, p_item uuid) returns boolean
14 language sql stable as $$
15 select exists(
16 select 1 from plan_item p
17 where p.id=p_item
18 and (
19 p.owner_user_id=p_viewer
20 or (
21 p.family_id is not null
22 and is_active_family_member(p.family_id,p.owner_user_id)
23 and is_active_family_member(p.family_id,p_viewer)
24 and (
25 p.visibility='family'
26 or (
27 p.visibility='parent_guardian'
28 and is_active_guardian(p.family_id,p_viewer,p.owner_user_id)
29 )
30 or (
31 p.visibility='selected_members'
32 and exists(
33 select 1 from sharing_grant sg
34 where sg.resource_type='plan_item'
35 and sg.resource_id=p.id
36 and sg.grantee_user_id=p_viewer
37 and sg.granted_by=p.owner_user_id
38 )
39 )
40 )
41 )
42 )
43 )
44 $$;
45
46 -- Remove grants that are already invalid; later rejoining must not restore them.
47 delete from sharing_grant sg
48 where sg.resource_type='plan_item'
49 and not exists(
50 select 1 from plan_item p
51 where p.id=sg.resource_id
52 and p.visibility='selected_members'
53 and p.family_id is not null
54 and sg.granted_by=p.owner_user_id
55 and is_active_family_member(p.family_id,p.owner_user_id)
56 and is_active_family_member(p.family_id,sg.grantee_user_id)
57 );
58
59 create function revoke_changed_plan_item_grants() returns trigger
60 language plpgsql as $$
61 begin
62 if new.visibility <> 'selected_members'
63 or new.family_id is distinct from old.family_id
64 or new.owner_user_id is distinct from old.owner_user_id then
65 delete from sharing_grant where resource_type='plan_item' and resource_id=new.id;
66 end if;
67 return new;
68 end
69 $$;
70
71 create trigger plan_item_revoke_changed_grants
72 after update of visibility,family_id,owner_user_id on plan_item
73 for each row execute function revoke_changed_plan_item_grants();
74
75 create function revoke_ended_membership_grants() returns trigger
76 language plpgsql as $$
77 begin
78 if tg_op='DELETE' or new.ended_at is not null then
79 delete from sharing_grant sg
80 using plan_item p
81 where sg.resource_type='plan_item'
82 and sg.resource_id=p.id
83 and p.family_id=old.family_id
84 and (p.owner_user_id=old.user_id or sg.grantee_user_id=old.user_id);
85 end if;
86 return null;
87 end
88 $$;
89
90 create trigger family_membership_revoke_ended_grants
91 after update of ended_at or delete on family_membership
92 for each row execute function revoke_ended_membership_grants();
93
94 commit;