core: fix error when raising SkipObject in mapping (cherry-pick #10153) (#10173)

core: fix error when raising SkipObject in mapping (#10153)

* core: fix error when raising SkipObject in mapping



* fix events not being saved

thanks tests



---------

Signed-off-by: Jens Langhammer <jens@goauthentik.io>
Co-authored-by: Jens L <jens@goauthentik.io>
This commit is contained in:
gcp-cherry-pick-bot[bot]
2024-06-19 23:45:06 +09:00
committed by GitHub
parent 7b7a7e3073
commit 9ea517d606
3 changed files with 17 additions and 3 deletions

View File

@ -76,8 +76,11 @@ class PropertyMappingEvaluator(BaseEvaluator):
) )
if "request" in self._context: if "request" in self._context:
req: PolicyRequest = self._context["request"] req: PolicyRequest = self._context["request"]
if req.http_request:
event.from_http(req.http_request, req.user) event.from_http(req.http_request, req.user)
return return
elif req.user:
event.set_user(req.user)
event.save() event.save()
def evaluate(self, *args, **kwargs) -> Any: def evaluate(self, *args, **kwargs) -> Any:

View File

@ -16,3 +16,7 @@ class SkipObjectException(PropertyMappingExpressionException):
"""Exception which can be raised in a property mapping to skip syncing an object. """Exception which can be raised in a property mapping to skip syncing an object.
Only applies to Property mappings which sync objects, and not on mappings which transitively Only applies to Property mappings which sync objects, and not on mappings which transitively
apply to a single user""" apply to a single user"""
def __init__(self) -> None:
# For this class only, both of these are set by the function evaluating the property mapping
super().__init__(exc=None, mapping=None)

View File

@ -4,7 +4,10 @@ from django.db.models import QuerySet
from django.http import HttpRequest from django.http import HttpRequest
from authentik.core.expression.evaluator import PropertyMappingEvaluator from authentik.core.expression.evaluator import PropertyMappingEvaluator
from authentik.core.expression.exceptions import PropertyMappingExpressionException from authentik.core.expression.exceptions import (
PropertyMappingExpressionException,
SkipObjectException,
)
from authentik.core.models import PropertyMapping, User from authentik.core.models import PropertyMapping, User
@ -57,6 +60,10 @@ class PropertyMappingManager:
mapping.set_context(user, request, **kwargs) mapping.set_context(user, request, **kwargs)
try: try:
value = mapping.evaluate(mapping.model.expression) value = mapping.evaluate(mapping.model.expression)
except SkipObjectException as exc:
exc.exc = exc
exc.mapping = mapping
raise exc from exc
except PropertyMappingExpressionException as exc: except PropertyMappingExpressionException as exc:
raise exc from exc raise exc from exc
except Exception as exc: except Exception as exc: