Skip to content

Commit

Permalink
Added audit trail for changing validated name
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel authored and Iso5786 committed Oct 8, 2024
1 parent fe88205 commit a0e0731
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 62 deletions.
26 changes: 26 additions & 0 deletions apps/badgrsocialauth/providers/eduid/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 3.2.25 on 2024-10-07 11:03

from django.db import migrations, models
import uuid


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='ValidatedNameAuditTrail',
fields=[
('pkid', models.BigAutoField(editable=False, primary_key=True, serialize=False)),
('id', models.UUIDField(default=uuid.uuid4, editable=False, unique=True)),
('action_datetime', models.DateTimeField(auto_now=True)),
('user', models.CharField(blank=True, max_length=254)),
('old_validated_name', models.CharField(blank=True, max_length=255)),
('new_validated_name', models.CharField(blank=True, max_length=255)),
],
),
]
Empty file.
52 changes: 52 additions & 0 deletions apps/badgrsocialauth/providers/eduid/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import logging
import uuid

import django.dispatch
from django.db import models
from django.dispatch import receiver


class ValidatedNameAuditTrail(models.Model):
pkid = models.BigAutoField(primary_key=True, editable=False)
id = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
action_datetime = models.DateTimeField(auto_now=True)
user = models.CharField(max_length=254, blank=True)
old_validated_name = models.CharField(max_length=255, blank=True)
new_validated_name = models.CharField(max_length=255, blank=True)


# Signals doc: https://docs.djangoproject.com/en/3.2/topics/signals/
val_name_audit_trail_signal = django.dispatch.Signal(
providing_args=["user", "old_validated_name", "new_validated_name"]
) # creates a custom signal and specifies the args required.

logger = logging.getLogger(__name__)


@receiver(val_name_audit_trail_signal)
def new_val_name_audit_trail(
sender, user, old_validated_name, new_validated_name, **kwargs
):
try:
if not old_validated_name == new_validated_name:
if old_validated_name is None:
audit_trail = ValidatedNameAuditTrail.objects.create(
user=user,
new_validated_name=new_validated_name,
)
elif new_validated_name is None:
audit_trail = ValidatedNameAuditTrail.objects.create(
user=user,
old_validated_name=old_validated_name,
)
else:
audit_trail = ValidatedNameAuditTrail.objects.create(
user=user,
old_validated_name=old_validated_name,
new_validated_name=new_validated_name,
)
logger.info(
f"val_name_audit_trail created {audit_trail.id} for user {audit_trail.user}"
)
except Exception as e:
logger.error("val_name_audit_trail error: %s" % (e))
Loading

0 comments on commit a0e0731

Please sign in to comment.