Skip to content

Commit

Permalink
events: always use expiry from current tenant for events, not only wh…
Browse files Browse the repository at this point in the history
…en creating from HTTP request (#11415)

Signed-off-by: Jens Langhammer <[email protected]>
  • Loading branch information
BeryJu authored Sep 17, 2024
1 parent c4d6388 commit 70b5a21
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
13 changes: 6 additions & 7 deletions authentik/events/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
from authentik.root.middleware import ClientIPMiddleware
from authentik.stages.email.utils import TemplateEmailMessage
from authentik.tenants.models import Tenant
from authentik.tenants.utils import get_current_tenant

LOGGER = get_logger()
DISCORD_FIELD_LIMIT = 25
Expand All @@ -58,7 +59,11 @@
def default_event_duration():
"""Default duration an Event is saved.
This is used as a fallback when no brand is available"""
return now() + timedelta(days=365)
try:
tenant = get_current_tenant()
return now() + timedelta_from_string(tenant.event_retention)
except Tenant.DoesNotExist:
return now() + timedelta(days=365)


def default_brand():
Expand Down Expand Up @@ -245,12 +250,6 @@ def from_http(self, request: HttpRequest, user: User | None = None) -> "Event":
if QS_QUERY in self.context["http_request"]["args"]:
wrapped = self.context["http_request"]["args"][QS_QUERY]
self.context["http_request"]["args"] = cleanse_dict(QueryDict(wrapped))
if hasattr(request, "tenant"):
tenant: Tenant = request.tenant
# Because self.created only gets set on save, we can't use it's value here
# hence we set self.created to now and then use it
self.created = now()
self.expires = self.created + timedelta_from_string(tenant.event_retention)
if hasattr(request, "brand"):
brand: Brand = request.brand
self.brand = sanitize_dict(model_to_dict(brand))
Expand Down
3 changes: 2 additions & 1 deletion authentik/events/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.test import TestCase

from authentik.core.models import default_token_key
from authentik.events.models import default_event_duration
from authentik.lib.utils.reflection import get_apps


Expand All @@ -20,7 +21,7 @@ def tester(self: TestModels):
allowed = 0
# Token-like objects need to lookup the current tenant to get the default token length
for field in test_model._meta.fields:
if field.default == default_token_key:
if field.default in [default_token_key, default_event_duration]:
allowed += 1
with self.assertNumQueries(allowed):
str(test_model())
Expand Down

0 comments on commit 70b5a21

Please sign in to comment.