From c8ebd9f74b3139e1cad002a028ebd8542956a44a Mon Sep 17 00:00:00 2001 From: Marc 'risson' Schmitt Date: Fri, 13 Jun 2025 17:29:14 +0200 Subject: [PATCH] wip Signed-off-by: Marc 'risson' Schmitt --- authentik/core/api/files.py | 7 +++- ...le_location_alter_file_content_and_more.py | 32 +++++++++++++++++++ authentik/core/models.py | 21 +++++++++++- 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 authentik/core/migrations/0050_file_location_alter_file_content_and_more.py diff --git a/authentik/core/api/files.py b/authentik/core/api/files.py index 4d9440e908..5f8aaeb85c 100644 --- a/authentik/core/api/files.py +++ b/authentik/core/api/files.py @@ -15,7 +15,9 @@ class FileSerializer(ModelSerializer): "pk", "name", "content", + "location", "private", + "url", ) @@ -24,4 +26,7 @@ class FileViewSet(UsedByMixin, ModelViewSet): serializer_class = FileSerializer filterset_fields = ("private",) ordering = ("name",) - search_fields = ("name",) + search_fields = ( + "name", + "location", + ) diff --git a/authentik/core/migrations/0050_file_location_alter_file_content_and_more.py b/authentik/core/migrations/0050_file_location_alter_file_content_and_more.py new file mode 100644 index 0000000000..3899f0c03b --- /dev/null +++ b/authentik/core/migrations/0050_file_location_alter_file_content_and_more.py @@ -0,0 +1,32 @@ +# Generated by Django 5.1.11 on 2025-06-13 15:29 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("authentik_core", "0049_file_rename_meta_icon_application_meta_old_icon"), + ] + + operations = [ + migrations.AddField( + model_name="file", + name="location", + field=models.TextField(null=True), + ), + migrations.AlterField( + model_name="file", + name="content", + field=models.BinaryField(null=True), + ), + migrations.AddConstraint( + model_name="file", + constraint=models.CheckConstraint( + condition=models.Q( + ("content__isnull", False), ("location__isnull", False), _connector="OR" + ), + name="one_of_content_location_is_defined", + ), + ), + ] diff --git a/authentik/core/models.py b/authentik/core/models.py index 72c28835fa..6365416a5d 100644 --- a/authentik/core/models.py +++ b/authentik/core/models.py @@ -29,6 +29,7 @@ from authentik.blueprints.models import ManagedModel from authentik.core.expression.exceptions import PropertyMappingExpressionException from authentik.core.types import UILoginButton, UserSettingSerializer from authentik.lib.avatars import get_avatar +from authentik.lib.config import CONFIG from authentik.lib.expression.exceptions import ControlFlowException from authentik.lib.generators import generate_id from authentik.lib.merge import MERGE_LIST_UNIQUE @@ -1107,12 +1108,19 @@ class File(SerializerModel): id = models.UUIDField(primary_key=True, editable=False, default=uuid4) name = models.TextField() - content = models.BinaryField() + content = models.BinaryField(null=True) + location = models.TextField(null=True) public = models.BooleanField(default=False) class Meta: verbose_name = _("File") verbose_name = _("Files") + constraints = ( + models.CheckConstraint( + condition=Q(content__isnull=False) | Q(location__isnull=False), + name="one_of_content_location_is_defined", + ), + ) def __str__(self) -> str: return self.name @@ -1122,3 +1130,14 @@ class File(SerializerModel): from authentik.core.api.files import FileSerializer return FileSerializer + + @property + def url(self) -> str: + if self.content: + return ( + CONFIG.get("web.path", "/")[:-1] + + f"/files/{'public' if self.public else 'private'}/{self.pk}" + ) + elif self.location.startswith("/"): + return CONFIG.get("web.path", "/")[:-1] + self.location + return self.location