policies: add types action to policy API, use MetaNameSerializer

This commit is contained in:
Jens Langhammer 2021-02-19 16:34:33 +01:00
parent bd6a473d4f
commit 47bde052ca
1 changed files with 43 additions and 17 deletions

View File

@ -1,17 +1,25 @@
"""policy API Views"""
from django.core.cache import cache
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.response import Response
from rest_framework.serializers import (
ModelSerializer,
PrimaryKeyRelatedField,
Serializer,
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
@ -45,7 +53,7 @@ class PolicyBindingModelForeignKey(PrimaryKeyRelatedField):
return correct_model.pk
class PolicySerializer(ModelSerializer):
class PolicySerializer(ModelSerializer, MetaNameSerializer):
"""Policy Serializer"""
_resolve_inheritance: bool
@ -58,7 +66,7 @@ class PolicySerializer(ModelSerializer):
def get_object_type(self, obj):
"""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):
# pyright: reportGeneralTypeIssues=false
@ -71,7 +79,14 @@ class PolicySerializer(ModelSerializer):
class Meta:
model = Policy
fields = ["pk", "name", "execution_logging", "object_type"]
fields = [
"pk",
"name",
"execution_logging",
"object_type",
"verbose_name",
"verbose_name_plural",
]
depth = 3
@ -88,6 +103,28 @@ class PolicyViewSet(ReadOnlyModelViewSet):
def get_queryset(self):
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):
"""PolicyBinding Serializer"""
@ -124,14 +161,3 @@ class PolicyBindingViewSet(ModelViewSet):
serializer_class = PolicyBindingSerializer
filterset_fields = ["policy", "target", "enabled", "order", "timeout"]
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_*"))}})