forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits

parent_link.rs107 lines, 4.3 KiBHistoryRawDownload
1//! Parent-link decisions: who may be tied to whom (the admin's `link`),
2//! and what a link grants. A link row alone is never the grant — every
3//! parent-side permission re-derives it against the student's *live* role
4//! through [`links_live`], because the demotion sweeps in
5//! [`crate::service::user::set_role`] run once over the rows that exist at
6//! that moment and a sweep can lose a race with `link`. The row-storage
7//! itself is [`crate::db::parent_link`].
8
9use crate::database::Database;
10use crate::db::parent_link;
11use crate::domain::parent_link::ParentLink;
12use crate::domain::role::Role;
13use crate::domain::user::{User, UserId};
14use crate::error::{AppError, ValidationError};
15
16/// Tie (idempotently) `student` to `parent`. The gate is the school-office
17/// rule: `{id}` must hold the `parent` role and `user_id` the `student`
18/// role. A stale-link race cannot start here — a target demoted between
19/// this check and the upsert fails the role gate again on the next call,
20/// and the sweeps treat the fresh row like any other.
21///
22/// Returns the tie plus the two accounts it was judged on, so the caller
23/// can render the response without re-reading either.
24pub async fn link(
25 db: &Database,
26 parent: &UserId,
27 student: &UserId,
28 linked_by: &UserId,
29) -> Result<(ParentLink, User, User), AppError> {
30 let parent = crate::service::user::read(db, parent)
31 .await?
32 .ok_or(AppError::NotFound)?;
33 if parent.get_role() != Role::Parent {
34 return Err(AppError::Validation(ValidationError::Invalid {
35 field: "id",
36 reason: "students can only be tied to a parent account",
37 }));
38 }
39 let Some(student) = crate::service::user::read(db, student).await? else {
40 return Err(AppError::Validation(ValidationError::Invalid {
41 field: "user_id",
42 reason: "target user does not exist",
43 }));
44 };
45 if student.get_role() != Role::Student {
46 return Err(AppError::Validation(ValidationError::Invalid {
47 field: "user_id",
48 reason: "only students can be tied to a parent",
49 }));
50 }
51 let link = parent_link::link(db, parent.get_id(), student.get_id(), linked_by).await?;
52 Ok((link, parent, student))
53}
54
55/// True iff `parent` holds a *live* link to `student`: the row exists AND
56/// the student side still holds the student role. The link row alone is
57/// not the grant — like a stale enrollment, a link whose student side
58/// changed role must be inert, so the target's live role is re-read here.
59/// A missing or non-student target reads as `false`, so a parent never
60/// gets an existence oracle.
61pub async fn links_live(
62 db: &Database,
63 parent: &UserId,
64 student: &UserId,
65) -> Result<bool, AppError> {
66 Ok(parent_link::exists(db, parent, student).await?
67 && crate::service::user::read(db, student)
68 .await?
69 .is_some_and(|student| student.get_role() == Role::Student))
70}
71
72/// May `caller` read `target`'s per-student reports (marks, attendance,
73/// pomodoro)? Teacher+ always may (per-endpoint narrowing is the caller's
74/// business); a parent may exactly when a `parent_link` row ties them to the
75/// target *and* the target still holds the student role. Everyone else —
76/// students included — gets a 403 (self-reads go through the `/me` endpoints).
77pub async fn ensure_can_observe(
78 caller: &User,
79 target: &UserId,
80 db: &Database,
81) -> Result<(), AppError> {
82 if caller.get_role().at_least(Role::Teacher) {
83 return Ok(());
84 }
85 if caller.get_role() == Role::Parent && links_live(db, caller.get_id(), target).await? {
86 return Ok(());
87 }
88 Err(AppError::Forbidden(
89 "requires teacher role or higher, or a parent link to this student",
90 ))
91}
92
93/// Every student `parent` observes, newest link first. Rows, not a live
94/// filter — callers re-derive each student's live role themselves when the
95/// grant matters (the inert-row filter lives with each reader).
96pub async fn list_for_parent(db: &Database, parent: &UserId) -> Result<Vec<ParentLink>, AppError> {
97 parent_link::list_for_parent(db, parent).await
98}
99
100/// Untie a student from a parent — `Some` iff a tie existed.
101pub async fn remove(
102 db: &Database,
103 parent: &UserId,
104 student: &UserId,
105) -> Result<Option<ParentLink>, AppError> {
106 parent_link::remove(db, parent, student).await
107}