From 415bb4cc881a9ed359bcfcceabb7a984f4764795 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Fri, 2 Apr 2021 16:32:03 +0200 Subject: [PATCH] policies/event_matcher: migrate to web Signed-off-by: Jens Langhammer --- authentik/policies/dummy/tests.py | 16 --- authentik/policies/event_matcher/forms.py | 25 ----- authentik/policies/event_matcher/models.py | 9 +- .../event_matcher/EventMatcherPolicyForm.ts | 106 ++++++++++++++++++ 4 files changed, 108 insertions(+), 48 deletions(-) delete mode 100644 authentik/policies/event_matcher/forms.py create mode 100644 web/src/pages/policies/event_matcher/EventMatcherPolicyForm.ts diff --git a/authentik/policies/dummy/tests.py b/authentik/policies/dummy/tests.py index 8d0cefd5c..c433d0d39 100644 --- a/authentik/policies/dummy/tests.py +++ b/authentik/policies/dummy/tests.py @@ -2,7 +2,6 @@ from django.test import TestCase from guardian.shortcuts import get_anonymous_user -from authentik.policies.dummy.forms import DummyPolicyForm from authentik.policies.dummy.models import DummyPolicy from authentik.policies.engine import PolicyRequest @@ -22,18 +21,3 @@ class TestDummyPolicy(TestCase): result = policy.passes(self.request) self.assertFalse(result.passing) self.assertEqual(result.messages, ("dummy",)) - - def test_form(self): - """test form""" - form = DummyPolicyForm( - data={ - "name": "dummy", - "negate": False, - "order": 0, - "timeout": 1, - "result": True, - "wait_min": 1, - "wait_max": 2, - } - ) - self.assertTrue(form.is_valid()) diff --git a/authentik/policies/event_matcher/forms.py b/authentik/policies/event_matcher/forms.py deleted file mode 100644 index e9707f5db..000000000 --- a/authentik/policies/event_matcher/forms.py +++ /dev/null @@ -1,25 +0,0 @@ -"""authentik Event Matcher Policy forms""" - -from django import forms -from django.utils.translation import gettext_lazy as _ - -from authentik.policies.event_matcher.models import EventMatcherPolicy -from authentik.policies.forms import PolicyForm - - -class EventMatcherPolicyForm(PolicyForm): - """EventMatcherPolicy Form""" - - class Meta: - - model = EventMatcherPolicy - fields = PolicyForm.Meta.fields + [ - "action", - "client_ip", - "app", - ] - widgets = { - "name": forms.TextInput(), - "client_ip": forms.TextInput(), - } - labels = {"client_ip": _("Client IP")} diff --git a/authentik/policies/event_matcher/models.py b/authentik/policies/event_matcher/models.py index 76930c14f..e1aeefb73 100644 --- a/authentik/policies/event_matcher/models.py +++ b/authentik/policies/event_matcher/models.py @@ -1,9 +1,6 @@ """Event Matcher models""" -from typing import Type - from django.apps import apps from django.db import models -from django.forms import ModelForm from django.utils.translation import gettext as _ from rest_framework.serializers import BaseSerializer @@ -63,10 +60,8 @@ class EventMatcherPolicy(Policy): return EventMatcherPolicySerializer @property - def form(self) -> Type[ModelForm]: - from authentik.policies.event_matcher.forms import EventMatcherPolicyForm - - return EventMatcherPolicyForm + def component(self) -> str: + return "ak-policy-event-matcher-form" def passes(self, request: PolicyRequest) -> PolicyResult: if "event" not in request.context: diff --git a/web/src/pages/policies/event_matcher/EventMatcherPolicyForm.ts b/web/src/pages/policies/event_matcher/EventMatcherPolicyForm.ts new file mode 100644 index 000000000..f7565ee64 --- /dev/null +++ b/web/src/pages/policies/event_matcher/EventMatcherPolicyForm.ts @@ -0,0 +1,106 @@ +import { AdminApi, EventMatcherPolicy, EventsApi, PoliciesApi } from "authentik-api"; +import { gettext } from "django"; +import { customElement, property } from "lit-element"; +import { html, TemplateResult } from "lit-html"; +import { DEFAULT_CONFIG } from "../../../api/Config"; +import { Form } from "../../../elements/forms/Form"; +import { ifDefined } from "lit-html/directives/if-defined"; +import "../../../elements/forms/HorizontalFormElement"; +import "../../../elements/forms/FormGroup"; +import { until } from "lit-html/directives/until"; + +@customElement("ak-policy-event-matcher-form") +export class EventMatcherPolicyForm extends Form { + + set policyUUID(value: string) { + new PoliciesApi(DEFAULT_CONFIG).policiesEventMatcherRead({ + policyUuid: value, + }).then(policy => { + this.policy = policy; + }); + } + + @property({attribute: false}) + policy?: EventMatcherPolicy; + + getSuccessMessage(): string { + if (this.policy) { + return gettext("Successfully updated policy."); + } else { + return gettext("Successfully created policy."); + } + } + + send = (data: EventMatcherPolicy): Promise => { + if (this.policy) { + return new PoliciesApi(DEFAULT_CONFIG).policiesEventMatcherUpdate({ + policyUuid: this.policy.pk || "", + data: data + }); + } else { + return new PoliciesApi(DEFAULT_CONFIG).policiesEventMatcherCreate({ + data: data + }); + } + }; + + renderForm(): TemplateResult { + return html`
+ + + + +
+ + +
+

${gettext("When this option is enabled, all executions of this policy will be logged. By default, only execution errors are logged.")}

+
+ + + ${gettext("Policy-specific settings")} + +
+ + +

${gettext("Match created events with this action type. When left empty, all action types will be matched.")}

+
+ + +

${gettext("Matches Event's Client IP (strict matching, for network matching use an Expression Policy.")}

+
+ + +

${gettext("Match events created by selected application. When left empty, all applications are matched.")}

+
+
+
+
`; + } + +}