2022-02-24 19:42:12 +01:00
|
|
|
# w.c.s. - web application for online forms
|
|
|
|
# Copyright (C) 2005-2022 Entr'ouvert
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2023-01-09 18:53:37 +01:00
|
|
|
import datetime
|
|
|
|
|
2022-02-24 19:42:12 +01:00
|
|
|
from django.utils.timezone import now
|
2022-02-24 22:47:53 +01:00
|
|
|
from quixote import get_publisher, get_request
|
2022-02-24 19:42:12 +01:00
|
|
|
|
|
|
|
from wcs import sql
|
2022-02-24 22:47:53 +01:00
|
|
|
from wcs.qommon import _
|
2022-02-24 19:42:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Audit(sql.Audit):
|
|
|
|
id = None
|
|
|
|
timestamp = None
|
|
|
|
action = None
|
|
|
|
url = None
|
|
|
|
user_id = None
|
|
|
|
object_type = None # (formdef, carddef, etc.)
|
|
|
|
object_id = None
|
|
|
|
data_id = None # (for formdata and carddata)
|
|
|
|
extra_data = None
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def record(cls, action, obj=None, user_id=None, **kwargs):
|
|
|
|
audit = cls()
|
|
|
|
audit.action = action
|
|
|
|
audit.timestamp = now()
|
|
|
|
request = get_request()
|
|
|
|
user = None
|
|
|
|
if user_id:
|
|
|
|
audit.user_id = user_id
|
|
|
|
user = get_publisher().user_class.get(audit.user_id, ignore_errors=True)
|
|
|
|
elif request:
|
|
|
|
user = request.get_user()
|
2023-03-25 13:38:29 +01:00
|
|
|
if user and user.is_api_user:
|
|
|
|
# do not audit API calls
|
|
|
|
return
|
2022-02-24 19:42:12 +01:00
|
|
|
if user and hasattr(user, 'id'):
|
|
|
|
audit.user_id = user.id
|
|
|
|
if request:
|
|
|
|
audit.url = request.get_path_query()
|
|
|
|
if obj:
|
|
|
|
if hasattr(obj, '_formdef'): # formdata or carddata
|
|
|
|
audit.data_id = obj.id
|
|
|
|
obj = obj._formdef
|
|
|
|
audit.object_type = obj.xml_root_node
|
|
|
|
audit.object_id = obj.id
|
|
|
|
audit.extra_data = kwargs
|
|
|
|
audit.frozen = {
|
|
|
|
'user_email': getattr(user, 'email', None),
|
|
|
|
'user_full_name': getattr(user, 'display_name', None),
|
|
|
|
'user_nameid': getattr(user, 'nameid', None),
|
|
|
|
'object_slug': getattr(obj, 'slug', None),
|
|
|
|
'object_name': getattr(obj, 'name', None),
|
|
|
|
}
|
|
|
|
audit.store()
|
2022-02-24 22:47:53 +01:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_action_labels(cls):
|
|
|
|
return {
|
|
|
|
'listing': _('Listing'),
|
|
|
|
'export.csv': _('CSV Export'),
|
|
|
|
'export.ods': _('ODS Export'),
|
|
|
|
'download file': _('Download of attached file'),
|
|
|
|
'download files': _('Download of attached files (bundle)'),
|
2023-12-31 11:09:08 +01:00
|
|
|
'redirect to remote stored file': _('Redirect to remote stored file'),
|
2022-02-24 22:47:53 +01:00
|
|
|
'view': _('View Data'),
|
2023-12-07 17:46:06 +01:00
|
|
|
'settings': _('Change to global settings'),
|
2022-02-24 22:47:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
def get_action_description(self):
|
|
|
|
action_label = self.get_action_labels().get(self.action, self.action)
|
|
|
|
obj_name = self.frozen.get('object_name')
|
|
|
|
if self.object_type and self.object_id:
|
|
|
|
obj_class = get_publisher().get_object_class(self.object_type)
|
|
|
|
obj = obj_class.get(self.object_id, ignore_errors=True)
|
|
|
|
if obj:
|
|
|
|
obj_name = obj.name
|
2023-12-07 17:46:06 +01:00
|
|
|
parts = [str(action_label)]
|
|
|
|
if obj_name:
|
|
|
|
parts.append(obj_name)
|
2022-02-24 22:47:53 +01:00
|
|
|
if self.data_id:
|
|
|
|
parts.append(str(self.data_id))
|
2023-12-07 17:46:06 +01:00
|
|
|
if self.extra_data:
|
|
|
|
if self.extra_data.get('extra_label'):
|
|
|
|
parts.append(self.extra_data.get('extra_label'))
|
|
|
|
elif self.extra_data.get('cfg_key'):
|
|
|
|
parts.append(self.extra_data.get('cfg_key'))
|
2022-02-24 22:47:53 +01:00
|
|
|
return ' - '.join(parts)
|
2023-01-09 18:53:37 +01:00
|
|
|
|
|
|
|
@classmethod
|
2023-01-14 09:42:57 +01:00
|
|
|
def clean(cls, publisher=None, **kwargs):
|
2023-01-17 17:25:13 +01:00
|
|
|
audit_retention_days = (publisher or get_publisher()).get_site_option('audit-retention-days') or 365
|
2023-01-09 18:53:37 +01:00
|
|
|
Audit.wipe(clause=[sql.Less('timestamp', now() - datetime.timedelta(days=int(audit_retention_days)))])
|