@@ -380,18 +380,15 @@ class DocumentViewSet(
380380 9. **Media Auth**: Authorize access to document media.
381381 Example: GET /documents/media-auth/
382382
383- 10. **Collaboration Auth**: Authorize access to the collaboration server for a document.
384- Example: GET /documents/collaboration-auth/
385-
386- 11. **AI Transform**: Apply a transformation action on a piece of text with AI.
383+ 10. **AI Transform**: Apply a transformation action on a piece of text with AI.
387384 Example: POST /documents/{id}/ai-transform/
388385 Expected data:
389386 - text (str): The input text.
390387 - action (str): The transformation type, one of [prompt, correct, rephrase, summarize].
391388 Returns: JSON response with the processed text.
392389 Throttled by: AIDocumentRateThrottle, AIUserRateThrottle.
393390
394- 12 . **AI Translate**: Translate a piece of text with AI.
391+ 11 . **AI Translate**: Translate a piece of text with AI.
395392 Example: POST /documents/{id}/ai-translate/
396393 Expected data:
397394 - text (str): The input text.
@@ -1207,17 +1204,6 @@ def _auth_get_url_params(self, pattern, fragment):
12071204 logger .debug ("Failed to extract parameters from subrequest URL: %s" , exc )
12081205 raise drf .exceptions .PermissionDenied () from exc
12091206
1210- def _auth_get_document (self , pk ):
1211- """
1212- Retrieves the document corresponding to the given primary key (pk).
1213- Raises PermissionDenied if the document is not found.
1214- """
1215- try :
1216- return models .Document .objects .get (pk = pk )
1217- except models .Document .DoesNotExist as exc :
1218- logger .debug ("Document with ID '%s' does not exist" , pk )
1219- raise drf .exceptions .PermissionDenied () from exc
1220-
12211207 @drf .decorators .action (detail = False , methods = ["get" ], url_path = "media-auth" )
12221208 def media_auth (self , request , * args , ** kwargs ):
12231209 """
@@ -1265,42 +1251,6 @@ def media_auth(self, request, *args, **kwargs):
12651251
12661252 return drf .response .Response ("authorized" , headers = request .headers , status = 200 )
12671253
1268- @drf .decorators .action (detail = False , methods = ["get" ], url_path = "collaboration-auth" )
1269- def collaboration_auth (self , request , * args , ** kwargs ):
1270- """
1271- This view is used by an Nginx subrequest to control access to a document's
1272- collaboration server.
1273- """
1274- parsed_url = self ._auth_get_original_url (request )
1275- url_params = self ._auth_get_url_params (
1276- enums .COLLABORATION_WS_URL_PATTERN , parsed_url .query
1277- )
1278- document = self ._auth_get_document (url_params ["pk" ])
1279-
1280- abilities = document .get_abilities (request .user )
1281- if not abilities .get (self .action , False ):
1282- logger .debug (
1283- "User '%s' lacks permission for document '%s'" ,
1284- request .user ,
1285- document .pk ,
1286- )
1287- raise drf .exceptions .PermissionDenied ()
1288-
1289- if not settings .COLLABORATION_SERVER_SECRET :
1290- logger .debug ("Collaboration server secret is not defined" )
1291- raise drf .exceptions .PermissionDenied ()
1292-
1293- # Add the collaboration server secret token to the headers
1294- headers = {
1295- "Authorization" : settings .COLLABORATION_SERVER_SECRET ,
1296- "X-Can-Edit" : str (abilities ["partial_update" ]),
1297- }
1298-
1299- if request .user .is_authenticated :
1300- headers ["X-User-Id" ] = str (request .user .id )
1301-
1302- return drf .response .Response ("authorized" , headers = headers , status = 200 )
1303-
13041254 @drf .decorators .action (
13051255 detail = True ,
13061256 methods = ["post" ],
@@ -1420,12 +1370,7 @@ def cors_proxy(self, request, *args, **kwargs):
14201370
14211371class DocumentAccessViewSet (
14221372 ResourceAccessViewsetMixin ,
1423- drf .mixins .CreateModelMixin ,
1424- drf .mixins .DestroyModelMixin ,
1425- drf .mixins .ListModelMixin ,
1426- drf .mixins .RetrieveModelMixin ,
1427- drf .mixins .UpdateModelMixin ,
1428- viewsets .GenericViewSet ,
1373+ viewsets .ModelViewSet ,
14291374):
14301375 """
14311376 API ViewSet for all interactions with document accesses.
@@ -1457,6 +1402,32 @@ class DocumentAccessViewSet(
14571402 queryset = models .DocumentAccess .objects .select_related ("user" ).all ()
14581403 resource_field_name = "document"
14591404 serializer_class = serializers .DocumentAccessSerializer
1405+ is_current_user_owner_or_admin = False
1406+
1407+ def get_queryset (self ):
1408+ """Return the queryset according to the action."""
1409+ queryset = super ().get_queryset ()
1410+
1411+ if self .action == "list" :
1412+ try :
1413+ document = models .Document .objects .get (pk = self .kwargs ["resource_id" ])
1414+ except models .Document .DoesNotExist :
1415+ return queryset .none ()
1416+
1417+ roles = set (document .get_roles (self .request .user ))
1418+ is_owner_or_admin = bool (roles .intersection (set (models .PRIVILEGED_ROLES )))
1419+ self .is_current_user_owner_or_admin = is_owner_or_admin
1420+ if not is_owner_or_admin :
1421+ # Return only the document owner access
1422+ queryset = queryset .filter (role__in = models .PRIVILEGED_ROLES )
1423+
1424+ return queryset
1425+
1426+ def get_serializer_class (self ):
1427+ if self .action == "list" and not self .is_current_user_owner_or_admin :
1428+ return serializers .DocumentAccessLightSerializer
1429+
1430+ return super ().get_serializer_class ()
14601431
14611432 def perform_create (self , serializer ):
14621433 """Add a new access to the document and send an email to the new added user."""
0 commit comments