import { DEFAULT_CONFIG } from "@goauthentik/web/api/Config"; import "@goauthentik/web/elements/CodeMirror"; import "@goauthentik/web/elements/forms/HorizontalFormElement"; import { ModelForm } from "@goauthentik/web/elements/forms/ModelForm"; import "@goauthentik/web/pages/users/GroupSelectModal"; import { first } from "@goauthentik/web/utils"; import YAML from "yaml"; import { t } from "@lingui/macro"; import { CSSResult, TemplateResult, css, html } from "lit"; import { customElement } from "lit/decorators.js"; import { ifDefined } from "lit/directives/if-defined.js"; import { until } from "lit/directives/until.js"; import { CoreApi, Group, User } from "@goauthentik/api"; @customElement("ak-user-form") export class UserForm extends ModelForm { static get styles(): CSSResult[] { return super.styles.concat(css` .pf-c-button.pf-m-control { height: 100%; } .pf-c-form-control { height: auto !important; } `); } loadInstance(pk: number): Promise { return new CoreApi(DEFAULT_CONFIG).coreUsersRetrieve({ id: pk, }); } getSuccessMessage(): string { if (this.instance) { return t`Successfully updated user.`; } else { return t`Successfully created user.`; } } send = (data: User): Promise => { if (this.instance?.pk) { return new CoreApi(DEFAULT_CONFIG).coreUsersUpdate({ id: this.instance.pk, userRequest: data, }); } else { return new CoreApi(DEFAULT_CONFIG).coreUsersCreate({ userRequest: data, }); } }; renderForm(): TemplateResult { return html`

${t`User's primary identifier. 150 characters or fewer.`}

${t`User's display name.`}

${t`Designates whether this user should be treated as active. Unselect this instead of deleting accounts.`}

{ // Because the model only has the IDs, map the group list to IDs const ids = items.map((g) => g.pk); if (!this.instance) this.instance = {} as User; this.instance.groups = Array.from(this.instance?.groups || []).concat( ids, ); this.requestUpdate(); return Promise.resolve(); }} >
${until( new CoreApi(DEFAULT_CONFIG) .coreGroupsList({ ordering: "name", }) .then((groups) => { return groups.results.map((group) => { const selected = Array.from( this.instance?.groups || [], ).some((sg) => { return sg == group.pk; }); if (!selected) return; return html` { if (!this.instance) return; const groups = Array.from( this.instance?.groups || [], ); const idx = groups.indexOf(group.pk); groups.splice(idx, 1); this.instance.groups = groups; this.requestUpdate(); }} > ${group.name} `; }); }), html``, )}

${t`Set custom attributes using YAML or JSON.`}

`; } }