|
| 1 | +"""Audit models.""" |
| 2 | + |
| 3 | +from django.contrib.auth.models import User |
| 4 | +from django.db import models |
| 5 | +from django.utils.translation import ugettext_lazy as _ |
| 6 | +from django_extensions.db.models import TimeStampedModel |
| 7 | + |
| 8 | +from readthedocs.acl.utils import get_auth_backend |
| 9 | + |
| 10 | + |
| 11 | +class AuditLogManager(models.Manager): |
| 12 | + |
| 13 | + """AuditLog manager.""" |
| 14 | + |
| 15 | + def new(self, action, user=None, request=None, **kwargs): |
| 16 | + """ |
| 17 | + Create an audit log for `action`. |
| 18 | +
|
| 19 | + If user or request are given, |
| 20 | + other fields will be auto-populated from that information. |
| 21 | + """ |
| 22 | + |
| 23 | + actions_requiring_user = (AuditLog.PAGEVIEW, AuditLog.AUTHN, AuditLog.LOGOUT) |
| 24 | + if action in actions_requiring_user and (not user or not request): |
| 25 | + raise TypeError(f'A user and a request is required for the {action} action.') |
| 26 | + if action == AuditLog.PAGEVIEW and 'project' not in kwargs: |
| 27 | + raise TypeError(f'A project is required for the {action} action.') |
| 28 | + |
| 29 | + # Don't save anonymous users. |
| 30 | + if user and user.is_anonymous: |
| 31 | + user = None |
| 32 | + |
| 33 | + if request: |
| 34 | + kwargs['ip'] = request.META.get('REMOTE_ADDR') |
| 35 | + kwargs['browser'] = request.headers.get('User-Agent') |
| 36 | + kwargs.setdefault('resource', request.path_info) |
| 37 | + kwargs.setdefault('auth_backend', get_auth_backend(request)) |
| 38 | + |
| 39 | + return self.create( |
| 40 | + user=user, |
| 41 | + action=action, |
| 42 | + **kwargs, |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +class AuditLog(TimeStampedModel): |
| 47 | + |
| 48 | + """ |
| 49 | + Track user actions for audit purposes. |
| 50 | +
|
| 51 | + A log can be attached to a user and/or project. |
| 52 | + If the user or project are deleted the log will be preserved, |
| 53 | + and the deleted user/project can be accessed via the ``log_*`` attributes. |
| 54 | + """ |
| 55 | + |
| 56 | + PAGEVIEW = 'pageview' |
| 57 | + AUTHN = 'authentication' |
| 58 | + AUTHN_FAILURE = 'authentication-failure' |
| 59 | + LOGOUT = 'log-out' |
| 60 | + |
| 61 | + CHOICES = ( |
| 62 | + (PAGEVIEW, 'Page view'), |
| 63 | + (AUTHN, 'Authentication'), |
| 64 | + (AUTHN_FAILURE, 'Authentication failure'), |
| 65 | + (LOGOUT, 'Log out'), |
| 66 | + ) |
| 67 | + |
| 68 | + user = models.ForeignKey( |
| 69 | + User, |
| 70 | + verbose_name=_('User'), |
| 71 | + null=True, |
| 72 | + on_delete=models.SET_NULL, |
| 73 | + db_index=True, |
| 74 | + ) |
| 75 | + # Extra information in case the user is deleted. |
| 76 | + log_user_id = models.IntegerField( |
| 77 | + _('User ID'), |
| 78 | + blank=True, |
| 79 | + null=True, |
| 80 | + db_index=True, |
| 81 | + ) |
| 82 | + log_user_username = models.CharField( |
| 83 | + _('Username'), |
| 84 | + max_length=150, |
| 85 | + blank=True, |
| 86 | + null=True, |
| 87 | + db_index=True, |
| 88 | + ) |
| 89 | + |
| 90 | + project = models.ForeignKey( |
| 91 | + 'projects.Project', |
| 92 | + verbose_name=_('Project'), |
| 93 | + null=True, |
| 94 | + db_index=True, |
| 95 | + on_delete=models.SET_NULL, |
| 96 | + ) |
| 97 | + # Extra information in case the project is deleted. |
| 98 | + log_project_id = models.IntegerField( |
| 99 | + _('Project ID'), |
| 100 | + blank=True, |
| 101 | + null=True, |
| 102 | + db_index=True, |
| 103 | + ) |
| 104 | + log_project_slug = models.CharField( |
| 105 | + _('Project slug'), |
| 106 | + max_length=63, |
| 107 | + blank=True, |
| 108 | + null=True, |
| 109 | + db_index=True, |
| 110 | + ) |
| 111 | + |
| 112 | + action = models.CharField( |
| 113 | + _('Action'), |
| 114 | + max_length=150, |
| 115 | + choices=CHOICES, |
| 116 | + ) |
| 117 | + auth_backend = models.CharField( |
| 118 | + _('Auth backend'), |
| 119 | + max_length=250, |
| 120 | + blank=True, |
| 121 | + null=True, |
| 122 | + ) |
| 123 | + ip = models.GenericIPAddressField( |
| 124 | + _('IP address'), |
| 125 | + blank=True, |
| 126 | + null=True, |
| 127 | + ) |
| 128 | + browser = models.CharField( |
| 129 | + _('Browser user-agent'), |
| 130 | + max_length=250, |
| 131 | + blank=True, |
| 132 | + null=True, |
| 133 | + ) |
| 134 | + # Resource can be a path, |
| 135 | + # set it slightly greater than ``HTMLFile.path``. |
| 136 | + resource = models.CharField( |
| 137 | + _('Resource'), |
| 138 | + max_length=5500, |
| 139 | + blank=True, |
| 140 | + null=True, |
| 141 | + ) |
| 142 | + |
| 143 | + objects = AuditLogManager() |
| 144 | + |
| 145 | + def save(self, **kwargs): |
| 146 | + if self.user: |
| 147 | + self.log_user_id = self.user.id |
| 148 | + self.log_user_username = self.user.username |
| 149 | + if self.project: |
| 150 | + self.log_project_id = self.project.id |
| 151 | + self.log_project_slug = self.project.slug |
| 152 | + super().save(**kwargs) |
| 153 | + |
| 154 | + def __str__(self): |
| 155 | + return self.action |
0 commit comments