policies: add types action to policy API, use MetaNameSerializer
This commit is contained in:
parent
bd6a473d4f
commit
47bde052ca
|
@ -1,17 +1,25 @@
|
||||||
"""policy API Views"""
|
"""policy API Views"""
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
from django.core.exceptions import ObjectDoesNotExist
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
from rest_framework.mixins import ListModelMixin
|
from django.shortcuts import reverse
|
||||||
|
from drf_yasg2.utils import swagger_auto_schema
|
||||||
|
from rest_framework.decorators import action
|
||||||
from rest_framework.request import Request
|
from rest_framework.request import Request
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.serializers import (
|
from rest_framework.serializers import (
|
||||||
ModelSerializer,
|
ModelSerializer,
|
||||||
PrimaryKeyRelatedField,
|
PrimaryKeyRelatedField,
|
||||||
Serializer,
|
|
||||||
SerializerMethodField,
|
SerializerMethodField,
|
||||||
)
|
)
|
||||||
from rest_framework.viewsets import GenericViewSet, ModelViewSet, ReadOnlyModelViewSet
|
from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet
|
||||||
|
|
||||||
|
from authentik.core.api.utils import (
|
||||||
|
CacheSerializer,
|
||||||
|
MetaNameSerializer,
|
||||||
|
TypeCreateSerializer,
|
||||||
|
)
|
||||||
|
from authentik.lib.templatetags.authentik_utils import verbose_name
|
||||||
|
from authentik.lib.utils.reflection import all_subclasses
|
||||||
from authentik.policies.models import Policy, PolicyBinding, PolicyBindingModel
|
from authentik.policies.models import Policy, PolicyBinding, PolicyBindingModel
|
||||||
|
|
||||||
|
|
||||||
|
@ -45,7 +53,7 @@ class PolicyBindingModelForeignKey(PrimaryKeyRelatedField):
|
||||||
return correct_model.pk
|
return correct_model.pk
|
||||||
|
|
||||||
|
|
||||||
class PolicySerializer(ModelSerializer):
|
class PolicySerializer(ModelSerializer, MetaNameSerializer):
|
||||||
"""Policy Serializer"""
|
"""Policy Serializer"""
|
||||||
|
|
||||||
_resolve_inheritance: bool
|
_resolve_inheritance: bool
|
||||||
|
@ -58,7 +66,7 @@ class PolicySerializer(ModelSerializer):
|
||||||
|
|
||||||
def get_object_type(self, obj):
|
def get_object_type(self, obj):
|
||||||
"""Get object type so that we know which API Endpoint to use to get the full object"""
|
"""Get object type so that we know which API Endpoint to use to get the full object"""
|
||||||
return obj._meta.object_name.lower().replace("provider", "")
|
return obj._meta.object_name.lower().replace("policy", "")
|
||||||
|
|
||||||
def to_representation(self, instance: Policy):
|
def to_representation(self, instance: Policy):
|
||||||
# pyright: reportGeneralTypeIssues=false
|
# pyright: reportGeneralTypeIssues=false
|
||||||
|
@ -71,7 +79,14 @@ class PolicySerializer(ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|
||||||
model = Policy
|
model = Policy
|
||||||
fields = ["pk", "name", "execution_logging", "object_type"]
|
fields = [
|
||||||
|
"pk",
|
||||||
|
"name",
|
||||||
|
"execution_logging",
|
||||||
|
"object_type",
|
||||||
|
"verbose_name",
|
||||||
|
"verbose_name_plural",
|
||||||
|
]
|
||||||
depth = 3
|
depth = 3
|
||||||
|
|
||||||
|
|
||||||
|
@ -88,6 +103,28 @@ class PolicyViewSet(ReadOnlyModelViewSet):
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
return Policy.objects.select_subclasses()
|
return Policy.objects.select_subclasses()
|
||||||
|
|
||||||
|
@swagger_auto_schema(responses={200: TypeCreateSerializer(many=True)})
|
||||||
|
@action(detail=False)
|
||||||
|
def types(self, request: Request) -> Response:
|
||||||
|
"""Get all creatable policy types"""
|
||||||
|
data = []
|
||||||
|
for subclass in all_subclasses(self.queryset.model):
|
||||||
|
data.append(
|
||||||
|
{
|
||||||
|
"name": verbose_name(subclass),
|
||||||
|
"description": subclass.__doc__,
|
||||||
|
"link": reverse("authentik_admin:policy-create")
|
||||||
|
+ f"?type={subclass.__name__}",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return Response(TypeCreateSerializer(data, many=True).data)
|
||||||
|
|
||||||
|
@swagger_auto_schema(responses={200: CacheSerializer(many=False)})
|
||||||
|
@action(detail=False)
|
||||||
|
def cached(self, request: Request) -> Response:
|
||||||
|
"""Info about cached policies"""
|
||||||
|
return Response(data={"count": len(cache.keys("policy_*"))})
|
||||||
|
|
||||||
|
|
||||||
class PolicyBindingSerializer(ModelSerializer):
|
class PolicyBindingSerializer(ModelSerializer):
|
||||||
"""PolicyBinding Serializer"""
|
"""PolicyBinding Serializer"""
|
||||||
|
@ -124,14 +161,3 @@ class PolicyBindingViewSet(ModelViewSet):
|
||||||
serializer_class = PolicyBindingSerializer
|
serializer_class = PolicyBindingSerializer
|
||||||
filterset_fields = ["policy", "target", "enabled", "order", "timeout"]
|
filterset_fields = ["policy", "target", "enabled", "order", "timeout"]
|
||||||
search_fields = ["policy__name"]
|
search_fields = ["policy__name"]
|
||||||
|
|
||||||
|
|
||||||
class PolicyCacheViewSet(ListModelMixin, GenericViewSet):
|
|
||||||
"""Info about cached policies"""
|
|
||||||
|
|
||||||
queryset = Policy.objects.none()
|
|
||||||
serializer_class = Serializer
|
|
||||||
|
|
||||||
def list(self, request: Request) -> Response:
|
|
||||||
"""Info about cached policies"""
|
|
||||||
return Response(data={"pagination": {"count": len(cache.keys("policy_*"))}})
|
|
||||||
|
|
Reference in New Issue