import "@goauthentik/admin/applications/ProviderSelectModal"; import { iconHelperText } from "@goauthentik/admin/helperText"; import { DEFAULT_CONFIG } from "@goauthentik/common/api/config"; import { first } from "@goauthentik/common/utils"; import "@goauthentik/components/ak-file-input"; import "@goauthentik/components/ak-radio-input"; import "@goauthentik/components/ak-switch-input"; import "@goauthentik/components/ak-text-input"; import "@goauthentik/components/ak-textarea-input"; import "@goauthentik/elements/Alert.js"; import { CapabilitiesEnum, WithCapabilitiesConfig, } from "@goauthentik/elements/Interface/capabilitiesProvider"; import "@goauthentik/elements/forms/FormGroup"; import "@goauthentik/elements/forms/HorizontalFormElement"; import "@goauthentik/elements/forms/ModalForm"; import { ModelForm } from "@goauthentik/elements/forms/ModelForm"; import "@goauthentik/elements/forms/ProxyForm"; import "@goauthentik/elements/forms/Radio"; import "@goauthentik/elements/forms/SearchSelect"; import "@patternfly/elements/pf-tooltip/pf-tooltip.js"; import { msg } from "@lit/localize"; import { TemplateResult, html, nothing } from "lit"; import { customElement, property, state } from "lit/decorators.js"; import { ifDefined } from "lit/directives/if-defined.js"; import { Application, CoreApi, Provider } from "@goauthentik/api"; import { policyOptions } from "./PolicyOptions.js"; import "./components/ak-backchannel-input"; import "./components/ak-provider-search-input"; @customElement("ak-application-form") export class ApplicationForm extends WithCapabilitiesConfig(ModelForm) { constructor() { super(); this.handleConfirmBackchannelProviders = this.handleConfirmBackchannelProviders.bind(this); this.makeRemoveBackchannelProviderHandler = this.makeRemoveBackchannelProviderHandler.bind(this); } async loadInstance(pk: string): Promise { const app = await new CoreApi(DEFAULT_CONFIG).coreApplicationsRetrieve({ slug: pk, }); this.clearIcon = false; this.backchannelProviders = app.backchannelProvidersObj || []; return app; } @property({ attribute: false }) provider?: number; @state() backchannelProviders: Provider[] = []; @property({ type: Boolean }) clearIcon = false; getSuccessMessage(): string { return this.instance ? msg("Successfully updated application.") : msg("Successfully created application."); } async send(data: Application): Promise { let app: Application; data.backchannelProviders = this.backchannelProviders.map((p) => p.pk); if (this.instance) { app = await new CoreApi(DEFAULT_CONFIG).coreApplicationsUpdate({ slug: this.instance.slug, applicationRequest: data, }); } else { app = await new CoreApi(DEFAULT_CONFIG).coreApplicationsCreate({ applicationRequest: data, }); } if (this.can(CapabilitiesEnum.CanSaveMedia)) { const icon = this.getFormFiles().metaIcon; if (icon || this.clearIcon) { await new CoreApi(DEFAULT_CONFIG).coreApplicationsSetIconCreate({ slug: app.slug, file: icon, clear: this.clearIcon, }); } } else { await new CoreApi(DEFAULT_CONFIG).coreApplicationsSetIconUrlCreate({ slug: app.slug, filePathRequest: { url: data.metaIcon || "", }, }); } return app; } handleConfirmBackchannelProviders(items: Provider[]) { this.backchannelProviders = items; this.requestUpdate(); return Promise.resolve(); } makeRemoveBackchannelProviderHandler(provider: Provider) { return () => { const idx = this.backchannelProviders.indexOf(provider); this.backchannelProviders.splice(idx, 1); this.requestUpdate(); }; } handleClearIcon(ev: Event) { ev.stopPropagation(); if (!(ev instanceof InputEvent) || !ev.target) { return; } this.clearIcon = Boolean((ev.target as HTMLInputElement).checked); } renderForm(): TemplateResult { const alertMsg = msg( "Using this form will only create an Application. In order to authenticate with the application, you will have to manually pair it with a Provider.", ); return html`
${this.instance ? nothing : html`${alertMsg}`} `} > ${msg("UI settings")}
${this.can(CapabilitiesEnum.CanSaveMedia) ? html` ${this.instance?.metaIcon ? html` ` : html``}` : html` `}
`; } } declare global { interface HTMLElementTagNameMap { "ak-application-form": ApplicationForm; } }