Skip to content

Commit 147c67a

Browse files
committed
fix: permission ceiling is <= not >= — L10 user sees L1..L10, not L10..L15
1 parent 8e47a83 commit 147c67a

1 file changed

Lines changed: 24 additions & 25 deletions

File tree

backend/permission_compiler.py

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
# RESTRICTED_TAGS are the compliance tags that gate access.
2-
# Nodes with no tags, or tags like "ortho"/"medicine", are unrestricted.
31
RESTRICTED_TAGS = {"MNPI", "CONFIDENTIAL", "PHI", "LEGAL_HOLD"}
42

53

64
def compile_permissions(user, hierarchy_levels):
75
"""
86
O(1) lookup: {level_id: can_read}
97
10-
Permission rule (from spec):
11-
- ADMIN: reads everything
12-
- HOD: reads all levels in own dept + shared/global levels (no dept), ceiling-gated for other depts
13-
- EDITOR / QUALITY: same as HOD
14-
- VIEWER: ceiling-gated for ALL levels including own dept
15-
(Priya at L10 cannot see L4 Ortho HOD decisions)
16-
17-
"ceiling_level" means: user can read nodes whose hierarchy_level_number >= their ceiling.
18-
Lower number = higher in tree = more privileged.
19-
Priya ceiling=10 can read L10, L11, L12, L13, L14, L15 only.
20-
Vikram (HOD) ceiling=4 can read L4 and below in his dept.
8+
The DAG goes from root (L1) DOWN to leaf nodes (L10, L12).
9+
Lower level_number = higher in tree = more privileged.
10+
Higher level_number = deeper in tree = more specific/restricted.
11+
12+
"ceiling_level" = the DEEPEST level a user can see.
13+
Priya ceiling=10 → can read L1, L3, L5, L8, L10 (anything <= 10).
14+
Cannot read L12 patient-level nodes.
15+
Vikram (HOD) ceiling=4 → can read L1, L3 only for cross-dept.
16+
But reads ALL ortho levels (his own dept).
17+
Admin Suresh ceiling=1 → reads everything.
18+
19+
Rule: can_read = level_number <= user.ceiling_level
20+
Exception: HOD/EDITOR/QUALITY read ALL levels in their own department.
2121
"""
2222
role = user.get("role", "VIEWER")
2323
ceiling = user.get("ceiling_level", 15)
@@ -35,28 +35,27 @@ def compile_permissions(user, hierarchy_levels):
3535
permission_map[level_id] = True
3636

3737
elif role == "HOD":
38-
# HOD reads all levels in own dept (they manage the whole dept)
39-
# + shared/global levels (no department set)
40-
# + other depts only if at or below ceiling
41-
if level_dept == user_dept or level_dept is None:
38+
# HOD reads all levels in own dept
39+
# + shared/global levels (no dept) within ceiling
40+
# + other depts only within ceiling
41+
if level_dept == user_dept:
4242
permission_map[level_id] = True
4343
else:
44-
permission_map[level_id] = level_number >= ceiling
44+
permission_map[level_id] = level_number <= ceiling
4545

4646
elif role in ("EDITOR", "QUALITY"):
4747
# Same as HOD for read permissions
48-
if level_dept == user_dept or level_dept is None:
48+
if level_dept == user_dept:
4949
permission_map[level_id] = True
5050
else:
51-
permission_map[level_id] = level_number >= ceiling
51+
permission_map[level_id] = level_number <= ceiling
5252

5353
elif role == "AUDITOR":
54-
# AUDITOR reads everything (for compliance review) but ceiling-gated
55-
permission_map[level_id] = level_number >= ceiling
54+
permission_map[level_id] = level_number <= ceiling
5655

5756
else:
58-
# VIEWER: strictly ceiling-gated for ALL levels, including own dept
59-
# Priya (VIEWER, L10, Ortho) cannot see L4 Ortho HOD decisions
60-
permission_map[level_id] = level_number >= ceiling
57+
# VIEWER: ceiling-gated for ALL levels including own dept
58+
# Priya (VIEWER, L10, Ortho) can read L1-L10, not L12 patient nodes
59+
permission_map[level_id] = level_number <= ceiling
6160

6261
return permission_map

0 commit comments

Comments
 (0)