Skip to content

Commit f782a02

Browse files
sampaccoudAntoLC
authored andcommitted
♻️(backend) optimize refactoring access abilities and fix inheritance
The latest refactoring in a445278 kept some factorizations that are not legit anymore after the refactoring. It is also cleaner to not make serializer choice in the list view if the reason for this choice is related to something else b/c other views would then use the wrong serializer and that would be a security leak. This commit also fixes a bug in the access rights inheritance: if a user is allowed to see accesses on a document, he should see all acesses related to ancestors, even the ancestors that he can not read. This is because the access that was granted on all ancestors also apply on the current document... so it must be displayed. Lastly, we optimize database queries because the number of accesses we fetch is going up with multi-pages and we were generating a lot of useless queries.
1 parent c1fc1bd commit f782a02

File tree

5 files changed

+313
-207
lines changed

5 files changed

+313
-207
lines changed

src/backend/core/api/serializers.py

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,10 @@ class Meta:
3232
class UserLightSerializer(UserSerializer):
3333
"""Serialize users with limited fields."""
3434

35-
id = serializers.SerializerMethodField(read_only=True)
36-
email = serializers.SerializerMethodField(read_only=True)
37-
38-
def get_id(self, _user):
39-
"""Return always None. Here to have the same fields than in UserSerializer."""
40-
return None
41-
42-
def get_email(self, _user):
43-
"""Return always None. Here to have the same fields than in UserSerializer."""
44-
return None
45-
4635
class Meta:
4736
model = models.User
48-
fields = ["id", "email", "full_name", "short_name"]
49-
read_only_fields = ["id", "email", "full_name", "short_name"]
37+
fields = ["full_name", "short_name"]
38+
read_only_fields = ["full_name", "short_name"]
5039

5140

5241
class BaseAccessSerializer(serializers.ModelSerializer):
@@ -59,11 +48,11 @@ def update(self, instance, validated_data):
5948
validated_data.pop("user", None)
6049
return super().update(instance, validated_data)
6150

62-
def get_abilities(self, access) -> dict:
51+
def get_abilities(self, instance) -> dict:
6352
"""Return abilities of the logged-in user on the instance."""
6453
request = self.context.get("request")
6554
if request:
66-
return access.get_abilities(request.user)
55+
return instance.get_abilities(request.user)
6756
return {}
6857

6958
def validate(self, attrs):
@@ -77,7 +66,6 @@ def validate(self, attrs):
7766
# Update
7867
if self.instance:
7968
can_set_role_to = self.instance.get_abilities(user)["set_role_to"]
80-
8169
if role and role not in can_set_role_to:
8270
message = (
8371
f"You are only allowed to set role to {', '.join(can_set_role_to)}"
@@ -140,19 +128,41 @@ class DocumentAccessSerializer(BaseAccessSerializer):
140128
class Meta:
141129
model = models.DocumentAccess
142130
resource_field_name = "document"
143-
fields = ["id", "document_id", "user", "user_id", "team", "role", "abilities"]
131+
fields = [
132+
"id",
133+
"document_id",
134+
"user",
135+
"user_id",
136+
"team",
137+
"role",
138+
"abilities",
139+
]
144140
read_only_fields = ["id", "document_id", "abilities"]
145141

146142

147-
class DocumentAccessLightSerializer(BaseAccessSerializer):
143+
class DocumentAccessLightSerializer(DocumentAccessSerializer):
148144
"""Serialize document accesses with limited fields."""
149145

150146
user = UserLightSerializer(read_only=True)
151147

152148
class Meta:
153149
model = models.DocumentAccess
154-
fields = ["id", "user", "team", "role", "abilities"]
155-
read_only_fields = ["id", "team", "role", "abilities"]
150+
resource_field_name = "document"
151+
fields = [
152+
"id",
153+
"document_id",
154+
"user",
155+
"team",
156+
"role",
157+
"abilities",
158+
]
159+
read_only_fields = [
160+
"id",
161+
"document_id",
162+
"team",
163+
"role",
164+
"abilities",
165+
]
156166

157167

158168
class TemplateAccessSerializer(BaseAccessSerializer):

src/backend/core/api/viewsets.py

Lines changed: 71 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import json
55
import logging
66
import uuid
7+
from collections import defaultdict
78
from urllib.parse import unquote, urlencode, urlparse
89

910
from django.conf import settings
@@ -1500,49 +1501,88 @@ class DocumentAccessViewSet(
15001501
permission_classes = [permissions.IsAuthenticated, permissions.AccessPermission]
15011502
queryset = models.DocumentAccess.objects.select_related("user").all()
15021503
resource_field_name = "document"
1503-
serializer_class = serializers.DocumentAccessSerializer
15041504

1505-
def list(self, request, *args, **kwargs):
1506-
"""Return accesses for the current document with filters and annotations."""
1507-
user = self.request.user
1505+
def __init__(self, *args, **kwargs):
1506+
"""Initialize the viewset and define default value for contextual document."""
1507+
super().__init__(*args, **kwargs)
1508+
self.document = None
15081509

1509-
try:
1510-
document = models.Document.objects.get(pk=self.kwargs["resource_id"])
1511-
except models.Document.DoesNotExist:
1512-
return drf.response.Response([])
1510+
def initial(self, request, *args, **kwargs):
1511+
"""Retrieve self.document with annotated user roles."""
1512+
super().initial(request, *args, **kwargs)
15131513

1514-
role = document.get_role(user)
1515-
if role is None:
1516-
return drf.response.Response([])
1514+
try:
1515+
self.document = models.Document.objects.annotate_user_roles(
1516+
self.request.user
1517+
).get(pk=self.kwargs["resource_id"])
1518+
except models.Document.DoesNotExist as excpt:
1519+
raise Http404() from excpt
15171520

1518-
ancestors = (
1519-
(document.get_ancestors() | models.Document.objects.filter(pk=document.pk))
1520-
.filter(ancestors_deleted_at__isnull=True)
1521-
.order_by("path")
1521+
def get_serializer_class(self):
1522+
"""Use light serializer for unprivileged users."""
1523+
return (
1524+
serializers.DocumentAccessSerializer
1525+
if self.document.get_role(self.request.user) in choices.PRIVILEGED_ROLES
1526+
else serializers.DocumentAccessLightSerializer
15221527
)
1523-
highest_readable = ancestors.readable_per_se(user).only("depth").first()
15241528

1525-
if highest_readable is None:
1529+
def list(self, request, *args, **kwargs):
1530+
"""Return accesses for the current document with filters and annotations."""
1531+
user = request.user
1532+
1533+
role = self.document.get_role(user)
1534+
if not role:
15261535
return drf.response.Response([])
15271536

1528-
queryset = self.get_queryset()
1529-
queryset = queryset.filter(
1530-
document__in=ancestors.filter(depth__gte=highest_readable.depth)
1531-
)
1537+
ancestors = (
1538+
self.document.get_ancestors()
1539+
| models.Document.objects.filter(pk=self.document.pk)
1540+
).filter(ancestors_deleted_at__isnull=True)
15321541

1533-
is_privileged = role in choices.PRIVILEGED_ROLES
1534-
if is_privileged:
1535-
serializer_class = serializers.DocumentAccessSerializer
1536-
else:
1537-
# Return only the document's privileged accesses
1542+
queryset = self.get_queryset().filter(document__in=ancestors)
1543+
1544+
if role not in choices.PRIVILEGED_ROLES:
15381545
queryset = queryset.filter(role__in=choices.PRIVILEGED_ROLES)
1539-
serializer_class = serializers.DocumentAccessLightSerializer
15401546

1541-
queryset = queryset.distinct()
1542-
serializer = serializer_class(
1543-
queryset, many=True, context=self.get_serializer_context()
1547+
accesses = list(
1548+
queryset.annotate(document_path=db.F("document__path")).order_by(
1549+
"document_path"
1550+
)
15441551
)
1545-
return drf.response.Response(serializer.data)
1552+
1553+
# Annotate more information on roles
1554+
path_to_ancestors_roles = defaultdict(list)
1555+
path_to_role = defaultdict(lambda: None)
1556+
for access in accesses:
1557+
if access.user_id == user.id or access.team in user.teams:
1558+
parent_path = access.document_path[: -models.Document.steplen]
1559+
if parent_path:
1560+
path_to_ancestors_roles[access.document_path].extend(
1561+
path_to_ancestors_roles[parent_path]
1562+
)
1563+
path_to_ancestors_roles[access.document_path].append(
1564+
path_to_role[parent_path]
1565+
)
1566+
else:
1567+
path_to_ancestors_roles[access.document_path] = []
1568+
1569+
path_to_role[access.document_path] = choices.RoleChoices.max(
1570+
path_to_role[access.document_path], access.role
1571+
)
1572+
1573+
# serialize and return the response
1574+
context = self.get_serializer_context()
1575+
serializer_class = self.get_serializer_class()
1576+
serialized_data = []
1577+
for access in accesses:
1578+
access.set_user_roles_tuple(
1579+
choices.RoleChoices.max(*path_to_ancestors_roles[access.document_path]),
1580+
path_to_role.get(access.document_path),
1581+
)
1582+
serializer = serializer_class(access, context=context)
1583+
serialized_data.append(serializer.data)
1584+
1585+
return drf.response.Response(serialized_data)
15461586

15471587
def perform_create(self, serializer):
15481588
"""Add a new access to the document and send an email to the new added user."""

0 commit comments

Comments
 (0)