blueprints: don't export events by default and exclude anonymous user

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2022-08-31 23:31:48 +02:00
parent 398eb23d31
commit d9775f2822
1 changed files with 16 additions and 4 deletions

View File

@ -3,9 +3,11 @@ from typing import Iterator
from uuid import UUID from uuid import UUID
from django.apps import apps from django.apps import apps
from django.db.models import Q from django.contrib.auth import get_user_model
from django.db.models import Model, Q, QuerySet
from django.utils.timezone import now from django.utils.timezone import now
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from guardian.shortcuts import get_anonymous_user
from yaml import dump from yaml import dump
from authentik.blueprints.v1.common import ( from authentik.blueprints.v1.common import (
@ -16,6 +18,7 @@ from authentik.blueprints.v1.common import (
) )
from authentik.blueprints.v1.importer import is_model_allowed from authentik.blueprints.v1.importer import is_model_allowed
from authentik.blueprints.v1.labels import LABEL_AUTHENTIK_GENERATED from authentik.blueprints.v1.labels import LABEL_AUTHENTIK_GENERATED
from authentik.events.models import Event
from authentik.flows.models import Flow, FlowStageBinding, Stage from authentik.flows.models import Flow, FlowStageBinding, Stage
from authentik.policies.models import Policy, PolicyBinding from authentik.policies.models import Policy, PolicyBinding
from authentik.stages.prompt.models import PromptStage from authentik.stages.prompt.models import PromptStage
@ -24,10 +27,12 @@ from authentik.stages.prompt.models import PromptStage
class Exporter: class Exporter:
"""Export flow with attached stages into yaml""" """Export flow with attached stages into yaml"""
excluded_models = [] excluded_models: list[type[Model]] = []
def __init__(self): def __init__(self):
self.excluded_models = [] self.excluded_models = [
Event,
]
def get_entries(self) -> Iterator[BlueprintEntry]: def get_entries(self) -> Iterator[BlueprintEntry]:
"""Get blueprint entries""" """Get blueprint entries"""
@ -36,9 +41,16 @@ class Exporter:
continue continue
if model in self.excluded_models: if model in self.excluded_models:
continue continue
for obj in model.objects.all(): for obj in self.get_model_instances(model):
yield BlueprintEntry.from_model(obj) yield BlueprintEntry.from_model(obj)
def get_model_instances(self, model: type[Model]) -> QuerySet:
"""Return a queryset for `model`. Can be used to filter some
objects on some models"""
if model == get_user_model():
return model.objects.exclude(pk=get_anonymous_user().pk)
return model.objects.all()
def _pre_export(self, blueprint: Blueprint): def _pre_export(self, blueprint: Blueprint):
"""Hook to run anything pre-export""" """Hook to run anything pre-export"""