From 0f76e80341b3ac4390e5ab807ae6ab722701d9dc Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Fri, 2 Apr 2021 17:12:21 +0200 Subject: [PATCH] admin: remove policies views Signed-off-by: Jens Langhammer --- authentik/admin/urls.py | 9 +--- authentik/admin/views/policies.py | 44 ------------------- authentik/lib/widgets.py | 26 ----------- authentik/policies/api/policies.py | 1 - authentik/policies/forms.py | 42 ------------------ authentik/policies/hibp/models.py | 2 - authentik/policies/models.py | 6 +-- authentik/policies/reputation/models.py | 3 -- authentik/policies/tests/test_models.py | 30 ------------- web/src/pages/policies/PolicyListPage.ts | 4 ++ .../expression/ExpressionPolicyForm.ts | 3 +- .../policies/password/PasswordPolicyForm.ts | 2 +- 12 files changed, 10 insertions(+), 162 deletions(-) delete mode 100644 authentik/admin/views/policies.py delete mode 100644 authentik/lib/widgets.py delete mode 100644 authentik/policies/forms.py delete mode 100644 authentik/policies/tests/test_models.py diff --git a/authentik/admin/urls.py b/authentik/admin/urls.py index 1f07cd81d..1871d2b21 100644 --- a/authentik/admin/urls.py +++ b/authentik/admin/urls.py @@ -1,16 +1,9 @@ """authentik URL Configuration""" from django.urls import path -from authentik.admin.views import policies, stages +from authentik.admin.views import stages urlpatterns = [ - # Policies - path("policies/create/", policies.PolicyCreateView.as_view(), name="policy-create"), - path( - "policies//update/", - policies.PolicyUpdateView.as_view(), - name="policy-update", - ), # Stages path("stages/create/", stages.StageCreateView.as_view(), name="stage-create"), path( diff --git a/authentik/admin/views/policies.py b/authentik/admin/views/policies.py deleted file mode 100644 index c08017583..000000000 --- a/authentik/admin/views/policies.py +++ /dev/null @@ -1,44 +0,0 @@ -"""authentik Policy administration""" -from django.contrib.auth.mixins import LoginRequiredMixin -from django.contrib.auth.mixins import ( - PermissionRequiredMixin as DjangoPermissionRequiredMixin, -) -from django.contrib.messages.views import SuccessMessageMixin -from django.urls import reverse_lazy -from django.utils.translation import gettext as _ -from guardian.mixins import PermissionRequiredMixin - -from authentik.admin.views.utils import InheritanceCreateView, InheritanceUpdateView -from authentik.policies.models import Policy - - -class PolicyCreateView( - SuccessMessageMixin, - LoginRequiredMixin, - DjangoPermissionRequiredMixin, - InheritanceCreateView, -): - """Create new Policy""" - - model = Policy - permission_required = "authentik_policies.add_policy" - - template_name = "generic/create.html" - success_url = reverse_lazy("authentik_core:if-admin") - success_message = _("Successfully created Policy") - - -class PolicyUpdateView( - SuccessMessageMixin, - LoginRequiredMixin, - PermissionRequiredMixin, - InheritanceUpdateView, -): - """Update policy""" - - model = Policy - permission_required = "authentik_policies.change_policy" - - template_name = "generic/update.html" - success_url = reverse_lazy("authentik_core:if-admin") - success_message = _("Successfully updated Policy") diff --git a/authentik/lib/widgets.py b/authentik/lib/widgets.py deleted file mode 100644 index f2104313b..000000000 --- a/authentik/lib/widgets.py +++ /dev/null @@ -1,26 +0,0 @@ -"""Utility Widgets""" -from itertools import groupby - -from django.forms.models import ModelChoiceField, ModelChoiceIterator - - -class GroupedModelChoiceIterator(ModelChoiceIterator): - """ModelChoiceField which groups objects by their verbose_name""" - - def __iter__(self): - if self.field.empty_label is not None: - yield ("", self.field.empty_label) - queryset = self.queryset - # Can't use iterator() when queryset uses prefetch_related() - if not queryset._prefetch_related_lookups: - queryset = queryset.iterator() - # We can't use DB-level sorting as we sort by subclass - queryset = sorted(queryset, key=lambda x: x._meta.verbose_name) - for group, objs in groupby(queryset, key=lambda x: x._meta.verbose_name): - yield (group, [self.choice(obj) for obj in objs]) - - -class GroupedModelChoiceField(ModelChoiceField): - """ModelChoiceField which groups objects by their verbose_name""" - - iterator = GroupedModelChoiceIterator diff --git a/authentik/policies/api/policies.py b/authentik/policies/api/policies.py index 3b3f23d2b..86ea476ed 100644 --- a/authentik/policies/api/policies.py +++ b/authentik/policies/api/policies.py @@ -1,6 +1,5 @@ """policy API Views""" from django.core.cache import cache -from django.urls import reverse from drf_yasg.utils import no_body, swagger_auto_schema from guardian.shortcuts import get_objects_for_user from rest_framework import mixins diff --git a/authentik/policies/forms.py b/authentik/policies/forms.py deleted file mode 100644 index 656358fbc..000000000 --- a/authentik/policies/forms.py +++ /dev/null @@ -1,42 +0,0 @@ -"""General fields""" - -from django import forms - -from authentik.core.models import Group -from authentik.lib.widgets import GroupedModelChoiceField -from authentik.policies.models import Policy, PolicyBinding, PolicyBindingModel - - -class PolicyBindingForm(forms.ModelForm): - """Form to edit Policy to PolicyBindingModel Binding""" - - target = GroupedModelChoiceField( - queryset=PolicyBindingModel.objects.all().select_subclasses(), - to_field_name="pbm_uuid", - ) - policy = GroupedModelChoiceField( - queryset=Policy.objects.all().order_by("name").select_subclasses(), - required=False, - ) - group = forms.ModelChoiceField( - queryset=Group.objects.all().order_by("name"), required=False - ) - - def __init__(self, *args, **kwargs): # pragma: no cover - super().__init__(*args, **kwargs) - if "target" in self.initial: - self.fields["target"].widget = forms.HiddenInput() - - class Meta: - - model = PolicyBinding - fields = ["enabled", "policy", "group", "user", "target", "order", "timeout"] - - -class PolicyForm(forms.ModelForm): - """Base Policy form""" - - class Meta: - - model = Policy - fields = ["name", "execution_logging"] diff --git a/authentik/policies/hibp/models.py b/authentik/policies/hibp/models.py index c0511e945..4b2032a1d 100644 --- a/authentik/policies/hibp/models.py +++ b/authentik/policies/hibp/models.py @@ -1,9 +1,7 @@ """authentik HIBP Models""" from hashlib import sha1 -from typing import Type from django.db import models -from django.forms import ModelForm from django.utils.translation import gettext as _ from requests import get from rest_framework.serializers import BaseSerializer diff --git a/authentik/policies/models.py b/authentik/policies/models.py index 19045ae78..e7f0a71b6 100644 --- a/authentik/policies/models.py +++ b/authentik/policies/models.py @@ -1,9 +1,7 @@ """Policy base models""" -from typing import Type from uuid import uuid4 from django.db import models -from django.forms import ModelForm from django.utils.translation import gettext_lazy as _ from model_utils.managers import InheritanceManager from rest_framework.serializers import BaseSerializer @@ -147,8 +145,8 @@ class Policy(SerializerModel, CreatedUpdatedModel): objects = InheritanceAutoManager() @property - def form(self) -> Type[ModelForm]: - """Return Form class used to edit this object""" + def component(self) -> str: + """Return component used to edit this object""" raise NotImplementedError def __str__(self): diff --git a/authentik/policies/reputation/models.py b/authentik/policies/reputation/models.py index 8d0004f3e..4f8bfdc6b 100644 --- a/authentik/policies/reputation/models.py +++ b/authentik/policies/reputation/models.py @@ -1,9 +1,6 @@ """authentik reputation request policy""" -from typing import Type - from django.core.cache import cache from django.db import models -from django.forms import ModelForm from django.utils.translation import gettext as _ from rest_framework.serializers import BaseSerializer diff --git a/authentik/policies/tests/test_models.py b/authentik/policies/tests/test_models.py deleted file mode 100644 index 3e13b8528..000000000 --- a/authentik/policies/tests/test_models.py +++ /dev/null @@ -1,30 +0,0 @@ -"""flow model tests""" -from typing import Callable, Type - -from django.forms import ModelForm -from django.test import TestCase - -from authentik.lib.utils.reflection import all_subclasses -from authentik.policies.models import Policy - - -class TestPolicyProperties(TestCase): - """Generic model properties tests""" - - -def policy_tester_factory(model: Type[Policy]) -> Callable: - """Test a form""" - - def tester(self: TestPolicyProperties): - model_inst = model() - self.assertTrue(issubclass(model_inst.form, ModelForm)) - - return tester - - -for policy_type in all_subclasses(Policy): - setattr( - TestPolicyProperties, - f"test_policy_{policy_type.__name__}", - policy_tester_factory(policy_type), - ) diff --git a/web/src/pages/policies/PolicyListPage.ts b/web/src/pages/policies/PolicyListPage.ts index a0698be7d..2e2b2e153 100644 --- a/web/src/pages/policies/PolicyListPage.ts +++ b/web/src/pages/policies/PolicyListPage.ts @@ -20,6 +20,8 @@ import "./event_matcher/EventMatcherPolicyForm"; import "./expression/ExpressionPolicyForm"; import "./expiry/ExpiryPolicyForm"; import "./hibp/HaveIBeenPwnedPolicyForm"; +import "./password/PasswordPolicyForm"; +import "./reputation/ReputationPolicyForm"; @customElement("ak-policy-list") export class PolicyListPage extends TablePage { @@ -89,6 +91,8 @@ export class PolicyListPage extends TablePage { "expression": "ak-policy-expression-form", "passwordexpiry": "ak-policy-password-expiry-form", "haveibeenpwend": "ak-policy-hibp-form", + "password": "ak-policy-password-form", + "reputation": "ak-policy-reputation-form", }}>