Skip to content

Commit

Permalink
core: fix error when raising SkipObject in mapping (cherry-pick #10153)…
Browse files Browse the repository at this point in the history
… (#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 <[email protected]>
Co-authored-by: Jens L <[email protected]>
  • Loading branch information
gcp-cherry-pick-bot[bot] and BeryJu authored Jun 19, 2024
1 parent 7b7a7e3 commit 9ea517d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
7 changes: 5 additions & 2 deletions authentik/core/expression/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,11 @@ def handle_error(self, exc: Exception, expression_source: str):
)
if "request" in self._context:
req: PolicyRequest = self._context["request"]
event.from_http(req.http_request, req.user)
return
if req.http_request:
event.from_http(req.http_request, req.user)
return
elif req.user:
event.set_user(req.user)
event.save()

def evaluate(self, *args, **kwargs) -> Any:
Expand Down
4 changes: 4 additions & 0 deletions authentik/core/expression/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ class SkipObjectException(PropertyMappingExpressionException):
"""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
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)
9 changes: 8 additions & 1 deletion authentik/lib/sync/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
from django.http import HttpRequest

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


Expand Down Expand Up @@ -57,6 +60,10 @@ def iter_eval(
mapping.set_context(user, request, **kwargs)
try:
value = mapping.evaluate(mapping.model.expression)
except SkipObjectException as exc:
exc.exc = exc
exc.mapping = mapping
raise exc from exc
except PropertyMappingExpressionException as exc:
raise exc from exc
except Exception as exc:
Expand Down

0 comments on commit 9ea517d

Please sign in to comment.