Skip to content

Commit 796832e

Browse files
committed
✨(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 2286b02 commit 796832e

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
@@ -1451,24 +1451,23 @@ def list(self, request, *args, **kwargs):
14511451
accesses = list(queryset.order_by("document__path"))
14521452

14531453
# Annotate more information on roles
1454-
path_to_key_to_max_ancestors_role = defaultdict(
1455-
lambda: defaultdict(lambda: None)
1456-
)
1454+
# - accesses of the user (direct or via a team)
14571455
path_to_ancestors_roles = defaultdict(list)
14581456
path_to_role = defaultdict(lambda: None)
1457+
# - accesses of other users and teams
1458+
key_to_path_to_max_ancestors_role = defaultdict(
1459+
lambda: defaultdict(lambda: None)
1460+
)
14591461
for access in accesses:
14601462
key = access.target_key
14611463
path = access.document.path
14621464
parent_path = path[: -models.Document.steplen]
14631465

1464-
path_to_key_to_max_ancestors_role[path][key] = choices.RoleChoices.max(
1465-
path_to_key_to_max_ancestors_role[path][key], access.role
1466-
)
1467-
14681466
if parent_path:
1469-
path_to_key_to_max_ancestors_role[path][key] = choices.RoleChoices.max(
1470-
path_to_key_to_max_ancestors_role[parent_path][key],
1471-
path_to_key_to_max_ancestors_role[path][key],
1467+
key_to_path_to_max_ancestors_role[key][parent_path] = (
1468+
choices.RoleChoices.max(
1469+
*key_to_path_to_max_ancestors_role[key].values()
1470+
)
14721471
)
14731472
path_to_ancestors_roles[path].extend(
14741473
path_to_ancestors_roles[parent_path]
@@ -1477,6 +1476,10 @@ def list(self, request, *args, **kwargs):
14771476
else:
14781477
path_to_ancestors_roles[path] = []
14791478

1479+
key_to_path_to_max_ancestors_role[key][path] = choices.RoleChoices.max(
1480+
key_to_path_to_max_ancestors_role[key][parent_path], access.role
1481+
)
1482+
14801483
if access.user_id == user.id or access.team in user.teams:
14811484
path_to_role[path] = choices.RoleChoices.max(
14821485
path_to_role[path], access.role
@@ -1490,7 +1493,7 @@ def list(self, request, *args, **kwargs):
14901493
path = access.document.path
14911494
parent_path = path[: -models.Document.steplen]
14921495
access.max_ancestors_role = (
1493-
path_to_key_to_max_ancestors_role[parent_path][access.target_key]
1496+
key_to_path_to_max_ancestors_role[access.target_key][parent_path]
14941497
if parent_path
14951498
else None
14961499
)

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)