Repository: paperless-ngx/paperless-ngx

Issue:
[BUG] Statistics displays sum over all users
### Description

The statistics shown on the dashboard are a sum over all users ... while ok for "total character" count, its especially confusing for the "documents in inbox" count and somewhat confusing for the "total documents" count.

![image](https://user-images.githubusercontent.com/46486021/234636711-7c5b14f2-9c06-4285-a7c1-2bc863003726.png)

### Steps to reproduce

1. Login as User A
2. Upload a document and leave it in the inbox
3. Login as User B (without any document uploaded or in the inbox)
4. See the "Documents in inbox" count be 1 instead of 0

Same goes for "total documents"

### Webserver logs

```bash
-
```


### Browser logs

```bash
-
```


### Paperless-ngx version

1.14.0

### Host OS

Synology DSM 7.1.1-42962 Update 5

### Installation method

Docker - official image

### Browser

Chrome

### Configuration changes

-

### Other

-


Discussion:

Resolving patch:
diff --git a/src/documents/views.py b/src/documents/views.py
--- a/src/documents/views.py
+++ b/src/documents/views.py
@@ -57,6 +57,7 @@
 from documents.filters import ObjectOwnedOrGrantedPermissionsFilter
 from documents.permissions import PaperlessAdminPermissions
 from documents.permissions import PaperlessObjectPermissions
+from documents.permissions import get_objects_for_user_owner_aware
 from documents.permissions import has_perms_owner_aware
 from documents.tasks import consume_file
 from paperless import version
@@ -831,18 +832,35 @@ class StatisticsView(APIView):
     permission_classes = (IsAuthenticated,)
 
     def get(self, request, format=None):
-        documents_total = Document.objects.all().count()
+        user = request.user if request.user is not None else None
+
+        documents = (
+            Document.objects.all()
+            if user is None
+            else get_objects_for_user_owner_aware(
+                user,
+                "documents.view_document",
+                Document,
+            )
+        )
+        tags = (
+            Tag.objects.all()
+            if user is None
+            else get_objects_for_user_owner_aware(user, "documents.view_tag", Tag)
+        )
+
+        documents_total = documents.count()
 
-        inbox_tag = Tag.objects.filter(is_inbox_tag=True)
+        inbox_tag = tags.filter(is_inbox_tag=True)
 
         documents_inbox = (
-            Document.objects.filter(tags__is_inbox_tag=True).distinct().count()
+            documents.filter(tags__is_inbox_tag=True).distinct().count()
             if inbox_tag.exists()
             else None
         )
 
         document_file_type_counts = (
-            Document.objects.values("mime_type")
+            documents.values("mime_type")
             .annotate(mime_type_count=Count("mime_type"))
             .order_by("-mime_type_count")
             if documents_total > 0
@@ -850,7 +868,7 @@ def get(self, request, format=None):
         )
 
         character_count = (
-            Document.objects.annotate(
+            documents.annotate(
                 characters=Length("content"),
             )
             .aggregate(Sum("characters"))


Test patch: