From c63ba3f378fceada820979ead5fd08a6b3253bae Mon Sep 17 00:00:00 2001 From: sdimovv <36302090+sdimovv@users.noreply.github.com> Date: Fri, 6 Jan 2023 08:49:28 +0000 Subject: [PATCH] blueprints: Fix resolve model_name in `!Find` tag (#4371) Resolve model_name in !Find tag --- authentik/blueprints/tests/fixtures/tags.yaml | 2 +- authentik/blueprints/v1/common.py | 16 ++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/authentik/blueprints/tests/fixtures/tags.yaml b/authentik/blueprints/tests/fixtures/tags.yaml index 46364cf84..a57e207dd 100644 --- a/authentik/blueprints/tests/fixtures/tags.yaml +++ b/authentik/blueprints/tests/fixtures/tags.yaml @@ -25,7 +25,7 @@ entries: [slug, default-source-authentication], ] enrollment_flow: - !Find [authentik_flows.Flow, [slug, default-source-enrollment]] + !Find [!Format ["%s", authentik_flows.Flow], [slug, default-source-enrollment]] - attrs: expression: return True identifiers: diff --git a/authentik/blueprints/v1/common.py b/authentik/blueprints/v1/common.py index 40f2cce3e..95751cf1d 100644 --- a/authentik/blueprints/v1/common.py +++ b/authentik/blueprints/v1/common.py @@ -287,15 +287,12 @@ class Format(YAMLTag): class Find(YAMLTag): """Find any object""" - model_name: str + model_name: str | YAMLTag conditions: list[list] - model_class: type[Model] - def __init__(self, loader: "BlueprintLoader", node: SequenceNode) -> None: super().__init__() - self.model_name = node.value[0].value - self.model_class = apps.get_model(*self.model_name.split(".")) + self.model_name = loader.construct_object(node.value[0]) self.conditions = [] for raw_node in node.value[1:]: values = [] @@ -304,6 +301,13 @@ class Find(YAMLTag): self.conditions.append(values) def resolve(self, entry: BlueprintEntry, blueprint: Blueprint) -> Any: + if isinstance(self.model_name, YAMLTag): + model_name = self.model_name.resolve(entry, blueprint) + else: + model_name = self.model_name + + model_class = apps.get_model(*model_name.split(".")) + query = Q() for cond in self.conditions: if isinstance(cond[0], YAMLTag): @@ -315,7 +319,7 @@ class Find(YAMLTag): else: query_value = cond[1] query &= Q(**{query_key: query_value}) - instance = self.model_class.objects.filter(query).first() + instance = model_class.objects.filter(query).first() if instance: return instance.pk return None