import { CoreApi, Group, User } from "@goauthentik/api"; import { t } from "@lingui/macro"; import { customElement } from "lit-element"; import { html, TemplateResult } from "lit-html"; import { DEFAULT_CONFIG } from "../../api/Config"; import { ifDefined } from "lit-html/directives/if-defined"; import "../../elements/forms/HorizontalFormElement"; import "../../elements/CodeMirror"; import "./GroupSelectModal"; import YAML from "yaml"; import { first } from "../../utils"; import { ModelForm } from "../../elements/forms/ModelForm"; import { until } from "lit-html/directives/until"; @customElement("ak-user-form") export class UserForm extends ModelForm { 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`Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.`}

${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.`}

`; } }