From cb0b5f7146a9b6041f0063389c5f6d4a4ae6c08b Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Tue, 30 Mar 2021 22:12:06 +0200 Subject: [PATCH] web/admin: migrate prompts to web Signed-off-by: Jens Langhammer --- authentik/admin/urls.py | 12 --- authentik/admin/views/stages_prompts.py | 48 ---------- authentik/stages/prompt/forms.py | 22 +---- web/src/api/legacy.ts | 4 - web/src/pages/stages/PromptForm.ts | 122 ++++++++++++++++++++++++ web/src/pages/stages/PromptListPage.ts | 37 ++++--- 6 files changed, 149 insertions(+), 96 deletions(-) delete mode 100644 authentik/admin/views/stages_prompts.py create mode 100644 web/src/pages/stages/PromptForm.ts diff --git a/authentik/admin/urls.py b/authentik/admin/urls.py index 8df23a894..aabbbe09b 100644 --- a/authentik/admin/urls.py +++ b/authentik/admin/urls.py @@ -10,7 +10,6 @@ from authentik.admin.views import ( sources, stages, stages_bindings, - stages_prompts, ) from authentik.providers.saml.views.metadata import MetadataImportView @@ -74,17 +73,6 @@ urlpatterns = [ stages_bindings.StageBindingUpdateView.as_view(), name="stage-binding-update", ), - # Stage Prompts - path( - "stages_prompts/create/", - stages_prompts.PromptCreateView.as_view(), - name="stage-prompt-create", - ), - path( - "stages_prompts//update/", - stages_prompts.PromptUpdateView.as_view(), - name="stage-prompt-update", - ), # Property Mappings path( "property-mappings/create/", diff --git a/authentik/admin/views/stages_prompts.py b/authentik/admin/views/stages_prompts.py deleted file mode 100644 index f47e6dba1..000000000 --- a/authentik/admin/views/stages_prompts.py +++ /dev/null @@ -1,48 +0,0 @@ -"""authentik Prompt 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 django.views.generic import UpdateView -from guardian.mixins import PermissionRequiredMixin - -from authentik.lib.views import CreateAssignPermView -from authentik.stages.prompt.forms import PromptAdminForm -from authentik.stages.prompt.models import Prompt - - -class PromptCreateView( - SuccessMessageMixin, - LoginRequiredMixin, - DjangoPermissionRequiredMixin, - CreateAssignPermView, -): - """Create new Prompt""" - - model = Prompt - form_class = PromptAdminForm - permission_required = "authentik_stages_prompt.add_prompt" - - template_name = "generic/create.html" - success_url = reverse_lazy("authentik_core:if-admin") - success_message = _("Successfully created Prompt") - - -class PromptUpdateView( - SuccessMessageMixin, - LoginRequiredMixin, - PermissionRequiredMixin, - UpdateView, -): - """Update prompt""" - - model = Prompt - form_class = PromptAdminForm - permission_required = "authentik_stages_prompt.change_prompt" - - template_name = "generic/update.html" - success_url = reverse_lazy("authentik_core:if-admin") - success_message = _("Successfully updated Prompt") diff --git a/authentik/stages/prompt/forms.py b/authentik/stages/prompt/forms.py index abdcf0ed8..9f3b22bde 100644 --- a/authentik/stages/prompt/forms.py +++ b/authentik/stages/prompt/forms.py @@ -1,7 +1,7 @@ """Prompt forms""" from django import forms -from authentik.stages.prompt.models import Prompt, PromptStage +from authentik.stages.prompt.models import PromptStage class PromptStageForm(forms.ModelForm): @@ -14,23 +14,3 @@ class PromptStageForm(forms.ModelForm): widgets = { "name": forms.TextInput(), } - - -class PromptAdminForm(forms.ModelForm): - """Form to edit Prompt instances for admins""" - - class Meta: - - model = Prompt - fields = [ - "field_key", - "label", - "type", - "required", - "placeholder", - "order", - ] - widgets = { - "label": forms.TextInput(), - "placeholder": forms.TextInput(), - } diff --git a/web/src/api/legacy.ts b/web/src/api/legacy.ts index 204c4d9d7..abce12739 100644 --- a/web/src/api/legacy.ts +++ b/web/src/api/legacy.ts @@ -24,10 +24,6 @@ export class AdminURLManager { return `/administration/stages/${rest}`; } - static stagePrompts(rest: string): string { - return `/administration/stages_prompts/${rest}`; - } - static stageBindings(rest: string): string { return `/administration/stages/bindings/${rest}`; } diff --git a/web/src/pages/stages/PromptForm.ts b/web/src/pages/stages/PromptForm.ts new file mode 100644 index 000000000..6caa3b9cf --- /dev/null +++ b/web/src/pages/stages/PromptForm.ts @@ -0,0 +1,122 @@ +import { Prompt, PromptTypeEnum, StagesApi } 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"; + +@customElement("ak-stage-prompt-form") +export class PromptForm extends Form { + + @property({attribute: false}) + prompt?: Prompt; + + getSuccessMessage(): string { + if (this.prompt) { + return gettext("Successfully updated prompt."); + } else { + return gettext("Successfully created prompt."); + } + } + + send = (data: Prompt): Promise => { + if (this.prompt) { + return new StagesApi(DEFAULT_CONFIG).stagesPromptPromptsUpdate({ + promptUuid: this.prompt.pk || "", + data: data + }); + } else { + return new StagesApi(DEFAULT_CONFIG).stagesPromptPromptsCreate({ + data: data + }); + } + }; + + renderTypes(): TemplateResult { + return html` + + + + + + + + + + + + `; + } + + renderForm(): TemplateResult { + return html`
+ + +

${gettext("Name of the form field, also used to store the value.")}

+
+ + +

${gettext("Label shown next to/above the prompt.")}

+
+ + + + +
+ + +
+
+ + +

${gettext("Optionally pre-fill the input value")}

+
+ + + +
`; + } + +} diff --git a/web/src/pages/stages/PromptListPage.ts b/web/src/pages/stages/PromptListPage.ts index 106f491fc..525719fd3 100644 --- a/web/src/pages/stages/PromptListPage.ts +++ b/web/src/pages/stages/PromptListPage.ts @@ -6,11 +6,12 @@ import { TablePage } from "../../elements/table/TablePage"; import "../../elements/buttons/ModalButton"; import "../../elements/buttons/SpinnerButton"; import "../../elements/forms/DeleteForm"; +import "../../elements/forms/ModalForm"; +import "./PromptForm"; import { TableColumn } from "../../elements/table/Table"; import { PAGE_SIZE } from "../../constants"; import { Prompt, StagesApi } from "authentik-api"; import { DEFAULT_CONFIG } from "../../api/Config"; -import { AdminURLManager } from "../../api/legacy"; @customElement("ak-stage-prompt-list") export class PromptListPage extends TablePage { @@ -60,12 +61,19 @@ export class PromptListPage extends TablePage { return html`
  • ${stage.name}
  • `; })}`, html` - - + + + ${gettext("Update")} + + + ${gettext("Update Prompt")} + + + + + { renderToolbar(): TemplateResult { return html` - - + + ${gettext("Create")} - -
    -
    + + + ${gettext("Create Prompt")} + + + + + ${super.renderToolbar()} `; }