import YAML from "yaml"; import { t } from "@lingui/macro"; import { html, TemplateResult } from "lit"; import { customElement } from "lit/decorators"; import { ifDefined } from "lit/directives/if-defined"; import { until } from "lit/directives/until"; import { CoreApi, Group, User } from "@goauthentik/api"; import { DEFAULT_CONFIG } from "../../api/Config"; import "../../elements/CodeMirror"; import "../../elements/forms/HorizontalFormElement"; import { ModelForm } from "../../elements/forms/ModelForm"; import { first } from "../../utils"; import "./GroupSelectModal"; @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.`}

`; } }