import { DEFAULT_CONFIG } from "@goauthentik/web/api/Config"; import "@goauthentik/web/elements/CodeMirror"; import "@goauthentik/web/elements/chips/Chip"; import "@goauthentik/web/elements/chips/ChipGroup"; import "@goauthentik/web/elements/forms/HorizontalFormElement"; import { ModelForm } from "@goauthentik/web/elements/forms/ModelForm"; import { UserOption } from "@goauthentik/web/elements/user/utils"; import "@goauthentik/web/pages/groups/MemberSelectModal"; 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-group-form") export class GroupForm 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: string): Promise { return new CoreApi(DEFAULT_CONFIG).coreGroupsRetrieve({ groupUuid: pk, }); } getSuccessMessage(): string { if (this.instance) { return t`Successfully updated group.`; } else { return t`Successfully created group.`; } } send = (data: Group): Promise => { if (this.instance?.pk) { return new CoreApi(DEFAULT_CONFIG).coreGroupsUpdate({ groupUuid: this.instance.pk, groupRequest: data, }); } else { data.users = Array.from(this.instance?.users || []); return new CoreApi(DEFAULT_CONFIG).coreGroupsCreate({ groupRequest: data, }); } }; renderForm(): TemplateResult { return html`

${t`Users added to this group will be superusers.`}

{ // Because the model only has the IDs, map the user list to IDs const ids = items.map((u) => u.pk || 0); if (!this.instance) this.instance = {} as Group; this.instance.users = Array.from(this.instance?.users || []).concat( ids, ); this.requestUpdate(); return Promise.resolve(); }} >
${until( new CoreApi(DEFAULT_CONFIG) .coreUsersList({ ordering: "username", }) .then((users) => { return users.results.map((user) => { const selected = Array.from( this.instance?.users || [], ).some((su) => { return su == user.pk; }); if (!selected) return; return html` { if (!this.instance) return; const users = Array.from( this.instance?.users || [], ); const idx = users.indexOf(user.pk || 0); users.splice(idx, 1); this.instance.users = users; this.requestUpdate(); }} > ${UserOption(user)} `; }); }), html``, )}

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

`; } }