Skip to content

Commit f3244c3

Browse files
sampaccoudPanchoutNathan
authored andcommitted
✨(backend) add child_set_role_to field to document access abilities
The frontend needs to know what options to propose for an access that would be created on a child document. This depends on the access a user/team already has with ancestors...
1 parent b193a6e commit f3244c3

File tree

4 files changed

+275
-156
lines changed

4 files changed

+275
-156
lines changed

src/backend/core/api/viewsets.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,24 +1454,23 @@ def list(self, request, *args, **kwargs):
14541454
accesses = list(queryset.order_by("document__path"))
14551455

14561456
# Annotate more information on roles
1457-
path_to_key_to_max_ancestors_role = defaultdict(
1458-
lambda: defaultdict(lambda: None)
1459-
)
1457+
# - accesses of the user (direct or via a team)
14601458
path_to_ancestors_roles = defaultdict(list)
14611459
path_to_role = defaultdict(lambda: None)
1460+
# - accesses of other users and teams
1461+
key_to_path_to_max_ancestors_role = defaultdict(
1462+
lambda: defaultdict(lambda: None)
1463+
)
14621464
for access in accesses:
14631465
key = access.target_key
14641466
path = access.document.path
14651467
parent_path = path[: -models.Document.steplen]
14661468

1467-
path_to_key_to_max_ancestors_role[path][key] = choices.RoleChoices.max(
1468-
path_to_key_to_max_ancestors_role[path][key], access.role
1469-
)
1470-
14711469
if parent_path:
1472-
path_to_key_to_max_ancestors_role[path][key] = choices.RoleChoices.max(
1473-
path_to_key_to_max_ancestors_role[parent_path][key],
1474-
path_to_key_to_max_ancestors_role[path][key],
1470+
key_to_path_to_max_ancestors_role[key][parent_path] = (
1471+
choices.RoleChoices.max(
1472+
*key_to_path_to_max_ancestors_role[key].values()
1473+
)
14751474
)
14761475
path_to_ancestors_roles[path].extend(
14771476
path_to_ancestors_roles[parent_path]
@@ -1480,6 +1479,10 @@ def list(self, request, *args, **kwargs):
14801479
else:
14811480
path_to_ancestors_roles[path] = []
14821481

1482+
key_to_path_to_max_ancestors_role[key][path] = choices.RoleChoices.max(
1483+
key_to_path_to_max_ancestors_role[key][parent_path], access.role
1484+
)
1485+
14831486
if access.user_id == user.id or access.team in user.teams:
14841487
path_to_role[path] = choices.RoleChoices.max(
14851488
path_to_role[path], access.role
@@ -1493,7 +1496,7 @@ def list(self, request, *args, **kwargs):
14931496
path = access.document.path
14941497
parent_path = path[: -models.Document.steplen]
14951498
access.max_ancestors_role = (
1496-
path_to_key_to_max_ancestors_role[parent_path][access.target_key]
1499+
key_to_path_to_max_ancestors_role[access.target_key][parent_path]
14971500
if parent_path
14981501
else None
14991502
)

src/backend/core/models.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,13 +1150,20 @@ def get_abilities(self, user):
11501150
for candidate_role in set_role_to
11511151
if RoleChoices.get_priority(candidate_role) > ancestors_role_priority
11521152
]
1153+
child_set_role_to = [
1154+
candidate_role
1155+
for candidate_role in set_role_to
1156+
if RoleChoices.get_priority(candidate_role)
1157+
> RoleChoices.get_priority(self.role)
1158+
]
11531159

11541160
return {
11551161
"destroy": can_delete,
11561162
"update": bool(set_role_to) and is_owner_or_admin,
11571163
"partial_update": bool(set_role_to) and is_owner_or_admin,
11581164
"retrieve": (self.user and self.user.id == user.id) or is_owner_or_admin,
11591165
"set_role_to": set_role_to,
1166+
"child_set_role_to": child_set_role_to,
11601167
}
11611168

11621169

0 commit comments

Comments
 (0)