Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to ignore request.user validation #233

Merged
merged 1 commit into from
Sep 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ Below are some of the settings you may want to use. These should be defined in y
determined by the `pre_save` handler sees that there are no changed fields. We are keeping it off by default so that
projects that wish to use this (potentially less `CRUDEvent`) can choose to turn it on! And those that do not want it (yet or ever),
or those that do not closely follow the release notes of this project will have one less worry when upgrading.


* `DJANGO_EASY_AUDIT_CHECK_IF_REQUEST_USER_EXISTS`

By default this is `True`, but this allows the calling project to make easyaudit ignore user validation on audit event creation.
This is useful when you have a app with soft delete or no delete on users model. With this set to `False`, easyaudit only fetch `request.user` for audit event creation, no db check is made, meaning you can speed up audit events creation and save some DB calls.

* `DJANGO_EASY_AUDIT_READONLY_EVENTS`

Expand Down
71 changes: 29 additions & 42 deletions easyaudit/signals/model_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ def should_audit(instance):
return True


def get_current_user_details():
jheld marked this conversation as resolved.
Show resolved Hide resolved
user_id = None
user_pk_as_string = None

try:
user = get_current_user()
if user and not isinstance(user, AnonymousUser):
if getattr(settings, "DJANGO_EASY_AUDIT_CHECK_IF_REQUEST_USER_EXISTS", True):
# validate that the user still exists
user = get_user_model().objects.get(pk=user.pk)
user_id, user_pk_as_string = user.id, str(user.pk)
except:
pass

return user_id, user_pk_as_string


# signals
def pre_save(sender, instance, raw, using, update_fields, **kwargs):
"""https://docs.djangoproject.com/es/1.10/ref/signals/#post-save"""
Expand Down Expand Up @@ -76,15 +93,7 @@ def pre_save(sender, instance, raw, using, update_fields, **kwargs):
event_type = CRUDEvent.UPDATE

# user
try:
user = get_current_user()
# validate that the user still exists
user = get_user_model().objects.get(pk=user.pk)
except:
user = None

if isinstance(user, AnonymousUser):
user = None
user_id, user_pk_as_string = get_current_user_details()

# callbacks
kwargs['request'] = get_current_request() # make request available for callbacks
Expand All @@ -106,9 +115,9 @@ def crud_flow():
'changed_fields': changed_fields,
'content_type_id': c_t.id,
'object_id': instance.pk,
'user_id': getattr(user, 'id', None),
'user_id': user_id,
'datetime': timezone.now(),
'user_pk_as_string': str(user.pk) if user else user
'user_pk_as_string': user_pk_as_string,
})
except Exception as e:
try:
Expand Down Expand Up @@ -142,15 +151,7 @@ def post_save(sender, instance, created, raw, using, update_fields, **kwargs):
event_type = CRUDEvent.CREATE

# user
try:
user = get_current_user()
# validate that the user still exists
user = get_user_model().objects.get(pk=user.pk)
except:
user = None

if isinstance(user, AnonymousUser):
user = None
user_id, user_pk_as_string = get_current_user_details()

# callbacks
kwargs['request'] = get_current_request() # make request available for callbacks
Expand All @@ -172,9 +173,9 @@ def crud_flow():
'object_json_repr': object_json_repr,
'content_type_id': c_t.id,
'object_id': instance.pk,
'user_id': getattr(user, 'id', None),
'user_id': user_id,
'datetime': timezone.now(),
'user_pk_as_string': str(user.pk) if user else user
'user_pk_as_string': user_pk_as_string
})
except Exception as e:
try:
Expand Down Expand Up @@ -253,15 +254,8 @@ def m2m_changed(sender, instance, action, reverse, model, pk_set, using, **kwarg
event_type = CRUDEvent.M2M_CHANGE # just in case

# user
try:
user = get_current_user()
# validate that the user still exists
user = get_user_model().objects.get(pk=user.pk)
except:
user = None
user_id, user_pk_as_string = get_current_user_details()

if isinstance(user, AnonymousUser):
user = None
c_t = ContentType.objects.get_for_model(instance)

def crud_flow():
Expand All @@ -278,9 +272,9 @@ def crud_flow():
'changed_fields': changed_fields,
'content_type_id': c_t.id,
'object_id': instance.pk,
'user_id': getattr(user, 'id', None),
'user_id': user_id,
'datetime': timezone.now(),
'user_pk_as_string': str(user.pk) if user else user
'user_pk_as_string': user_pk_as_string
})
except Exception as e:
try:
Expand Down Expand Up @@ -308,15 +302,8 @@ def post_delete(sender, instance, using, **kwargs):
object_json_repr = serializers.serialize("json", [instance])

# user
try:
user = get_current_user()
# validate that the user still exists
user = get_user_model().objects.get(pk=user.pk)
except:
user = None
user_id, user_pk_as_string = get_current_user_details()

if isinstance(user, AnonymousUser):
user = None
c_t = ContentType.objects.get_for_model(instance)

# object id to be used later
Expand All @@ -332,9 +319,9 @@ def crud_flow():
'object_json_repr': object_json_repr,
'content_type_id': c_t.id,
'object_id': obj_id,
'user_id': getattr(user, 'id', None),
'user_id': user_id,
'datetime': timezone.now(),
'user_pk_as_string': str(user.pk) if user else user
'user_pk_as_string': user_pk_as_string
})

except Exception as e:
Expand Down