admin: remove user enable/disable views

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-03-18 13:09:00 +01:00
parent db9aa5d9dc
commit 93bd95436f
7 changed files with 101 additions and 96 deletions

View File

@ -172,10 +172,6 @@ urlpatterns = [
# Users
path("users/create/", users.UserCreateView.as_view(), name="user-create"),
path("users/<int:pk>/update/", users.UserUpdateView.as_view(), name="user-update"),
path(
"users/<int:pk>/disable/", users.UserDisableView.as_view(), name="user-disable"
),
path("users/<int:pk>/enable/", users.UserEnableView.as_view(), name="user-enable"),
path(
"users/<int:pk>/reset/",
users.UserPasswordResetView.as_view(),

View File

@ -6,7 +6,6 @@ from django.contrib.auth.mixins import (
)
from django.contrib.messages.views import SuccessMessageMixin
from django.http import HttpRequest, HttpResponse
from django.http.response import HttpResponseRedirect
from django.shortcuts import redirect
from django.urls import reverse_lazy
from django.utils.http import urlencode
@ -15,7 +14,6 @@ from django.views.generic import DetailView, UpdateView
from guardian.mixins import PermissionRequiredMixin
from authentik.admin.forms.users import UserForm
from authentik.admin.views.utils import DeleteMessageView
from authentik.core.models import Token, User
from authentik.lib.views import CreateAssignPermView
@ -56,48 +54,6 @@ class UserUpdateView(
success_message = _("Successfully updated User")
class UserDisableView(LoginRequiredMixin, PermissionRequiredMixin, DeleteMessageView):
"""Disable user"""
object: User
model = User
permission_required = "authentik_core.update_user"
# By default the object's name is user which is used by other checks
context_object_name = "object"
template_name = "administration/user/disable.html"
success_url = reverse_lazy("authentik_core:shell")
success_message = _("Successfully disabled User")
def delete(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
self.object: User = self.get_object()
success_url = self.get_success_url()
self.object.is_active = False
self.object.save()
return HttpResponseRedirect(success_url)
class UserEnableView(LoginRequiredMixin, PermissionRequiredMixin, DetailView):
"""Enable user"""
object: User
model = User
permission_required = "authentik_core.update_user"
# By default the object's name is user which is used by other checks
context_object_name = "object"
success_url = reverse_lazy("authentik_core:shell")
success_message = _("Successfully enabled User")
def get(self, request: HttpRequest, *args, **kwargs):
self.object: User = self.get_object()
self.object.is_active = True
self.object.save()
return HttpResponseRedirect(self.success_url)
class UserPasswordResetView(LoginRequiredMixin, PermissionRequiredMixin, DetailView):
"""Get Password reset link for user"""

View File

@ -17,11 +17,6 @@ urlpatterns = [
user.TokenUpdateView.as_view(),
name="user-tokens-update",
),
path(
"-/user/tokens/<slug:identifier>/delete/",
user.TokenDeleteView.as_view(),
name="user-tokens-delete",
),
# Impersonation
path(
"-/impersonation/<int:user_id>/",

View File

@ -14,7 +14,6 @@ from django.views.generic.base import TemplateView
from guardian.mixins import PermissionRequiredMixin
from guardian.shortcuts import get_objects_for_user
from authentik.admin.views.utils import DeleteMessageView
from authentik.core.forms.token import UserTokenForm
from authentik.core.forms.users import UserDetailForm
from authentik.core.models import Token, TokenIntents
@ -92,23 +91,3 @@ class TokenUpdateView(
.filter(intent=TokenIntents.INTENT_API, identifier=identifier)
.first()
)
class TokenDeleteView(LoginRequiredMixin, PermissionRequiredMixin, DeleteMessageView):
"""Delete token"""
model = Token
permission_required = "authentik_core.delete_token"
template_name = "generic/delete.html"
success_url = "/"
success_message = _("Successfully deleted Token")
def get_object(self) -> Token:
identifier = self.kwargs.get("identifier")
return (
get_objects_for_user(
self.request.user, self.permission_required, self.model
)
.filter(intent=TokenIntents.INTENT_API, identifier=identifier)
.first()
)

View File

@ -17,10 +17,7 @@ export class DeleteForm extends ModalButton {
confirm(): void {
this.delete().then(() => {
showMessage({
message: gettext(`Successfully deleted ${this.objectLabel} ${this.obj?.name}`),
level_tag: "success",
});
this.onSuccess();
this.open = false;
this.dispatchEvent(
new CustomEvent("ak-refresh", {
@ -29,10 +26,21 @@ export class DeleteForm extends ModalButton {
})
);
}).catch((e) => {
showMessage({
message: gettext(`Failed to delete ${this.objectLabel}: ${e.toString()}`),
level_tag: "error",
});
this.onError(e);
});
}
onSuccess(): void {
showMessage({
message: gettext(`Successfully deleted ${this.objectLabel} ${ this.obj?.name }`),
level_tag: "success",
});
}
onError(e: Error): void {
showMessage({
message: gettext(`Failed to delete ${this.objectLabel}: ${e.toString()}`),
level_tag: "error",
});
}

View File

@ -0,0 +1,66 @@
import { gettext } from "django";
import { customElement, html, TemplateResult } from "lit-element";
import { DeleteForm } from "../../elements/forms/DeleteForm";
import { showMessage } from "../../elements/messages/MessageContainer";
@customElement("ak-user-active-form")
export class UserActiveForm extends DeleteForm {
onSuccess(): void {
showMessage({
message: gettext(`Successfully updated ${this.objectLabel} ${this.obj?.name}`),
level_tag: "success",
});
}
onError(e: Error): void {
showMessage({
message: gettext(`Failed to update ${this.objectLabel}: ${e.toString()}`),
level_tag: "error",
});
}
renderModalInner(): TemplateResult {
return html`<section class="pf-c-page__main-section pf-m-light">
<div class="pf-c-content">
<h1 class="pf-c-title pf-m-2xl">
${gettext(`Update ${this.objectLabel}`)}
</h1>
</div>
</section>
<section class="pf-c-page__main-section">
<div class="pf-l-stack">
<div class="pf-l-stack__item">
<div class="pf-c-card">
<div class="pf-c-card__body">
<form class="pf-c-form pf-m-horizontal">
<p>
${gettext(
`Are you sure you want to update ${this.objectLabel} '${this.obj?.name}'?`
)}
</p>
</form>
</div>
</div>
</div>
</div>
</section>
<footer class="pf-c-modal-box__footer">
<ak-spinner-button
.callAction=${() => {
this.confirm();
}}
class="pf-m-warning">
${gettext("Update")}
</ak-spinner-button>&nbsp;
<ak-spinner-button
.callAction=${() => {
this.open = false;
}}
class="pf-m-secondary">
${gettext("Cancel")}
</ak-spinner-button>
</footer>`;
}
}

View File

@ -12,6 +12,7 @@ import { CoreApi, User } from "authentik-api";
import { DEFAULT_CONFIG } from "../../api/Config";
import { AdminURLManager } from "../../api/legacy";
import "../../elements/forms/DeleteForm";
import "./UserActiveForm";
@customElement("ak-user-list")
export class UserListPage extends TablePage<User> {
@ -71,19 +72,23 @@ export class UserListPage extends TablePage<User> {
</button>
<ul class="pf-c-dropdown__menu" hidden>
<li>
${item.isActive ?
html`<ak-modal-button href="${AdminURLManager.users(`${item.pk}/disable/`)}">
<button slot="trigger" class="pf-c-dropdown__menu-item">
${gettext("Disable")}
</button>
<div slot="modal"></div>
</ak-modal-button>`:
html`<ak-modal-button href="${AdminURLManager.users(`${item.pk}/enable/`)}">
<button slot="trigger" class="pf-c-dropdown__menu-item">
${gettext("Enable")}
</button>
<div slot="modal"></div>
</ak-modal-button>`}
<ak-user-active-form
.obj=${item}
objectLabel=${gettext("User")}
.delete=${() => {
return new CoreApi(DEFAULT_CONFIG).coreUsersPartialUpdate({
id: item.pk || 0,
data: {
username: item.username,
name: item.name,
isActive: !item.isActive,
}
});
}}>
<button slot="trigger" class="pf-c-dropdown__menu-item">
${item.isActive ? gettext("Disable") : gettext("Enable")}
</button>
</ak-user-active-form>
</li>
<li class="pf-c-divider" role="separator"></li>
<li>
@ -95,7 +100,7 @@ export class UserListPage extends TablePage<User> {
id: item.pk || 0
});
}}>
<button slot="trigger" class="pf-c-button pf-m-danger">
<button slot="trigger" class="pf-c-dropdown__menu-item">
${gettext("Delete")}
</button>
</ak-forms-delete>