@@ -380,18 +380,15 @@ class DocumentViewSet(
380
380
9. **Media Auth**: Authorize access to document media.
381
381
Example: GET /documents/media-auth/
382
382
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.
387
384
Example: POST /documents/{id}/ai-transform/
388
385
Expected data:
389
386
- text (str): The input text.
390
387
- action (str): The transformation type, one of [prompt, correct, rephrase, summarize].
391
388
Returns: JSON response with the processed text.
392
389
Throttled by: AIDocumentRateThrottle, AIUserRateThrottle.
393
390
394
- 12 . **AI Translate**: Translate a piece of text with AI.
391
+ 11 . **AI Translate**: Translate a piece of text with AI.
395
392
Example: POST /documents/{id}/ai-translate/
396
393
Expected data:
397
394
- text (str): The input text.
@@ -1207,17 +1204,6 @@ def _auth_get_url_params(self, pattern, fragment):
1207
1204
logger .debug ("Failed to extract parameters from subrequest URL: %s" , exc )
1208
1205
raise drf .exceptions .PermissionDenied () from exc
1209
1206
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
-
1221
1207
@drf .decorators .action (detail = False , methods = ["get" ], url_path = "media-auth" )
1222
1208
def media_auth (self , request , * args , ** kwargs ):
1223
1209
"""
@@ -1265,42 +1251,6 @@ def media_auth(self, request, *args, **kwargs):
1265
1251
1266
1252
return drf .response .Response ("authorized" , headers = request .headers , status = 200 )
1267
1253
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
-
1304
1254
@drf .decorators .action (
1305
1255
detail = True ,
1306
1256
methods = ["post" ],
@@ -1420,12 +1370,7 @@ def cors_proxy(self, request, *args, **kwargs):
1420
1370
1421
1371
class DocumentAccessViewSet (
1422
1372
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 ,
1429
1374
):
1430
1375
"""
1431
1376
API ViewSet for all interactions with document accesses.
@@ -1457,6 +1402,32 @@ class DocumentAccessViewSet(
1457
1402
queryset = models .DocumentAccess .objects .select_related ("user" ).all ()
1458
1403
resource_field_name = "document"
1459
1404
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 ()
1460
1431
1461
1432
def perform_create (self , serializer ):
1462
1433
"""Add a new access to the document and send an email to the new added user."""
0 commit comments