api: add capabilities to API, add can_save_media

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-05-20 17:00:47 +02:00
parent f6f93640c5
commit 92106ca4bf
3 changed files with 32 additions and 1 deletions

View File

@ -1,6 +1,10 @@
"""core Configs API""" """core Configs API"""
from os import path
from django.conf import settings
from django.db import models
from drf_spectacular.utils import extend_schema from drf_spectacular.utils import extend_schema
from rest_framework.fields import BooleanField, CharField, ListField from rest_framework.fields import BooleanField, CharField, ChoiceField, ListField
from rest_framework.permissions import AllowAny from rest_framework.permissions import AllowAny
from rest_framework.request import Request from rest_framework.request import Request
from rest_framework.response import Response from rest_framework.response import Response
@ -17,6 +21,12 @@ class FooterLinkSerializer(PassiveSerializer):
name = CharField(read_only=True) name = CharField(read_only=True)
class Capabilities(models.TextChoices):
"""Define capabilities which influence which APIs can/should be used"""
CAN_SAVE_MEDIA = "can_save_media"
class ConfigSerializer(PassiveSerializer): class ConfigSerializer(PassiveSerializer):
"""Serialize authentik Config into DRF Object""" """Serialize authentik Config into DRF Object"""
@ -28,12 +38,22 @@ class ConfigSerializer(PassiveSerializer):
error_reporting_environment = CharField(read_only=True) error_reporting_environment = CharField(read_only=True)
error_reporting_send_pii = BooleanField(read_only=True) error_reporting_send_pii = BooleanField(read_only=True)
capabilities = ListField(child=ChoiceField(choices=Capabilities.choices))
class ConfigView(APIView): class ConfigView(APIView):
"""Read-only view set that returns the current session's Configs""" """Read-only view set that returns the current session's Configs"""
permission_classes = [AllowAny] permission_classes = [AllowAny]
def get_capabilities(self) -> list[Capabilities]:
"""Get all capabilities this server instance supports"""
caps = []
deb_test = settings.DEBUG or settings.TEST
if path.ismount(settings.MEDIA_ROOT) or deb_test:
caps.append(Capabilities.CAN_SAVE_MEDIA)
return caps
@extend_schema(responses={200: ConfigSerializer(many=False)}) @extend_schema(responses={200: ConfigSerializer(many=False)})
def get(self, request: Request) -> Response: def get(self, request: Request) -> Response:
"""Retrive public configuration options""" """Retrive public configuration options"""
@ -45,6 +65,7 @@ class ConfigView(APIView):
"error_reporting_environment": CONFIG.y("error_reporting.environment"), "error_reporting_environment": CONFIG.y("error_reporting.environment"),
"error_reporting_send_pii": CONFIG.y("error_reporting.send_pii"), "error_reporting_send_pii": CONFIG.y("error_reporting.send_pii"),
"ui_footer_links": CONFIG.y("authentik.footer_links"), "ui_footer_links": CONFIG.y("authentik.footer_links"),
"capabilities": self.get_capabilities(),
} }
) )
return Response(config.data) return Response(config.data)

View File

@ -15179,6 +15179,10 @@ components:
readOnly: true readOnly: true
required: required:
- count - count
CapabilitiesEnum:
enum:
- can_save_media
type: string
CaptchaStage: CaptchaStage:
type: object type: object
description: CaptchaStage Serializer description: CaptchaStage Serializer
@ -15362,9 +15366,14 @@ components:
error_reporting_send_pii: error_reporting_send_pii:
type: boolean type: boolean
readOnly: true readOnly: true
capabilities:
type: array
items:
$ref: '#/components/schemas/CapabilitiesEnum'
required: required:
- branding_logo - branding_logo
- branding_title - branding_title
- capabilities
- error_reporting_enabled - error_reporting_enabled
- error_reporting_environment - error_reporting_environment
- error_reporting_send_pii - error_reporting_send_pii

View File

@ -22,6 +22,7 @@ export const DefaultConfig: Config = {
errorReportingEnvironment: "", errorReportingEnvironment: "",
errorReportingSendPii: false, errorReportingSendPii: false,
uiFooterLinks: [], uiFooterLinks: [],
capabilities: [],
}; };
@customElement("ak-sidebar-brand") @customElement("ak-sidebar-brand")