diff --git a/authentik/core/api/providers.py b/authentik/core/api/providers.py index 67b2427fc..a5095dcde 100644 --- a/authentik/core/api/providers.py +++ b/authentik/core/api/providers.py @@ -1,4 +1,6 @@ """Provider API Views""" +from django.db.models import QuerySet +from django.db.models.query import Q from django.utils.translation import gettext_lazy as _ from django_filters.filters import BooleanFilter from django_filters.filterset import FilterSet @@ -56,17 +58,22 @@ class ProviderSerializer(ModelSerializer, MetaNameSerializer): class ProviderFilter(FilterSet): - """Filter for groups""" + """Filter for providers""" - application__isnull = BooleanFilter( - field_name="application", - lookup_expr="isnull", - ) + application__isnull = BooleanFilter(method="filter_application__isnull") backchannel_only = BooleanFilter( method="filter_backchannel_only", ) - def filter_backchannel_only(self, queryset, name, value): + def filter_application__isnull(self, queryset: QuerySet, name, value): + """Only return providers that are neither assigned to application, + both as provider or application provider""" + return queryset.filter( + Q(backchannel_application__isnull=value, is_backchannel=True) + | Q(application__isnull=value) + ) + + def filter_backchannel_only(self, queryset: QuerySet, name, value): """Only return backchannel providers""" return queryset.filter(is_backchannel=value) diff --git a/authentik/providers/ldap/api.py b/authentik/providers/ldap/api.py index 52c4cdc60..96b3a5926 100644 --- a/authentik/providers/ldap/api.py +++ b/authentik/providers/ldap/api.py @@ -1,4 +1,8 @@ """LDAPProvider API Views""" +from django.db.models import QuerySet +from django.db.models.query import Q +from django_filters.filters import BooleanFilter +from django_filters.filterset import FilterSet from rest_framework.fields import CharField, ListField, SerializerMethodField from rest_framework.serializers import ModelSerializer from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet @@ -29,24 +33,41 @@ class LDAPProviderSerializer(ProviderSerializer): extra_kwargs = ProviderSerializer.Meta.extra_kwargs +class LDAPProviderFilter(FilterSet): + """LDAP Provider filters""" + + application__isnull = BooleanFilter(method="filter_application__isnull") + + def filter_application__isnull(self, queryset: QuerySet, name, value): + """Only return providers that are neither assigned to application, + both as provider or application provider""" + return queryset.filter( + Q(backchannel_application__isnull=value) | Q(application__isnull=value) + ) + + class Meta: + model = LDAPProvider + fields = { + "application": ["isnull"], + "name": ["iexact"], + "authorization_flow__slug": ["iexact"], + "base_dn": ["iexact"], + "search_group__group_uuid": ["iexact"], + "search_group__name": ["iexact"], + "certificate__kp_uuid": ["iexact"], + "certificate__name": ["iexact"], + "tls_server_name": ["iexact"], + "uid_start_number": ["iexact"], + "gid_start_number": ["iexact"], + } + + class LDAPProviderViewSet(UsedByMixin, ModelViewSet): """LDAPProvider Viewset""" queryset = LDAPProvider.objects.all() serializer_class = LDAPProviderSerializer - filterset_fields = { - "application": ["isnull"], - "name": ["iexact"], - "authorization_flow__slug": ["iexact"], - "base_dn": ["iexact"], - "search_group__group_uuid": ["iexact"], - "search_group__name": ["iexact"], - "certificate__kp_uuid": ["iexact"], - "certificate__name": ["iexact"], - "tls_server_name": ["iexact"], - "uid_start_number": ["iexact"], - "gid_start_number": ["iexact"], - } + filterset_class = LDAPProviderFilter search_fields = ["name"] ordering = ["name"] diff --git a/web/src/admin/outposts/OutpostForm.ts b/web/src/admin/outposts/OutpostForm.ts index 9a845a49f..0b4f70338 100644 --- a/web/src/admin/outposts/OutpostForm.ts +++ b/web/src/admin/outposts/OutpostForm.ts @@ -191,8 +191,12 @@ export class OutpostForm extends ModelForm { const selected = Array.from(this.instance?.providers || []).some((sp) => { return sp == provider.pk; }); + let appName = provider.assignedApplicationName; + if (provider.assignedBackchannelApplicationName) { + appName = provider.assignedBackchannelApplicationName; + } return html``; })}