import { t } from "@lingui/macro"; import { CSSResult, customElement, html, property, TemplateResult } from "lit-element"; import { AKResponse } from "../../api/Client"; import { TablePage } from "../../elements/table/TablePage"; import PFDescriptionList from "@patternfly/patternfly/components/DescriptionList/description-list.css"; import "../../elements/forms/ModalForm"; import "../../elements/buttons/ActionButton"; import { TableColumn } from "../../elements/table/Table"; import { PAGE_SIZE } from "../../constants"; import { CoreApi, User } from "@goauthentik/api"; import { DEFAULT_CONFIG, tenant } from "../../api/Config"; import "../../elements/forms/DeleteBulkForm"; import "./UserActiveForm"; import "./UserForm"; import "./UserResetEmailForm"; import { showMessage } from "../../elements/messages/MessageContainer"; import { MessageLevel } from "../../elements/messages/Message"; import { first } from "../../utils"; import { until } from "lit-html/directives/until"; @customElement("ak-user-list") export class UserListPage extends TablePage { expandable = true; checkbox = true; searchEnabled(): boolean { return true; } pageTitle(): string { return t`Users`; } pageDescription(): string { return ""; } pageIcon(): string { return "pf-icon pf-icon-user"; } @property() order = "last_login"; @property({ type: Boolean }) hideServiceAccounts = true; static get styles(): CSSResult[] { return super.styles.concat(PFDescriptionList); } apiEndpoint(page: number): Promise> { return new CoreApi(DEFAULT_CONFIG).coreUsersList({ ordering: this.order, page: page, pageSize: PAGE_SIZE, search: this.search || "", attributes: this.hideServiceAccounts ? JSON.stringify({ "goauthentik.io/user/service-account__isnull": "true", }) : undefined, }); } columns(): TableColumn[] { return [ new TableColumn(t`Name`, "username"), new TableColumn(t`Active`, "active"), new TableColumn(t`Last login`, "last_login"), new TableColumn(t`Actions`), ]; } renderToolbarSelected(): TemplateResult { const disabled = this.selectedElements.length < 1; return html` { return [ { key: t`Username`, value: item.username }, { key: t`ID`, value: item.pk.toString() }, ]; }} .usedBy=${(item: User) => { return new CoreApi(DEFAULT_CONFIG).coreUsersUsedByList({ id: item.pk, }); }} .delete=${(item: User) => { return new CoreApi(DEFAULT_CONFIG).coreUsersDestroy({ id: item.pk, }); }} > `; } row(item: User): TemplateResult[] { return [ html`
${item.username}
${item.name}
`, html`${item.isActive ? t`Yes` : t`No`}`, html`${first(item.lastLogin?.toLocaleString(), "-")}`, html` ${t`Update`} ${t`Update User`} ${t`Impersonate`} `, ]; } renderExpanded(item: User): TemplateResult { return html`
${t`User status`}
${item.isActive ? t`Active` : t`Inactive`}
${item.isSuperuser ? t`Superuser` : t`Regular user`}
${t`Change status`}
{ return new CoreApi( DEFAULT_CONFIG, ).coreUsersPartialUpdate({ id: item.pk || 0, patchedUserRequest: { username: item.username, name: item.name, isActive: !item.isActive, }, }); }} >
${until( tenant().then((te) => { if (!te.flowRecovery) { return html``; } return html`
${t`Recovery`}
{ return new CoreApi(DEFAULT_CONFIG) .coreUsersRecoveryRetrieve({ id: item.pk || 0, }) .then((rec) => { showMessage({ level: MessageLevel.success, message: t`Successfully generated recovery link`, description: rec.link, }); }) .catch((ex: Response) => { ex.json().then(() => { showMessage({ level: MessageLevel.error, message: t`No recovery flow is configured.`, }); }); }); }} > ${t`Copy recovery link`} ${item.email ? html` ${t`Send link`} ${t`Send recovery link to user`} ` : html`${t`Recovery link cannot be emailed, user has no email address saved.`}`}
`; }), )}
`; } renderToolbar(): TemplateResult { return html` ${t`Create`} ${t`Create User`} ${super.renderToolbar()} `; } renderToolbarAfter(): TemplateResult { return html` 
{ this.hideServiceAccounts = !this.hideServiceAccounts; this.page = 1; this.fetch(); }} />
`; } }