diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7e99e36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc \ No newline at end of file diff --git a/audit_logging/file_logging.py b/audit_logging/file_logging.py index 9fed938..599c5cb 100644 --- a/audit_logging/file_logging.py +++ b/audit_logging/file_logging.py @@ -44,58 +44,52 @@ def __init__(self, regular_file, user_details): self.AuditEvent = AuditEvent self.regular_file = regular_file self.user_details = user_details + self.writes = 0 + self.reads = 0 def write(self, *args, **kwargs): res = self.regular_file.write(*args, **kwargs) - log_event( - event='FileWrite', resource_type='file', resource_uuid=self.regular_file.name, - user_details=self.user_details - ) + self.writes += 1 return res def writelines(self, *args, **kwargs): res = self.regular_file.writelines(*args, **kwargs) - log_event( - event='FileWrite', resource_type='file', resource_uuid=self.regular_file.name, - user_details=self.user_details - ) + self.writes += 1 return res def truncate(self, *args, **kwargs): res = self.regular_file.truncate(*args, **kwargs) - log_event( - event='FileWrite', resource_type='file', resource_uuid=self.regular_file.name, - user_details=self.user_details - ) + self.writes += 1 return res def read(self, *args, **kwargs): res = self.regular_file.read(*args, **kwargs) - log_event( - event='FileRead', resource_type='file', resource_uuid=self.regular_file.name, - user_details=self.user_details - ) + self.reads += 1 return res def readline(self, *args, **kwargs): res = self.regular_file.readline(*args, **kwargs) - log_event( - event='FileRead', resource_type='file', resource_uuid=self.regular_file.name, - user_details=self.user_details - ) + self.reads += 1 return res def readlines(self, *args, **kwargs): res = self.regular_file.readlines(*args, **kwargs) - log_event(event='FileRead', resource_type='file', resource_uuid=self.regular_file.name, - user_details=self.user_details - ) + self.reads += 1 return res def __enter__(self, *args, **kwargs): + log_event( + event='FileOpen', resource_type='file', resource_uuid=self.regular_file.name, + user_details=self.user_details + ) return self def __exit__(self, *args, **kwargs): + event = "File{0}{1}Close".format('Read' if self.reads else '', 'Write' if self.writes else '') + log_event( + event=event, resource_type='file', resource_uuid=self.regular_file.name, + user_details=self.user_details + ) self.regular_file.close() def __iter__(self, *args, **kwargs): diff --git a/audit_logging/migrations/0002_auto_20171116_2048.py b/audit_logging/migrations/0002_auto_20171116_2048.py new file mode 100644 index 0000000..540a8b2 --- /dev/null +++ b/audit_logging/migrations/0002_auto_20171116_2048.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.6 on 2017-11-16 20:48 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('audit_logging', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='auditevent', + name='event', + field=models.CharField(blank=True, max_length=24, null=True), + ), + ] diff --git a/audit_logging/models.py b/audit_logging/models.py index d5def5a..289aa7b 100644 --- a/audit_logging/models.py +++ b/audit_logging/models.py @@ -26,7 +26,7 @@ class AuditEvent(models.Model): serialize=False, auto_created=True, primary_key=True) - event = models.CharField(max_length=16, null=True, blank=True) + event = models.CharField(max_length=24, null=True, blank=True) username = models.CharField(max_length=255, null=True, blank=False) ip = models.GenericIPAddressField(null=True, blank=True) email = models.EmailField(null=True, blank=True)