import "@goauthentik/admin/users/ServiceAccountForm"; import "@goauthentik/admin/users/UserActiveForm"; import "@goauthentik/admin/users/UserForm"; import "@goauthentik/admin/users/UserPasswordForm"; import "@goauthentik/admin/users/UserResetEmailForm"; import { DEFAULT_CONFIG } from "@goauthentik/common/api/config"; import { MessageLevel } from "@goauthentik/common/messages"; import { uiConfig } from "@goauthentik/common/ui/config"; import { first } from "@goauthentik/common/utils"; import { rootInterface } from "@goauthentik/elements/Base"; import { PFColor } from "@goauthentik/elements/Label"; import "@goauthentik/elements/buttons/ActionButton"; import "@goauthentik/elements/buttons/Dropdown"; import "@goauthentik/elements/forms/DeleteBulkForm"; import { Form } from "@goauthentik/elements/forms/Form"; import "@goauthentik/elements/forms/HorizontalFormElement"; import "@goauthentik/elements/forms/ModalForm"; import { showMessage } from "@goauthentik/elements/messages/MessageContainer"; import { getURLParam, updateURLParams } from "@goauthentik/elements/router/RouteMatch"; import { PaginatedResponse } from "@goauthentik/elements/table/Table"; import { Table, TableColumn } from "@goauthentik/elements/table/Table"; import { UserOption } from "@goauthentik/elements/user/utils"; import "@patternfly/elements/pf-tooltip/pf-tooltip.js"; import { msg, str } from "@lit/localize"; import { CSSResult, TemplateResult, html } from "lit"; import { customElement, property, state } from "lit/decorators.js"; import { ifDefined } from "lit/directives/if-defined.js"; import PFAlert from "@patternfly/patternfly/components/Alert/alert.css"; import PFBanner from "@patternfly/patternfly/components/Banner/banner.css"; import PFDescriptionList from "@patternfly/patternfly/components/DescriptionList/description-list.css"; import { CapabilitiesEnum, CoreApi, CoreUsersListTypeEnum, Group, ResponseError, User, } from "@goauthentik/api"; @customElement("ak-user-related-add") export class RelatedUserAdd extends Form<{ users: number[] }> { @property({ attribute: false }) group?: Group; @state() usersToAdd: User[] = []; getSuccessMessage(): string { return msg("Successfully added user(s)."); } async send(data: { users: number[] }): Promise<{ users: number[] }> { await Promise.all( data.users.map((user) => { return new CoreApi(DEFAULT_CONFIG).coreGroupsAddUserCreate({ groupUuid: this.group?.pk || "", userAccountRequest: { pk: user, }, }); }), ); return data; } renderInlineForm(): TemplateResult { return html`${this.group?.isSuperuser ? html`` : html``}
{ this.usersToAdd = items; this.requestUpdate(); return Promise.resolve(); }} >
${this.usersToAdd.map((user) => { return html` { const idx = this.usersToAdd.indexOf(user); this.usersToAdd.splice(idx, 1); this.requestUpdate(); }} > ${UserOption(user)} `; })}
`; } } @customElement("ak-user-related-list") export class RelatedUserList extends Table { expandable = true; checkbox = true; searchEnabled(): boolean { return true; } @property({ attribute: false }) targetGroup?: Group; @property() order = "last_login"; @property({ type: Boolean }) hideServiceAccounts = getURLParam("hideServiceAccounts", true); static get styles(): CSSResult[] { return super.styles.concat(PFDescriptionList, PFAlert, PFBanner); } async apiEndpoint(page: number): Promise> { return new CoreApi(DEFAULT_CONFIG).coreUsersList({ ordering: this.order, page: page, pageSize: (await uiConfig()).pagination.perPage, search: this.search || "", groupsByPk: this.targetGroup ? [this.targetGroup.pk] : [], type: this.hideServiceAccounts ? [CoreUsersListTypeEnum.External, CoreUsersListTypeEnum.Internal] : undefined, }); } columns(): TableColumn[] { return [ new TableColumn(msg("Name"), "username"), new TableColumn(msg("Active"), "is_active"), new TableColumn(msg("Last login"), "last_login"), new TableColumn(msg("Actions")), ]; } renderToolbarSelected(): TemplateResult { const disabled = this.selectedElements.length < 1; return html` { return [ { key: msg("Username"), value: item.username }, { key: msg("ID"), value: item.pk.toString() }, { key: msg("UID"), value: item.uid }, ]; }} .delete=${(item: User) => { return new CoreApi(DEFAULT_CONFIG).coreGroupsRemoveUserCreate({ groupUuid: this.targetGroup?.pk || "", userAccountRequest: { pk: item.pk, }, }); }} > `; } row(item: User): TemplateResult[] { return [ html`
${item.username}
${item.name}
`, html` ${item.isActive ? msg("Yes") : msg("No")} `, html`${first(item.lastLogin?.toLocaleString(), msg("-"))}`, html` ${msg("Update")} ${msg("Update User")} ${rootInterface()?.config?.capabilities.includes(CapabilitiesEnum.CanImpersonate) ? html` { return new CoreApi(DEFAULT_CONFIG) .coreUsersImpersonateCreate({ id: item.pk, }) .then(() => { window.location.href = "/"; }); }} > ${msg("Impersonate")} ` : html``}`, ]; } renderExpanded(item: User): TemplateResult { return html`
${msg("User status")}
${item.isActive ? msg("Active") : msg("Inactive")}
${item.isSuperuser ? msg("Superuser") : msg("Regular user")}
${msg("Change status")}
{ return new CoreApi( DEFAULT_CONFIG, ).coreUsersPartialUpdate({ id: item.pk || 0, patchedUserRequest: { isActive: !item.isActive, }, }); }} >
${msg("Recovery")}
${msg("Update password")} ${msg("Update password")} ${rootInterface()?.tenant?.flowRecovery ? html` { return new CoreApi(DEFAULT_CONFIG) .coreUsersRecoveryRetrieve({ id: item.pk, }) .then((rec) => { showMessage({ level: MessageLevel.success, message: msg( "Successfully generated recovery link", ), description: rec.link, }); }) .catch((ex: ResponseError) => { ex.response.json().then(() => { showMessage({ level: MessageLevel.error, message: msg( "No recovery flow is configured.", ), }); }); }); }} > ${msg("Copy recovery link")} ${item.email ? html` ${msg("Send link")} ${msg("Send recovery link to user")} ` : html`${msg( "Recovery link cannot be emailed, user has no email address saved.", )}`} ` : html`

${msg( "To let a user directly reset a their password, configure a recovery flow on the currently active tenant.", )}

`}
`; } renderToolbar(): TemplateResult { return html` ${this.targetGroup ? html` ${msg("Add")} ${msg("Add User")} ${this.targetGroup.isSuperuser ? html`
${msg( "Warning: This group is configured with superuser access. Added users will have superuser access.", )}
` : html``}
` : html``} ${super.renderToolbar()} `; } renderToolbarAfter(): TemplateResult { return html` 
`; } }