From 264c678eaa263f975ef9066da320150c7a88e36b Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Sat, 20 Feb 2021 00:20:01 +0100 Subject: [PATCH] web: migrate user token list to web --- authentik/core/templates/user/settings.html | 4 +- authentik/core/urls.py | 1 - authentik/core/views/user.py | 35 +---------- web/src/api/Tokens.ts | 4 ++ web/src/main.ts | 1 + web/src/pages/tokens/TokenListPage.ts | 2 +- web/src/pages/tokens/UserTokenList.ts | 65 +++++++++++++++++++++ 7 files changed, 75 insertions(+), 37 deletions(-) create mode 100644 web/src/pages/tokens/UserTokenList.ts diff --git a/authentik/core/templates/user/settings.html b/authentik/core/templates/user/settings.html index 047288c59..6305bc773 100644 --- a/authentik/core/templates/user/settings.html +++ b/authentik/core/templates/user/settings.html @@ -24,9 +24,7 @@
- -
-
+
{% user_stages as user_stages_loc %} {% for stage, stage_link in user_stages_loc.items %} diff --git a/authentik/core/urls.py b/authentik/core/urls.py index 9383191ab..f24a27e69 100644 --- a/authentik/core/urls.py +++ b/authentik/core/urls.py @@ -8,7 +8,6 @@ urlpatterns = [ # User views path("-/user/", user.UserSettingsView.as_view(), name="user-settings"), path("-/user/details/", user.UserDetailsView.as_view(), name="user-details"), - path("-/user/tokens/", user.TokenListView.as_view(), name="user-tokens"), path( "-/user/tokens/create/", user.TokenCreateView.as_view(), diff --git a/authentik/core/views/user.py b/authentik/core/views/user.py index 140c6e6da..13939ca4f 100644 --- a/authentik/core/views/user.py +++ b/authentik/core/views/user.py @@ -6,20 +6,15 @@ from django.contrib.auth.mixins import ( PermissionRequiredMixin as DjangoPermissionRequiredMixin, ) from django.contrib.messages.views import SuccessMessageMixin -from django.db.models.query import QuerySet from django.http.response import HttpResponse from django.urls import reverse_lazy from django.utils.translation import gettext as _ -from django.views.generic import ListView, UpdateView +from django.views.generic import UpdateView from django.views.generic.base import TemplateView -from guardian.mixins import PermissionListMixin, PermissionRequiredMixin +from guardian.mixins import PermissionRequiredMixin from guardian.shortcuts import get_objects_for_user -from authentik.admin.views.utils import ( - DeleteMessageView, - SearchListMixin, - UserPaginateListMixin, -) +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 @@ -54,30 +49,6 @@ class UserDetailsView(SuccessMessageMixin, LoginRequiredMixin, UpdateView): return kwargs -class TokenListView( - LoginRequiredMixin, - PermissionListMixin, - UserPaginateListMixin, - SearchListMixin, - ListView, -): - """Show list of all tokens""" - - model = Token - ordering = "expires" - permission_required = "authentik_core.view_token" - - template_name = "user/token_list.html" - search_fields = [ - "identifier", - "intent", - "description", - ] - - def get_queryset(self) -> QuerySet: - return super().get_queryset().filter(intent=TokenIntents.INTENT_API) - - class TokenCreateView( SuccessMessageMixin, LoginRequiredMixin, diff --git a/web/src/api/Tokens.ts b/web/src/api/Tokens.ts index e1c6ab1b0..9319efa7d 100644 --- a/web/src/api/Tokens.ts +++ b/web/src/api/Tokens.ts @@ -34,6 +34,10 @@ export class Token { return `/administration/tokens/${rest}`; } + static userUrl(rest: string): string { + return `/-/user/tokens/${rest}`; + } + static getKey(identifier: string): Promise { return DefaultClient.fetch<{ key: string }>(["core", "tokens", identifier, "view_key"]).then( (r) => r.key diff --git a/web/src/main.ts b/web/src/main.ts index 9922f7ff0..88b4eba36 100644 --- a/web/src/main.ts +++ b/web/src/main.ts @@ -29,6 +29,7 @@ import "./pages/admin-overview/AdminOverviewPage"; import "./pages/admin-overview/TopApplicationsTable"; import "./pages/applications/ApplicationListPage"; import "./pages/applications/ApplicationViewPage"; +import "./pages/tokens/UserTokenList"; import "./pages/LibraryPage"; import "./elements/stages/authenticator_webauthn/WebAuthnRegister"; diff --git a/web/src/pages/tokens/TokenListPage.ts b/web/src/pages/tokens/TokenListPage.ts index 488971c17..f96016d11 100644 --- a/web/src/pages/tokens/TokenListPage.ts +++ b/web/src/pages/tokens/TokenListPage.ts @@ -52,7 +52,7 @@ export class TokenListPage extends TablePage { html`${item.expiring ? "Yes" : "No"}`, html`${item.expiring ? new Date(item.expires * 1000).toLocaleString() : "-"}`, html` - + ${gettext("Delete")} diff --git a/web/src/pages/tokens/UserTokenList.ts b/web/src/pages/tokens/UserTokenList.ts new file mode 100644 index 000000000..3ad10a789 --- /dev/null +++ b/web/src/pages/tokens/UserTokenList.ts @@ -0,0 +1,65 @@ +import { gettext } from "django"; +import { customElement, html, property, TemplateResult } from "lit-element"; +import { AKResponse } from "../../api/Client"; +import { TablePage } from "../../elements/table/TablePage"; + +import "../../elements/buttons/ModalButton"; +import "../../elements/buttons/Dropdown"; +import "../../elements/buttons/TokenCopyButton"; +import { Table, TableColumn } from "../../elements/table/Table"; +import { Token } from "../../api/Tokens"; + +@customElement("ak-token-user-list") +export class UserTokenList extends Table { + searchEnabled(): boolean { + return true; + } + + @property() + order = "expires"; + + apiEndpoint(page: number): Promise> { + return Token.list({ + ordering: this.order, + page: page, + search: this.search || "", + }); + } + + columns(): TableColumn[] { + return [ + new TableColumn("Identifier", "identifier"), + new TableColumn("User", "user"), + new TableColumn("Expires?", "expiring"), + new TableColumn("Expiry date", "expires"), + new TableColumn(""), + ]; + } + + row(item: Token): TemplateResult[] { + return [ + html`${item.identifier}`, + html`${item.user.username}`, + html`${item.expiring ? "Yes" : "No"}`, + html`${item.expiring ? new Date(item.expires * 1000).toLocaleString() : "-"}`, + html` + + + ${gettext("Edit")} + +
+
+ + + ${gettext("Delete")} + +
+
+ + ${gettext("Copy Key")} + + `, + ]; + } + +}