forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiB69d4295Ahmet Kaan Gümüş2 months ago536 commits

instance.rs115 lines, 5.2 KiBHistoryRawDownload
1//! Instance visibility: the two questions every instance-scoped surface asks
2//! before it reads or writes anything — whether this caller may act on one
3//! instance, and which instances are theirs at all. Both lived in
4//! [`crate::web::instances`] while the HTTP layer was their only caller; the
5//! RAG scope deriver ([`super::rag_scope`]) asks the same two questions, and a
6//! service may not reach up into the web layer — nor grow a second copy of a
7//! rule that two surfaces must never disagree about.
8//!
9//! The D10 rule itself is [`super::class_course::ensure_instance_teacher`],
10//! which [`can_manage_instance`] asks as a boolean so a route can word its own
11//! `403`; the row reads are [`crate::db`]'s.
12
13use std::collections::HashSet;
14
15use crate::database::Database;
16use crate::domain::class_course::{ClassCourse, ClassCourseId};
17use crate::domain::class_group::ClassGroupId;
18use crate::domain::role::Role;
19use crate::domain::user::User;
20use crate::error::AppError;
21
22/// Whether `user` may act inside this instance — D10, asked as a boolean so a
23/// handler can word its own `403` (the one gate every instance-scoped route
24/// shares; [`super::class_course::ensure_instance_teacher`] is the same rule
25/// with the refusal in it).
26pub async fn can_manage_instance(
27 db: &Database,
28 instance: &ClassCourseId,
29 user: &User,
30) -> Result<bool, AppError> {
31 Ok(
32 super::class_course::ensure_instance_teacher(db, user, instance)
33 .await
34 .is_ok(),
35 )
36}
37
38/// The instances a caller may see, each with whether they run it: the
39/// instances their class sections carry (a student's roster, and a homeroom
40/// teacher's), the instances they were assigned to teach (a teacher), and —
41/// for a homeroom teacher — their own section's. The per-instance answer is
42/// the same D10 rule every instance-scoped gate applies, computed once for the
43/// list.
44///
45/// [`crate::web::instances`] serves this set to every caller as
46/// `GET /instances/me`; the catalog-wide list other instance routes hand a
47/// manager+ is [`crate::web::courses`]'. [`super::rag_scope`] scopes a RAG
48/// question by it, which is why it lives here now.
49///
50/// Staff are *not* members of the section they run, so the two ways a teacher
51/// reaches a class section are read separately and unioned: `class_member` (a
52/// student's live stint) and the homeroom column (`class_group.teacher`, read
53/// through [`super::class_group::list_for_teacher`]). Leaving the second out
54/// dropped every row for a homeroom teacher who was neither enrolled nor
55/// assigned — even though [`super::class_course::ensure_instance_teacher`]
56/// lets them act on all of them.
57pub async fn visible_instances(
58 user: &User,
59 db: &Database,
60) -> Result<Vec<(ClassCourse, bool)>, AppError> {
61 let mut rows: Vec<(ClassCourse, bool)> = Vec::new();
62 // The class sections the caller is a live member of, unioned with the ones
63 // they are the homeroom teacher of (an empty second read for a student,
64 // and a cheap one for anyone else — it is keyed on the teacher column).
65 //
66 // The homeroom half carries the live-`teacher` floor the whole gate does
67 // (see [`super::class_course::ensure_instance_teacher`]): the column is
68 // history, a role flip that never ran the cascade leaves it standing, and
69 // an account below `teacher` may not keep reaching its sections — nor have
70 // the `manages` flag that shows them the drafts.
71 let staff = user.get_role().at_least(Role::Teacher);
72 let (members, _) = crate::db::class_member::list_for_user(db, user.get_id(), None, 0).await?;
73 let mut classes: Vec<ClassGroupId> = members
74 .iter()
75 .map(|member| member.get_class().clone())
76 .collect();
77 let homeroom = if staff {
78 super::class_group::list_for_teacher(db, user.get_id()).await?
79 } else {
80 Vec::new()
81 };
82 let homeroom_keys: Vec<String> = homeroom.iter().map(|class| class.get_id().key()).collect();
83 let mut seen: HashSet<String> = classes.iter().map(|class| class.key()).collect();
84 classes.extend(
85 homeroom
86 .iter()
87 .map(|class| class.get_id().clone())
88 .filter(|class| seen.insert(class.key())),
89 );
90 for instance in crate::db::class_course::list_for_class_ids(db, &classes).await? {
91 let manages = homeroom_keys.contains(&instance.get_class().key());
92 rows.push((instance, manages));
93 }
94 // The instances the caller teaches: read through the catalog courses they
95 // are assigned in (the junction has no teacher-keyed read of its own) and
96 // kept only where the assignment actually names them.
97 if user.get_role().at_least(Role::Teacher) {
98 for course in super::course::list_for_teacher(db, user.get_id()).await? {
99 let (taught, _) =
100 crate::db::class_course::list_for_course(db, course.get_id(), None, 0).await?;
101 for instance in taught {
102 if rows
103 .iter()
104 .any(|(row, _)| row.get_id() == instance.get_id())
105 {
106 continue;
107 }
108 if can_manage_instance(db, instance.get_id(), user).await? {
109 rows.push((instance, true));
110 }
111 }
112 }
113 }
114 Ok(rows)
115}