Просмотр исходного кода

implements #2933 - username in webhooks

John Anderson 7 лет назад
Родитель
Сommit
2170eedf08
4 измененных файлов с 8 добавлено и 5 удалено
  1. 1 0
      CHANGELOG.md
  2. 2 2
      netbox/extras/middleware.py
  3. 3 2
      netbox/extras/webhooks.py
  4. 2 1
      netbox/extras/webhooks_worker.py

+ 1 - 0
CHANGELOG.md

@@ -2,6 +2,7 @@ v2.5.9 (FUTURE)
 
 ## Enhancements
 
+* [#2933](https://github.com/digitalocean/netbox/issues/2933) - Add username to outbound webhook requests
 * [#3011](https://github.com/digitalocean/netbox/issues/3011) - Add SSL support for django-rq (requires django-rq v1.3.1+)
 
 ## Bug Fixes

+ 2 - 2
netbox/extras/middleware.py

@@ -37,7 +37,7 @@ def _record_object_deleted(request, instance, **kwargs):
     if hasattr(instance, 'log_change'):
         instance.log_change(request.user, request.id, OBJECTCHANGE_ACTION_DELETE)
 
-    enqueue_webhooks(instance, OBJECTCHANGE_ACTION_DELETE)
+    enqueue_webhooks(instance, request.user, OBJECTCHANGE_ACTION_DELETE)
 
 
 class ObjectChangeMiddleware(object):
@@ -83,7 +83,7 @@ class ObjectChangeMiddleware(object):
                 obj.log_change(request.user, request.id, action)
 
             # Enqueue webhooks
-            enqueue_webhooks(obj, action)
+            enqueue_webhooks(obj, request.user, action)
 
         # Housekeeping: 1% chance of clearing out expired ObjectChanges
         if _thread_locals.changed_objects and settings.CHANGELOG_RETENTION and random.randint(1, 100) == 1:

+ 3 - 2
netbox/extras/webhooks.py

@@ -9,7 +9,7 @@ from utilities.api import get_serializer_for_model
 from .constants import WEBHOOK_MODELS
 
 
-def enqueue_webhooks(instance, action):
+def enqueue_webhooks(instance, user, action):
     """
     Find Webhook(s) assigned to this instance + action and enqueue them
     to be processed
@@ -47,5 +47,6 @@ def enqueue_webhooks(instance, action):
                 serializer.data,
                 instance._meta.model_name,
                 action,
-                str(datetime.datetime.now())
+                str(datetime.datetime.now()),
+                user.username
             )

+ 2 - 1
netbox/extras/webhooks_worker.py

@@ -10,7 +10,7 @@ from extras.constants import WEBHOOK_CT_JSON, WEBHOOK_CT_X_WWW_FORM_ENCODED, OBJ
 
 
 @job('default')
-def process_webhook(webhook, data, model_name, event, timestamp):
+def process_webhook(webhook, data, model_name, event, timestamp, username):
     """
     Make a POST request to the defined Webhook
     """
@@ -18,6 +18,7 @@ def process_webhook(webhook, data, model_name, event, timestamp):
         'event': dict(OBJECTCHANGE_ACTION_CHOICES)[event].lower(),
         'timestamp': timestamp,
         'model': model_name,
+        'username': username,
         'data': data
     }
     headers = {