stages/user_delete: migrate to web

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-04-03 00:02:24 +02:00
parent 8ac82b97d3
commit f7aabe8ca9
9 changed files with 60 additions and 55 deletions

View file

@ -2,7 +2,6 @@
from typing import Type
from django.db import models
from django.forms import ModelForm
from django.utils.translation import gettext_lazy as _
from django.views import View
from rest_framework.serializers import BaseSerializer

View file

@ -1,20 +0,0 @@
"""authentik flows delete forms"""
from django import forms
from authentik.stages.user_delete.models import UserDeleteStage
class UserDeleteStageForm(forms.ModelForm):
"""Form to delete/edit UserDeleteStage instances"""
class Meta:
model = UserDeleteStage
fields = ["name"]
widgets = {
"name": forms.TextInput(),
}
class UserDeleteForm(forms.Form):
"""Confirmation form to ensure user knows they are deleting their profile"""

View file

@ -1,7 +1,6 @@
"""delete stage models"""
from typing import Type
from django.forms import ModelForm
from django.utils.translation import gettext_lazy as _
from django.views import View
from rest_framework.serializers import BaseSerializer
@ -26,10 +25,8 @@ class UserDeleteStage(Stage):
return UserDeleteStageView
@property
def form(self) -> Type[ModelForm]:
from authentik.stages.user_delete.forms import UserDeleteStageForm
return UserDeleteStageForm
def component(self) -> str:
return "ak-stage-user-delete-form"
class Meta:

View file

@ -2,31 +2,25 @@
from django.contrib import messages
from django.http import HttpRequest, HttpResponse
from django.utils.translation import gettext as _
from django.views.generic import FormView
from structlog.stdlib import get_logger
from authentik.core.models import User
from authentik.flows.planner import PLAN_CONTEXT_PENDING_USER
from authentik.flows.stage import StageView
from authentik.stages.user_delete.forms import UserDeleteForm
LOGGER = get_logger()
class UserDeleteStageView(FormView, StageView):
class UserDeleteStageView(StageView):
"""Finalise unenrollment flow by deleting the user object."""
form_class = UserDeleteForm
def get(self, request: HttpRequest) -> HttpResponse:
"""Delete currently pending user"""
if PLAN_CONTEXT_PENDING_USER not in self.executor.plan.context:
message = _("No Pending User.")
messages.error(request, message)
LOGGER.debug(message)
return self.executor.stage_invalid()
return super().get(request)
def form_valid(self, form: UserDeleteForm) -> HttpResponse:
user: User = self.executor.plan.context[PLAN_CONTEXT_PENDING_USER]
user.delete()
LOGGER.debug("Deleted user", user=user)

View file

@ -13,7 +13,6 @@ from authentik.flows.planner import PLAN_CONTEXT_PENDING_USER, FlowPlan
from authentik.flows.tests.test_views import TO_STAGE_RESPONSE_MOCK
from authentik.flows.views import SESSION_KEY_PLAN
from authentik.stages.password.stage import PLAN_CONTEXT_AUTHENTICATION_BACKEND
from authentik.stages.user_login.forms import UserLoginStageForm
from authentik.stages.user_login.models import UserLoginStage
@ -112,10 +111,3 @@ class TestUserLoginStage(TestCase):
"type": ChallengeTypes.NATIVE.value,
},
)
def test_form(self):
"""Test Form"""
data = {"name": "test", "session_duration": "seconds=0"}
self.assertEqual(UserLoginStageForm(data).is_valid(), True)
data = {"name": "test", "session_duration": "123"}
self.assertEqual(UserLoginStageForm(data).is_valid(), False)

View file

@ -9,7 +9,6 @@ from authentik.flows.models import Flow, FlowDesignation, FlowStageBinding
from authentik.flows.planner import PLAN_CONTEXT_PENDING_USER, FlowPlan
from authentik.flows.views import SESSION_KEY_PLAN
from authentik.stages.password.stage import PLAN_CONTEXT_AUTHENTICATION_BACKEND
from authentik.stages.user_logout.forms import UserLogoutStageForm
from authentik.stages.user_logout.models import UserLogoutStage
@ -51,8 +50,3 @@ class TestUserLogoutStage(TestCase):
force_str(response.content),
{"to": reverse("authentik_core:root-redirect"), "type": "redirect"},
)
def test_form(self):
"""Test Form"""
data = {"name": "test"}
self.assertEqual(UserLogoutStageForm(data).is_valid(), True)

View file

@ -1,7 +1,6 @@
"""write stage models"""
from typing import Type
from django.forms import ModelForm
from django.utils.translation import gettext_lazy as _
from django.views import View
from rest_framework.serializers import BaseSerializer

View file

@ -15,7 +15,6 @@ from authentik.flows.planner import PLAN_CONTEXT_PENDING_USER, FlowPlan
from authentik.flows.tests.test_views import TO_STAGE_RESPONSE_MOCK
from authentik.flows.views import SESSION_KEY_PLAN
from authentik.stages.prompt.stage import PLAN_CONTEXT_PROMPT
from authentik.stages.user_write.forms import UserWriteStageForm
from authentik.stages.user_write.models import UserWriteStage
@ -135,8 +134,3 @@ class TestUserWriteStage(TestCase):
"type": ChallengeTypes.NATIVE.value,
},
)
def test_form(self):
"""Test Form"""
data = {"name": "test"}
self.assertEqual(UserWriteStageForm(data).is_valid(), True)

View file

@ -0,0 +1,56 @@
import { UserDeleteStage, 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-user-delete-form")
export class UserLogoutStageForm extends Form<UserDeleteStage> {
set stageUUID(value: string) {
new StagesApi(DEFAULT_CONFIG).stagesUserDeleteRead({
stageUuid: value,
}).then(stage => {
this.stage = stage;
});
}
@property({attribute: false})
stage?: UserDeleteStage;
getSuccessMessage(): string {
if (this.stage) {
return gettext("Successfully updated stage.");
} else {
return gettext("Successfully created stage.");
}
}
send = (data: UserDeleteStage): Promise<UserDeleteStage> => {
if (this.stage) {
return new StagesApi(DEFAULT_CONFIG).stagesUserDeleteUpdate({
stageUuid: this.stage.pk || "",
data: data
});
} else {
return new StagesApi(DEFAULT_CONFIG).stagesUserDeleteCreate({
data: data
});
}
};
renderForm(): TemplateResult {
return html`<form class="pf-c-form pf-m-horizontal">
<ak-form-element-horizontal
label=${gettext("Name")}
?required=${true}
name="name">
<input type="text" value="${ifDefined(this.stage?.name || "")}" class="pf-c-form-control" required>
</ak-form-element-horizontal>
</form>`;
}
}