import "@goauthentik/admin/users/GroupSelectModal"; import { DEFAULT_CONFIG } from "@goauthentik/common/api/config"; import { first } from "@goauthentik/common/utils"; import "@goauthentik/elements/CodeMirror"; import { CodeMirrorMode } from "@goauthentik/elements/CodeMirror"; import "@goauthentik/elements/forms/HorizontalFormElement"; import { ModelForm } from "@goauthentik/elements/forms/ModelForm"; import "@goauthentik/elements/forms/Radio"; import YAML from "yaml"; import { msg, str } from "@lit/localize"; import { CSSResult, TemplateResult, css, html } from "lit"; import { customElement, property } from "lit/decorators.js"; import { ifDefined } from "lit/directives/if-defined.js"; import { CoreApi, Group, User, UserTypeEnum } from "@goauthentik/api"; @customElement("ak-user-form") export class UserForm extends ModelForm { @property({ attribute: false }) group?: Group; static get defaultUserAttributes(): { [key: string]: unknown } { return {}; } 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 msg("Successfully updated user."); } else { if (this.group) { return msg(str`Successfully created user and added to group ${this.group.name}`); } return msg("Successfully created user."); } } async send(data: User): Promise { if (data.attributes === null) { data.attributes = UserForm.defaultUserAttributes; } let user; if (this.instance?.pk) { user = await new CoreApi(DEFAULT_CONFIG).coreUsersPartialUpdate({ id: this.instance.pk, patchedUserRequest: data, }); } else { data.groups = []; user = await new CoreApi(DEFAULT_CONFIG).coreUsersCreate({ userRequest: data, }); } if (this.group) { await new CoreApi(DEFAULT_CONFIG).coreGroupsAddUserCreate({ groupUuid: this.group.pk, userAccountRequest: { pk: user.pk, }, }); } return user; } renderForm(): TemplateResult { return html`

${msg("User's primary identifier. 150 characters or fewer.")}

${msg("User's display name.")}

${msg( "Designates whether this user should be treated as active. Unselect this instead of deleting accounts.", )}

${msg("Set custom attributes using YAML or JSON.")}

`; } }