diff --git a/Changelog.md b/Changelog.md index 6788fe1..cc4116c 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,4 +1,7 @@ +## [unreleased - 03-12-2021] +- Allow `username` logging when user is authenticated + ## 0.4.0 (29-01-2020) ### Note worthy changes diff --git a/src/django_audit_log/logger.py b/src/django_audit_log/logger.py index bd0601b..cf78584 100644 --- a/src/django_audit_log/logger.py +++ b/src/django_audit_log/logger.py @@ -63,6 +63,7 @@ def set_user_from_request( email=getattr(user, 'email', '') if user else '', roles=roles, ip=get_client_ip(request), + username=request.user.username if user and user.is_authenticated else '', ) return self diff --git a/tests/test_logger.py b/tests/test_logger.py index d68c9ca..f3998df 100644 --- a/tests/test_logger.py +++ b/tests/test_logger.py @@ -77,6 +77,7 @@ def test_set_user_from_request(self): self.assertEqual(audit_log.user['email'], 'username@host.com') self.assertEqual(audit_log.user['roles'], ['testgroup']) self.assertEqual(audit_log.user['ip'], '127.0.0.1') + self.assertEqual(audit_log.user['username'], 'username') def test_extras_user_from_request(self): user = User.objects.create_user(username='username', email='username@host.com') @@ -97,6 +98,7 @@ def test_extras_user_from_request(self): self.assertEqual(extras['user']['email'], 'username@host.com') self.assertEqual(extras['user']['roles'], ['testgroup']) self.assertEqual(extras['user']['ip'], '127.0.0.1') + self.assertEqual(extras['user']['username'], 'username') def test_get_headers_from_response(self): expected_headers = {