]> git.ipfire.org Git - ipfire.org.git/commitdiff
dbl: Make it more convenient to impersonate another user
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 3 Mar 2026 14:21:26 +0000 (14:21 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 3 Mar 2026 14:21:26 +0000 (14:21 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/backend/dbl.py

index a196bea4aef14a8da196ef8fbdb5f6fd0757b47d..8c94d630883cf82c125ee1cc9d0b365ecfac6992 100644 (file)
@@ -20,7 +20,7 @@ from .decorators import *
 log = logging.getLogger(__name__)
 
 class DBL(Object):
-       async def _fetch(self, path, headers=None, args=None, body=None, **kwargs):
+       async def _fetch(self, path, headers=None, args=None, body=None, as_user=None, **kwargs):
                if headers is None:
                        headers = {}
 
@@ -43,6 +43,16 @@ class DBL(Object):
                        "X-API-Key" : self.backend.settings.get("dbl-api-key", ""),
                }
 
+               # Impersonate a user
+               if as_user:
+                       if isinstance(as_user, accounts.Account):
+                               as_user = as_user.uid
+
+                       # Compose the headers
+                       headers |= {
+                               "X-Impersonated-Uid" : as_user,
+                       }
+
                # Serialize any content
                if body:
                        headers |= {
@@ -234,15 +244,6 @@ class List(Model):
                """
                        Submits a report
                """
-               # Only submit the UID of the user
-               if isinstance(reported_by, accounts.Account):
-                       reported_by = reported_by.uid
-
-               # Compose the headers
-               headers = {
-                       "X-Impersonated-Uid" : reported_by,
-               }
-
                # Compose the request body
                body = {
                        "name"        : name,
@@ -252,7 +253,7 @@ class List(Model):
 
                # Submit the report
                response = await self._backend.dbl._fetch(
-                       "/lists/%s/reports" % self.slug, method="POST", body=body,
+                       "/lists/%s/reports" % self.slug, method="POST", body=body, as_user=reported_by,
                )
 
                # Return the report